2016-07-15 57 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... 
    }); 
};