smart-table:条件st-sort

时间:2015-10-30 09:58:25

标签: angularjs smart-table

有没有办法在智能表中进行条件st-sort。示例 - 我有一个数组告诉我表中的列是可排序的。假设我的表有[FirstName,LastName,Age,email]列,我的分拣机数组是[firstName,age],这意味着只有firstname和age字段是sortabe。如何使用smart-talbe实现这一目标。任何帮助深表感谢!谢谢!

2 个答案:

答案 0 :(得分:0)

智能表希望您指定列是否可排序的方式是使用s:random_number中的st-sort指令,它告诉哪个列应该按哪个对象属性排序,如此:

<thead>

你所说的是你有一个数组告诉你的表哪个列是可排序的,哪些不是。 在我看来,这是一个非常尴尬的方式来做一般的事情,它可以在ST中完整地完成。

但是,您可以编写一个指令,使用数组中的值有条件地从表中添加或删除<thead> <tr> <th st-sort="firstName">first name</th> <!-- sortable rows --> <th st-sort="lastName">last name</th> <th st-sort="age">age</th> <th>email</th> <!-- not sortable --> </tr> </thead> 。但那只会是一团糟

答案 1 :(得分:0)

我同意这是一种尴尬的方式,但这就是服务如何将数据返回给我们。我尝试了以下代码并使其工作(它对firstname和age列进行了排序。),虽然它对我来说似乎不是一个非常干净的方法 -

在我的HTML中 -

<thead>
    <tr>
        <th st-ratio="20" ng-attr-st-sort="sorters.getSorter('firstName')">first name</th> <!-- sortable rows -->
            <th st-ratio="20" ng-attr-st-sort="sorters.getSorter('lastName')">last name</th>
    <th st-ratio="10" ng-attr-st-sort="sorters.getSorter('age')">age</th>
    <th st-ratio="30" ng-attr-st-sort="sorters.getSorter('email')">email</th>
    <th st-ratio="20" ng-attr-st-sort="sorters.getSorter('balance')">balance</th>
    </tr>
    </thead>  

enter code here

在我的js -

 $scope.sorters = {
                getSorter: function(prop){
                  return function(value){
                    var sortables = ['firstName', 'age'];
                    for(var i=0;i<sortables.length; i++){
                      if(sortables[i] === prop) {
                        return value[prop];
                      };
                    }

                    return undefined;
                  }
                }
              };  

虽然这解决了我的问题,但我不喜欢这种方法。这有更好的选择吗? P.S - 如果我没有更好的选择,我最终会把它转换为指令。

相关问题