Ronan Rafferty
Ronan Rafferty

Reputation: 189

Angular orderBy filter in a directive

I want to use Angular filters from within a directive but I can't seem to work out what todo. I have: <tr ng-repeat="row in dataset | filter:searchon | orderBy:sorton ">

I have the sorton as a isolated scope variable and change its value on click.

  .directive('gbsdatatable', function () {
return {
    restrict: 'A',
    transclude: false,
    replace: false,
    scope: {
      dataset: '=',
      searchon: '=',
      exportcsv: '=',
      pagination: '=',
      colmenu: '=',
      sorton: "="
    },

fiddle here - note the filter on line 23

Upvotes: 0

Views: 2266

Answers (1)

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

Use following as your template,

<tr ng-repeat=\"row in dataset | filter:searchon | orderBy:predicate:reverse \">

predicate here is the column name you want to order by.

reverse can be either true or false depending on its order.

You also need to bind ng-click to <td> elements inside <thead>

ng-click="predicate = 'comp'; reverse=!reverse"

On click, predicate is set to comp which is the name of column and reverse is set to !reverse.

Working Fiddle here.

Upvotes: 4

Related Questions