2016-12-02 37 views
1

DLG莫代爾我使用的數據表在那裏我已經宣佈了一些按鈕來觸發行動爲基礎的哪個按鈕用戶按:需要傳遞的參數,從數據表按鈕動作

var dpcroles = $('#example_roles').DataTable({ 
    dom: 'Bfrtip', 
    "columnDefs": [ 
     { 
      "targets": [0], 
      "visible": false 
     } 
    ], 
    buttons: [ 
     { 
      text: 'Add', 
      id: 'btn_add', 
      "data-action":'add', 
      action: function (e, dt, node, config) { 
       $('#modal_action').modal(); 
      } 
     }, 
     { 
      id: 'btn_edit', 
      text: 'Edit', 
      action: function (e, dt, node, config) { 
       var id = $(e.relatedTarget).data('id'); 
       console.log(id); 
       $('#model_action').modal(id); 
      }, 
      enabled: true 
     }, 

    ], 

然而,如何傳遞參數的JavaScript它處理模態開放?

$('#model_action').on('show.bs.modal', function(e) { 
    // if button 'edit', do something 
     . 
     . 
    // else if button 'add' do other thing 
     . 
     . 
     . 
}) 

回答

1

您可以調用modal()方法之前設置了一個模式data-屬性。例如:

$('#modal_action').data('mode', 'edit'); 
$('#modal_action').modal(); 

然後在你的事件處理程序,您可以檢索數據。例如:

$('#modal_action').on('show.bs.modal', function(e) { 
    var $modal = $(this); 

    var mode = $modal.data('mode'); 

    // if button 'edit', do something 
    if(mode === 'edit'){ 

    // else if button 'add' do other thing 
    } else { 

    } 
}); 

另一種解決方案是使用兩種不同的模式,每個動作使用一種模式。

請注意,在您的示例中,您可能拼寫錯誤的模式名稱,它應該是modal_actionmodel_action

+0

好的,謝謝,它確實有效。謝謝。以上代碼示例在我清理代碼進行公開審查時發生了拼寫錯誤。 –