0

I have the directive from angular smart table for adding a checkbox selection to the rows of the table shown below. I want to be able to execute a custom function when I click on the checkbox. Is this possible ? I basically need to store the rowid or another property of the rows when the checkbox is clicked.

app.directive('csSelect', function () {
    return {
        require: '^stTable',
        template: '<input type="checkbox"/>',
        scope: {
            row: '=csSelect'
        },
        link: function (scope, element, attr, ctrl) {

            element.bind('change', function (evt) {
                scope.$apply(function () {
                    ctrl.select(scope.row, 'multiple');
                });
            });

            scope.$watch('row.isSelected', function (newValue, oldValue) {
                if (newValue === true) {
                    element.parent().addClass('st-selected');
                } else {
                    element.parent().removeClass('st-selected');
                }
            });
        }
    };
});

EDIT

The way this directive is used as follows:

<td cs-select="row"></td>

note that if I add ng-click to the td element to check for a clicked row, it does not matter whether I click on the checkbox. As soon as I click anywhere inside the td element, it acts as that I clicked on the row. So that's not a solution

4 Answers 4

1
app.directive('csSelect', function () {
    return {
        require: '^stTable',
        template: '<input type="checkbox"/>',
        scope: {
            row: '=csSelect',
            myFunc: '&cs-func'
        },
        link: function (scope, element, attr, ctrl) {

            element.bind('change', function (evt) {
                scope.$apply(function () {
                    ctrl.select(scope.row, 'multiple');
                });
            });

            scope.$watch('row.isSelected', function (newValue, oldValue) {
                if (newValue === true) {
                    element.parent().addClass('st-selected');
                    scope.myFunc();
                } else {
                    element.parent().removeClass('st-selected');
                }
            });
        }
    };
});

then

<td cs-select="row" cs-func='alert("hello")'></td>
Sign up to request clarification or add additional context in comments.

2 Comments

Tried this now but checkbox is not visible for some reason
Works great! Thanks for your time
1

Add ng-click to the directives template.

template: "<input type='checkbox' ng-click='myFunc()'/>"

And in link/controller of directive:

scope.myFunc = function() { //Code };

Comments

0

You can expose functions to your template html in your directive by adding them to the scope argument injected into your link function.

You can use this for your change handler, and you can use ngClass to evaluate whether to attach the css class to your input.

Try something along these lines:

return {
  // ...,
  template: '<input ng-change="changeHandler()" ng-class="{'st-selected': row.isSelected}">',
  link: function (scope, elem, attrs, parentCtrl) {
    scope.changeHandler = function () {
      // parentCtrl.whatever(scope.row);
    };
  }
};

One thing your input is missing is an ngModel. I'm not sure what all is going on in your stTable directive, but you can and probably should bind your checkbox directive to a model. row.isSelected springs to mind.

Comments

0

Try:

ng-click="controller.function()"

1 Comment

That unfortunately does not solve my problem, see the edit

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.