Skip to content

Instantly share code, notes, and snippets.

@wiedikerli
Last active February 15, 2016 15:55
Show Gist options
  • Save wiedikerli/ac25593730b103bf6034 to your computer and use it in GitHub Desktop.
Save wiedikerli/ac25593730b103bf6034 to your computer and use it in GitHub Desktop.
set default selection for smart table. Check: http://plnkr.co/edit/ImstnnRdtpaL4z1XQGG2?p=info
app.directive('stDefaultSelection', function() {
return {
require: 'stTable',
restrict: 'A',
scope: {
selection: '=stDefaultSelection',
},
link: function link(scope, element, attrs, controller) {
var pagination = null,
hasLoaded = false,
selectionMode = 'single';
if (scope.selection != null) {
scope.$watch(function() {
pagination = controller.tableState().pagination;
if (pagination != null && pagination.number != null && hasLoaded == false) {
var rows = controller.getFilteredCollection(),
indexOfRow = angular.isNumber(scope.selection) ? scope.selection : rows.indexOf(scope.selection),
finalPage = Math.ceil(indexOfRow / pagination.number) - 1; // starts at zero
// select row
controller.select(rows[indexOfRow], selectionMode);
if (indexOfRow > -1) {
controller.slice(finalPage * pagination.number, pagination.number);
hasLoaded = true;
}
}
}, true);
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment