2

So I have this code:

Controller:

    vm.dtColumns = [
        DTColumnBuilder.newColumn('product_code').withTitle('Code'),
        DTColumnBuilder.newColumn('product_name').withTitle('Name'),
        DTColumnBuilder.newColumn(null).withTitle('Actions').notSortable()
        .renderWith(function(data, type, full, meta) {
            return ` <button class="btn btn-info btn-raised" ng-click="openViewProductModal(${data.product_id})">View</button>`;
        })
    ];

    $scope.openViewProductModal = function(id) {
       console.log(id)
    }

HTML:

<div ng-controller="ProductCtrl as productControl" ng-init="loadProducts()">
    <table datatable="" dt-options="productControl.dtOptions" dt-columns="productControl.dtColumns" dt-instance="productControl.dtInstance" class="row-border hover"></table>
</div>

I can't even get the console.log() to work though everything is rendered perfectly. Am I missing something here?

1

1 Answer 1

1

try below solution.Put a createdRow function after vm.dtColumns , you need to compile row for the ng-click event.

  vm.dtColumns = [
        DTColumnBuilder.newColumn('product_code').withTitle('Code'),
        DTColumnBuilder.newColumn('product_name').withTitle('Name'),
        DTColumnBuilder.newColumn(null).withTitle('Actions').notSortable()
        .renderWith(function(data, type, full, meta) {
            return ` <button class="btn btn-info btn-raised" ng-click="openViewProductModal(${data.product_id})">View</button>`;
        })
    ];

    function createdRow(row, data, dataIndex) {
        $compile(angular.element(row).contents())($scope);
    }

    $scope.openViewProductModal = function(id) {
       console.log(id)
    }
Sign up to request clarification or add additional context in comments.

3 Comments

It worked. Now my problem is to how to pass the index of an item to the button.
@FewFlyBy ,I am not sure , but you may be find it in meta.
@chiragsatapara ng-click="openViewProductModal('+full.product_id+')" ...

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.