如何在 angular 数据表 select 上创建回调函数

How to create a callback function on angular datatable select

我在使用 angular 数据表 select 编辑特定数据时如何触发事件时遇到问题 select。

我想在有 selected 行时启用编辑和删除按钮。

这是我的代码:

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 但它只触发了一次。

我已经解决了!我刚刚在 drawCallback 函数中添加了 this.DataTable()。

代码如下:

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

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