0

I am having a problem on how to trigger an event when a certain data is selected using angular datatable select.

I want to enable Edit and Delete Button when there is a selected row.

Here is my code:

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'
        });

I tried drawCallback but it only triggered once.

1 Answer 1

2

I already solved it! I just added this.DataTable() inside the drawCallback function.

Here is the code:

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

     table.on('select', function() {
          alert('Selected!');
          // Enable/disable buttons here...
     });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.