Define Default Sort Programmatically
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>
+1. I just came here to look for this option.
+1
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.
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
You're setting ng-attr-ts-default to undefined in both conditions of that ternary.
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