2015-02-10 51 views
0

學習Laravel(4.1)和我在將錶行ID傳遞給控制器​​時遇到問題。我想要做的是用戶點擊圖標來刪除記錄。模態窗口首先出現,詢問他們是否確定要刪除。如果他們單擊是,則應將ID發送到控制器,以便可以進行刪除。從引導將行ID傳遞給laravel控制器3刪除模態

我Laravel 路線看起來像這樣

DELETE entir/guests/{guests} | entir.guests.destroy | [email protected] 

控制器爲破壞看起來是這樣的。現在我只是打印出ID和一條消息。

public function destroy($id) 
{ 
print_r($id); 
return 'You called the Destroy function'; 
} 

是建立在表中的HTML代碼

<table id="table" data-toggle="table" data-url="{{ action('[email protected]') }}" data-click-to-select="true" data-single-select="true" data-pagination="true" data-search="true">     
<thead> 
    <tr> 
     <th data-field="id" data-align="right" data-sortable="true">ID</th> 
     ... 
     <th data-field="operate" data-formatter="operateFormatter" data-events="operateEvents">Actions</th> 
    </tr> 
</thead> 

下面是表的javascript和刪除事件。

<script> 
function operateFormatter(value, row, index) { 
    return [ 
     '<a class="edit ml10" href="javascript:void(0)" title="Entir Guest">', 
     '<i class="glyphicon glyphicon-edit"></i>', 
     '</a>', 
     '<a class="remove ml10" href="javascript:void(0)" title="Remove">', 
     '<i class="glyphicon glyphicon-remove"></i>', 
     '</a>' 
     ].join(''); 
} 
window.operateEvents = {         
     'click .remove': 
     function (e, value, row, index) { 
      $('#deleteModal').data('id', row.id).modal('show'); 
       var id = JSON.stringify(row.id); 
       // Alert the ID for testing         
       alert('Remove ID: ' + id);     
      } 
}; 

刪除HTML模式

<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header modal-header-danger"> 
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
      <h4 class="modal-title" id="myModalLabel">Warning!</h4> 
     </div> 
     <div class="modal-body"> 
      <h4> Are you sure you want to Delete?</h4> 
     </div> 
     <!--/modal-body-collapse --> 
     <div class="modal-footer"> 
      {{Form::open(array('method'=>'DELETE', 'route' => array('entir.guests.destroy')))}} 
      <button type="submit" class="btn btn-danger" id="btnDelteYes">Yes</button> 
      <button type="button" class="btn btn-default" data-dismiss="modal">No</button> 
      {{ Form::close() }} 
     </div> 
     <!--/modal-footer-collapse --> 
    </div> 
    <!-- /.modal-content --> 
</div> 
<!-- /.modal-dialog --> 

可以說我有行1 - 20號的1 -20。如果我選擇第1行並單擊刪除圖標,則會看到一條提示「刪除ID:1」。然後模態窗口出現並要求您確認。如果我按下yes,則控制器會被調用,因爲我從destroy函數中獲取消息。正如你所看到的,我沒有通過這個ID,因爲我不確定這樣做的確切方式。我已經嘗試了一些我在網上找到的例子,但是最終我得到了laravel中的一個未定義的變量錯誤。任何幫助表示讚賞。

這是我假設我需要通過ID

{{Form::open(array('method'=>'DELETE', 'route' => array('entir.guests.destroy')))}} 

回答

1

通過經由控制器作爲HREF傳遞ID解決的問題。

相關問題