如何在angularjs中自定义排序功能?

时间:2016-09-21 15:04:37

标签: angularjs

我想在每个列中包含不同数据类型的表头中添加排序功能,所以我做了类似这样的事情:

<thead>
<tr>
    <th ng-click="sortItems('stringCol')">Name</th>
    <th ng-click="sortItems('dateCol')">Date Col</th>
    <th ng-click="sortItems('numCol')">Number Col</th>
    <th ng-click="sortItems('currencyCol')">Currency Col</th>
</tr>
</thead>
<tbody>
    <tr ng-repeat="item in items | orderBy:sortColumn:reverseSort">
        <td>{{item.stringCol}}</td>
        <td>{{item.dateCol}}</td>
        <td>{{item.numCol}}</td>
        <td>{{item.currencyCol}}</td>
    </tr>
</tbody>

这是js:

$scope.sortColumn = "stringCol";
    $scope.reverseSort = false;

    $scope.sortData = function(column){
        $scope.reverseSort = ($scope.sortColumn == column) ? !$scope.reverseSort : false;
        $scope.sortColumn = column;
    };

它将所有数据视为字符串,因此对于非字符串列不能正常工作。此外,每列也可以具有空值。 我是角度js的新手,我知道我需要一个自定义过滤功能,但我不知道怎么做。任何帮助,将不胜感激。

0 个答案:

没有答案