0

I have read several similar questions but none worked for me on this issue. I am using laravel-datatable with angular js. But 'ng-click' does not work with the dynamically generated datatable buttons. I read about $compile but dont know how to implement it with the table. Am very new to angular js.

When I click on the button nothing happens.

app.controller('myController', ['$scope','$compile', function($scope, $compile) {

$('#stafftbl').DataTable( {
      paging: true,
      "ordering": true,
      "searching": true,
      "bInfo" : true,
      deferRender: true,
      ajax: 'get_staffsalary',
      columns: [
          { data: 'staff_num', name: 'staff_num'},
          { data: 'staffname ', name: 'staffname' },
          { data: 'salary', name: 'salary' },
          { data: 'date_effected', name: 'date_effected' },
          { data: 'updated_at', name: 'updated_at' },
          { data: null, render: function (data, type, full) {
                  return '<button class="btn btn-xs btn-primary" ng-click="update('+full.id+')">Update Salary</button> ';
          }},

      ],

  });

   $scope.update= function (id) {
        alert(id)
    }
 }]);

Please any help with this?

3

2 Answers 2

1

Yes, you are correct in that you need to use $compile.

After dynamically adding html with angular attributes in it, like you are with your data table, you must call $compile so that angular can pick up the attributes.

You'll need to inject the $compile service into your controller right after $scope. Then, after the HTML has been added you will need to compile the new DOM and link it to the scope by calling $compile(new_html)($scope) in the context of your controller.

Refer to the $compile doco for reference

Sign up to request clarification or add additional context in comments.

5 Comments

Where do I add this code? $compile(new_html)($scope)
Where is that first code block in your question called from? If it's called within your controller, you should be able to call $compile in a callback on the DataTable. I'm not familiar with the DataTable plugin you are using, but it should have a callback option for when the html has been added.
I have now moved the codes to my controller and edited the question too. Where do I call the $compile(new_html)($scope)
I have added $compile(new_html)($scope) properly, and its working fine now. Do I need to post the new code as an answer?
Good to hear you got it working :) Yeah, add your solution as an answer. It might help other people. I'd appreciate an upvote if my answer helped you at all. Thanks.
0

I solved it using @MJL 's answer. Here is the full block of code, it may hep someone else

app.controller('myCtrl', ['$scope','$compile', function($scope, $compile) {

var table = $('#stafftbl').DataTable( {
    processing: false,
    serverSide: false,
    deferRender: true,
    ajax: 'get_staffsalary',
    columns: [
        { data: 'staff_num', name: 'staff_num'},
        { data: 'salary', name: 'salary' },
        { data: 'date_effected', name: 'date_effected' },
        { data: 'updated_at', name: 'updated_at' },
        { data: null, render: function (data, type, full) {
            return '<button class="btn btn-xs btn-primary text-size-small" ng-click="clickme()>Update</button> ';
        }},

    ],
    "createdRow": function ( row, data, index ) {
        $compile(row)($scope);  //add this to compile the DOM
    }

})

 $scope.clickme = function () {
   alert('clicked me')
}
}]);

2 Comments

@PrashantPokhriyal, the question should be closed, totally 1:1 dub of several previous questions, the answer above as well.
@PrashantPokhriyal, the question should be closed, totally 1:1 dub of several previous questions, the answer above as well.

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.