2016-07-15 55 views
0

我在使用角度數據表選擇某個數據時如何觸發事件有問題。如何在角度數據表select上創建回調函數

我想啓用編輯和刪除按鈕,當有一個選定的行。

這裏是我的代碼:

app.controller('SampleController', function($http, DTOptionsBuilder, DTColumnBuilder, $q) { 
     var vm = this; 

     vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() { 
      var defer = $q.defer(); 
      $http.get("{!! url('sample/api/v1/questions?questionType=1') !!}") 
      .then(function(response) { 
       defer.resolve(response.data.active_question_contents); 
      }); 

      return defer.promise; 
     }) 
     .withDOM('frtip') 
     .withPaginationType('full_numbers') 
     .withDisplayLength(5) 
     .withButtons([{ 
      text: 'Add', 
      key: '1', 
      action: function(e, dt, node, config) { 
      alert('Add'); 
      } 
     }, { 
      text: 'Edit', 
      key: '2', 
      action: function(e, dt, node, config) { 
      alert('Edit'); 
      }, 
      enabled: false 
     }, { 
      text: 'Delete', 
      key: '3', 
      action: function(e, dt, node, config) { 
      alert('Delete'); 
      }, 
      enabled: false 
     }]) 
     .withSelect({ 
      style: 'os', 
      selector: 'td:first-child' 
     }); 

我試圖drawCallback但它只能觸發一次。

回答

1

我已經解決了!我只是在drawCallback函數內添加了this.DataTable()。

下面是代碼:

vm.dtOptions.drawCallback = function() { 
    var table = this.DataTable(); 

    table.on('select', function() { 
      alert('Selected!'); 
      // Enable/disable buttons here... 
    }); 
};