【问题标题】:use orderBy for value in array使用 orderBy 获取数组中的值
【发布时间】:2016-09-06 11:20:40
【问题描述】:

我嵌套了从服务器提取数据的 ng-repeat:

<div ng-repeat="x in result | filter:sellbox " >
               <div ng-repeat="z in x.data | orderBy:'??' "  >


           {{x.rate_type}} to {{x.name}}  {{z[1]}}
       </div>

        </div> 

这是服务器的一部分:

 "result":[  
      {  
         "id":"BNLN_USD",
         "code":"BNLN",
         "name":"National Bank Of Georgia (BNLN)",
         "currency":"USD",
         "rate_type":"reference",
         "color":"#4b4a4b",
         "legendIndex":1,
         "data":[  
            [  
               1473033600000,
               2.2938
            ]
         ]
      },
      {  
         "id":"BAGA_USD_B",
         "code":"BAGA",
         "name":"Bank Of Georgia  (BAGA)",
         "currency":"USD",
         "rate_type":"buy",
         "color":"#ed7623",
         "dashStyle":"shortdot",
         "legendIndex":2,
         "data":[  
            [  
               1473033600000,
               2.25
            ]
         ]
      }

z[1] 在此段中给出 2.2938 和 2.25 的值。如何使用 orderBy 以使结果按 z[1] 的降序排列。

【问题讨论】:

    标签: angularjs angularjs-ng-repeat angularjs-orderby


    【解决方案1】:

    从 AngularJS 1.3.0-rc.5 开始,如果没有提供其他参数,orderBy 过滤器将使用其项目自动对数组进行排序。

    &lt;div ng-repeat="z in x.data | orderBy"&gt;{{item}}&lt;/div&gt;

    【讨论】:

    • 不是我的情况:)
    • 注意:如果您发现数字未按预期排序,请确保它们实际保存为数字而不是字符串。
    【解决方案2】:

    您不能 orderBy 的原因是您的 data 是一个数组数组,因此您必须调用自定义 orderBy 函数。

    在你的控制器中

    $scope.customOrder = function (content) {
         return content.sort()[0]; 
    }
    

    在您的 HTML 中传递 customOrder 函数并传递 true 以使其降序

    <div ng-repeat="x in result | filter:sellbox " >
      <div ng-repeat="z in x.data | orderBy:customOrder:true">
           {{x.rate_type}} to {{x.name}}  {{z[1]}}
      </div>
    </div>
    

    【讨论】:

    • 你能告诉我这里缺少什么,以便我可以帮忙
    • 它并没有说缺少任何东西。我按照您所说的实现了所有内容,并且控制台记录了一些行以检查 customOrder 是否正常工作(它是)。退货内容是我们需要的吗?
    【解决方案3】:

    我设法通过映射我的结​​果来解决这个问题:

    JS:

    $rootScope.bank_data = $rootScope.result.map(function(x){
    
      return {
          rate_type: x.rate_type,
          name: x.name,
          value :x.data[0][1]
    
             };
    })
    

    HTML:

    <input type="searchbox" ng-model="sellbox" ng-hide="buy"  >
              <div ng-repeat="m in bank_data | filter:sellbox | orderBy:'-value'" > {{m.rate_type}} {{m.name}} {{m.value}}</div>
    

    通过这种方式,我设法显示了我想要排序的数组,并且效果很好。谢谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2019-10-04
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2015-10-23
      相关资源
      最近更新 更多