我有一个带有嵌套数组DataRows的数组源,没有这样的属性名称:

 $scope.arraySource = [
                      ["21", "Leon", "Blue"],
                      ["15", "Marcel", "Red"],
                      ["14", "Jason", "Yellow"],
                      ["25", "Luc", "Green"],
                      ["74", "Cyrile", "Black"],
                      ["45", "John", "Grey"],
                      ["21", "Etiennes", "Green"],
                      ["58", "Mario", "Pink"],
                      ["56", "Sylvain", "Blue"],
                      ["87", "John", "White"]
];


我想在ng-repeat中使用它,并从第一个,第二个或第三个属性中排序该数据。

我想按数据源的索引而不是按propertyName排序

就像这样:

<select ng-model="indexForSort"
        ng-select="idx for idx in [0,1,2]">
</select>

 <table>
     <tr ng-repeat="row in arraySource   |
                orderBy: indexForSort">
       <td>row[0]</td>
       <td>row[1]</td>
       <td>row[2]</td>
     </tr>
    </table>


如果用户在下拉列表中选择0,则会按年龄对表格进行排序,
如果他选择1,它将按名称排序
如果他选择2,它将按颜色排序

你知道怎么做吗?

最佳答案

index.html:

<!DOCTYPE html>
<html ng-app="MyApp">

<head>
  <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">

  <select ng-model="indexForSort" ng-options="idx for idx in [0,1,2]">
  </select>

  <table>
    <tr ng-repeat="row in arraySource | orderBy: ''+indexForSort">
      <td>{{row[0]}}</td>
      <td>{{row[1]}}</td>
      <td>{{row[2]}}</td>
    </tr>
  </table>

</body>

</html>


app.js:

var app = angular.module('MyApp', []);

app.controller('MainCtrl', function($scope) {

  $scope.indexForSort = 0;

  $scope.arraySource = [
    ["21", "Leon", "Blue"],
    ["15", "Marcel", "Red"],
    ["14", "Jason", "Yellow"],
    ["25", "Luc", "Green"],
    ["74", "Cyrile", "Black"],
    ["45", "John", "Grey"],
    ["21", "Etiennes", "Green"],
    ["58", "Mario", "Pink"],
    ["56", "Sylvain", "Blue"],
    ["87", "John", "White"]
  ];
});

关于javascript - ng-repeat数组按数据源的属性索引顺序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39390634/

10-12 00:20
查看更多