如何对具有多个 ng-repeats angularjs 的嵌套 table 进行排序

How to sort a nested table that has multiple ng-repeats angularjs

我搜索了一个类似的例子,发现丢失了一些例子,但其中 none 与我的 json 数据具有相同的数据结构,所以我试过了都没有成功。这是我的 json 数据

vm.result = [
  {
    'first': [ 
      {"indicate":"all", "text":"All views"},
      {"indicate":"userview", "text":"User view"},
      {"indicate":"operatorview", "text":"Operator view"}
    ]
  },
  {
    'second': [ 
      {"indicate":"receipts","text":"Receipts"},
      {"indicate":"other", "text":"Other"},
      {"indicate":"error", "text":"Error resolver"}
    ]
  }
];

这是我的table

<table class="table" >
  <thead>
    <tr>
      <th ng-show="column">Key</th>
      <th>Master</th>
      <th>Editable</th>
    </tr>
  </thead>
  <tbody ng-repeat="res in vm.result track by $index">
    <tr  ng-repeat="ed in res.first | filter: searchfield | orderBy: 'indicate'  track by $index">
      <td {{ed.indicate}}</td>
      <td>{{ed.text}}</td>
      <td> {{vm.result[1].second[$index].indicate}}</td>
      <td > {{vm.result[1].second[$index].text}}</td>               
    </tr>
  </tbody>
</table>

OrderBy 仅适用于第一个数组,但不适用于第二个,是的,我尝试在第二个 ng-repeat 中的 re 之后删除 first,但它无法将其识别为数组。我需要 table 中的这种结构,以便每个数组都有自己的数据列。那么如何为两个数组添加 orderBy 指示?

预期输出:

Master           Editable
all      |   All      | receipts   | Receipts
userview | User view  | other      |Other

结果数据格式很差。很难转换您的预期输出。 尝试像这样使用结果数组或转换为这种格式,

$scope.newResult   = [{
     first:{"indicate":"all", "text":"All views"},
    second:{"indicate":"receipts","text":"Receipts"}
  },
  {
     first:{"indicate":"userview", "text":"User views"},
    second:{"indicate":"other","text":"Other"}
  },
  {
     first:{"indicate":"operatorview", "text":"Operator views"},
    second:{"indicate":"error","text":"Error resolver"}
  }
]

HTML代码

<table >
   <thead>
    <tr>
     <th>Key</th>
      <th colspan="2">Master</th>
      <th colspan="2">Editable</th>
    </tr>
  </thead>
  <tbody >
 <tr ng-repeat="res in new">
 <td>{{$index+1}}</td>
    <td>{{res.first.indicate}}</td>
    <td>{{res.first.text}}</td>
    <td>{{res.second.indicate}}</td>
    <td>{{res.second.text}}</td>
  </tr>
  </tbody>

  </table>

控制器

 $scope.result = [
  {
    'first': [ 
      {"indicate":"all", "text":"All views"},
      {"indicate":"userview", "text":"User view"},
      {"indicate":"operatorview", "text":"Operator view"}
    ]
  },
  {
    'second': [ 
      {"indicate":"receipts","text":"Receipts"},
      {"indicate":"other", "text":"Other"},
      {"indicate":"error", "text":"Error resolver"}
    ]
  }

];
$scope.new=[];
angular.forEach($scope.result, function(value, key) {
    if(key ===0){
    angular.forEach(value.first, function(val, k) {
     $scope.new.push({'first':val,'second':''});
     })
    }
    if(key ===1){
    angular.forEach(value.second, function(val, k) {
     $scope.new[k].second = val;
     })
    }

});

使用这种机制可以获得预期的输出,但更好的方法是对结果数组使用良好的数据格式。 jsfiddle为此。