angular-tablesort icon indicating copy to clipboard operation
angular-tablesort copied to clipboard

Define Default Sort Programmatically

Open aborchew opened this issue 10 years ago • 6 comments

Would be a nice feature to be able to specify a default sort column when the columns are being rendered via an ng-repeat:

Given the following controller

$scope.columns = ['foo', 'bar', 'biz'];
$scope.collection = [
  {
    'foo': 0,
    'bar': 7,
    'biz': 4
  },
  {
    'foo': 2,
    'bar': 9,
    'biz': 3
  },
  {
    'foo': 1,
    'bar': 1,
    'biz': 99
  }
  //, ...
]

Be able to specify 'bar' as the default sort key, as such


<table ts-wrapper ts-default-key="'bar'">
  <thead>
    <tr>
      <th ng-repeat="column in columns" ts-criteria="{{ column }}">{{ column }}</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in collection" ts-repeat>
      <td ng-repeat="column in columns">{{ row[column] }}</td>
    </tr>
  </tbody>
</table>

aborchew avatar Sep 28 '15 17:09 aborchew

+1. I just came here to look for this option.

chuckienorton avatar Nov 27 '15 21:11 chuckienorton

+1

dotob avatar Mar 16 '16 19:03 dotob

In case this helps anyone, this is the workaround I've implemented currently. At some point I'd like to actually write this feature but for now this admittedly hacky solution works.

<table ts-wrapper>
  <thead>
    <tr>
      <th ng-repeat="c in columns" ts-criteria="{{ c }}" id="{{ 'col-header-' + c }}">{{ c }}</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in collection" ts-repeat>
      <td ng-repeat="c in columns">{{ row[c] }}</td>
    </tr>
  </tbody>
</table>

Given the controller mentioned in the initial post (I know this shouldn't be in a controller),

var defSortHeader = $scope.columns[2];
angular.element('#col-header-' + defSortHeader).click();
// For ascending
// angular.element('#col-header-' + defSortHeader).click().click();

Hopefully that helps for now. It's important to note that it may be necessary to delay this click event if you're rendering an exceptionally long list.

aborchew avatar Mar 16 '16 20:03 aborchew

Hello,

I tried to use ng-attr like ng-attr-ts-default="{{isSortable ? '' : undefined}}"

but it doesn't work because all my column are default sorted... :( I have the same behaviour with ng-attr-ts-criteria

dtrouillet avatar May 11 '17 07:05 dtrouillet

You're setting ng-attr-ts-default to undefined in both conditions of that ternary.

aborchew avatar May 11 '17 12:05 aborchew

This problem has been bugging me recently on a project I'm implmenting, I have put a pull request in with a fix that does work for me using an ng-attr-ts-default= test.

<tr>
 <th ng-repeat="(key, col) IN columns"
       ts-criteria="{{'Col' + key}}"
       ng-attr-ts-default="{{sortColumn = col ? '' : undefined}}">
    {{col}}
 </th>
</tr>

https://github.com/mattiash/angular-tablesort/pull/94

TimIgoe avatar Jun 06 '17 11:06 TimIgoe