5

Is there a way to trigger an event on row select in AngularJS Smart Table?

This was a subject in other thread but still no answer to this point.

Unable to select the grid item using SmartTable in Angular JS

Community
  • 1
  • 1

2 Answers2

4

I needed this functionality in order to show a panel whenever at least 1 row was selected. I originally set up a watch, but decided that was too expensive.

I ended up adding a callback inside the stSelectRow directive.

ng.module('smart-table')
  .directive('stSelectRow', function () {
    return {
      restrict: 'A',
      require: '^stTable',
      scope: {
          row: '=stSelectRow',
          callback: '&stSelected' // ADDED THIS
      },
      link: function (scope, element, attr, ctrl) {
        var mode = attr.stSelectMode || 'single';
        element.bind('click', function ($event) {
          scope.$apply(function () {
              ctrl.select(scope.row, mode, $event.shiftKey);
              scope.callback(); // AND THIS
          });
        });

        //***///
      }
    };
  });

I was then able to pass a function from my controller to the directive (note: you could pass the selected row back, I didn't need to)

tr ng-repeat="row in customerResultsTable" st-select-row="row" st-select-mode="multiple" st-selected="rowSelected()">

Referenced this post for help Callback function inside directive attr defined in different attr

Community
  • 1
  • 1
What-About-Bob
  • 649
  • 5
  • 13
0

Here is an easy watch to place on a Smart Table:

// fired when table rows are selected
$scope.$watch('displayedCollection', function(row) {

  if(!row) return;

  // get selected row
  row.filter(function(r) {
     if (r.isSelected) {
       console.log(r);
     }
  })
}, true);

related html:

<table st-table="displayedCollection" st-safe-src="rowCollection" class="select-table table">

This github issue was helpful.

Banjer
  • 8,118
  • 5
  • 46
  • 61