Angular Smart Tables st-sort函数和参数

时间:2016-06-30 22:39:35

标签: angularjs sorting smart-table

我目前正尝试在智能表中使用排序功能。目前我们所拥有的设置是我们传递给智能表的只是一个标识符(这不是真实的例子 - 所以如果这个特定的例子可以被优化则忽略,它的假设是为了得到重点):

[{'id': 1},{'id':4},{'id': 3},{'id':4} }]

现在基于此,我们显示1列中的类型和另一列中的描述。这两个都在不同的对象中查找。我想知道如何使用通用的getter函数进行排序,我可以将对象查找传递给排序,而不是创建几个自定义的getter函数。

现在我必须这样做:

<table>
    <tr>
       <th st-sort="sortByTypeDescription">
       </th>
       <th st-sort="sortByTypeName">
       </th>
    </tr>
</table>

$scope.sortByTypeDescription = function(row) {
      return $scope.descriptions[row.id].description;
}

$scope.sortByTypeName = function(row) {
      return $scope.name[row.id].name;
}

我想知道是否有类似的东西我可以做或者在一个单独的属性中发送额外的参数,所以函数是动态的:

<table>
    <tr>
       <th st-sort="sortColumn(descriptions,description)">
       </th>
       <th st-sort="sortColumn(name,name)">
       </th>
    </tr>
</table>

$scope.sortByTypeName = function(row,a,b) {
      return $scope.[a][row.id][b];
}

2 个答案:

答案 0 :(得分:0)

来自@ charlietfl的评论: 使用getter-example查看smart-tables' doc处的示例。对于您的情况,您可以这样做:

100000

并在html中:

$scope.getters={
    sortByTypeDescription: function (row) {
        return $scope.descriptions[row.id].description;
    }
}

答案 1 :(得分:0)

您可以返回函数:

$scope.sortByTypeName = function(a,b) {
    return function (row) {
        $scope[a][row.id][b];
    }
}
相关问题