简体   繁体   English

如何有条件地更改angular中的orderBy参数?

[英]How to conditionally change orderBy parameter in angular?

I have a table row with an orderBy parameter. 我有一个带有orderBy参数的表行。 How do I change it to another parameter based on the value returned by a method? 如何根据方法返回的值将其更改为另一个参数?

This is what my tr looks like - 这是我的tr的样子-

<tr class="pl" ng-repeat="thing in things | orderBy:'oldId':reverse" ng-class="{'row-error': component.missing}">
          <td class="drag-handle" sortable-handle><i class="move"></i></td>
          <td class="some-checkbox delete">
            <div class="cb-wrapper">
                ...
            </div>
          </td>
          <td class="some-type">
            <i type-icon="component.type" component-url="component.url"/>
          </td>
        </tr>

Here, based on the value returned by an isFeatureAvailable() method, I would like the orderBy to be either 在这里,基于isFeatureAvailable()方法返回的值,我希望orderBy可以是

orderBy:'oldOrder':reverse"

or 要么

orderBy:'newOrder'

How do I do that? 我怎么做?

'oldOrder' and 'newOrder' can be on a scope variable which changes based on other logic. 'oldOrder'和'newOrder'可以在范围变量上,该变量会根据其他逻辑进行更改。

e.g. $scope.sortCriteria = 'oldOrder';

and then somewhere else: 然后在其他地方:

$scope.changeCriteriaToNew = function(){
    $scope.sortCriteria = 'newOrder';
}

and in html: 并在html中:

ng-repeat="thing in things | orderBy:sortCriteria:reverse"

SethGunnells's way is also another method of doing this. SethGunnells的方法也是这样做的另一种方法。

Something like this should do the trick. 这样的事情应该可以解决问题。

function order() {
  if (isFeatureAvailable() === 'something') return '-oldOrder';
  return 'newOrder';
}

Then just expose that function through your controller and use it in your directive like so: 然后只需通过控制器公开该函数,然后在您的指令中使用它,如下所示:

ng-repeat="thing in things | orderBy: order()"

Here is a live example in JS Bin. 这是JS Bin中的一个实时示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM