javascript - 如何在 ng-repeat Angularjs 中对对象的内部字段使用 orderBy

标签 javascript angularjs angularjs-ng-repeat angularjs-orderby

我的对象看起来像这样:

$scope.options = [{
      name: 'John',
      phone: '555-1212',
      age: 10,
      descriptions: [{
        "languageId": "EN",
        "description": "b Some description",

      }, {
        "languageId": "DE",
        "description": "b Some description in dutch",

      }]

    },
    {
      name: 'Jimmy',
      phone: '555-1212',
      age: 10,
      descriptions: [{
        "languageId": "EN",
        "description": " d Some description",

      }, {
        "languageId": "DE",
        "description": "d Some description in dutch",

      }]

    },
    {
      name: 'Cris',
      phone: '555-1212',
      age: 10,
      descriptions: [{
        "languageId": "EN",
        "description": "a Some description",

      }, {
        "languageId": "DE",
        "description": "a Some description in dutch",

      }]

    }]

我想根据“描述”旁边的“描述”字段对其进行排序,

我可以按姓名、电话和年龄等其他字段对此进行排序,但不能按 描述。

 <tr ng-repeat="option in options | orderBy:'name':true">
     <td> {{option.name}}</td>
      <td ng-repeat="desc in option.descriptions |filter:{languageId:'EN'} ">{{desc.description}}</td> 


    </tr>

请建议我一种按“描述”对数据进行排序的方法。

最佳答案

您可以使用ngInit来预过滤给定语言的描述,然后用它来排序和显示:

<tr ng-repeat="option in options | orderBy:'description'" 
    ng-init="option.description = (option.descriptions | filter:{languageId:'EN'})[0].description">
    <td>{{option.name}}</td>
    <td>{{option.description}}</td>
</tr>

演示: http://plnkr.co/edit/eyRYqfeJ6ewaJnGReGTB?p=preview

关于javascript - 如何在 ng-repeat Angularjs 中对对象的内部字段使用 orderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110887/

相关文章:

angularjs - 如何在 Jasmine 测试中注入(inject) Controller 依赖项?

javascript - AngularJS:不同高度的行的虚拟重复

javascript - 为什么过滤器隐藏所有元素?

javascript - 根据选定的下拉值创建 div

javascript - 在 html 属性中指定一个 javascript 方法?

javascript - AngularJS ng-click 表行但不单击某个 td

javascript - ng-repeat 中的 AngularJS "headers"

java - 使用 Ajax 从 Servlet 中的 doGet 获取参数

javascript - for 循环可以递增/递减一个以上吗?

java - 如何将多个对象从 Angular2 发送到 Java Spring 后端