智能表st-sort Angular JS - 排序不对象对象

时间:2016-09-17 15:17:08

标签: javascript angularjs sorting tablesorter

我正在调用一个返回对象对象的服务。 userCollections是存储的变量。

{
    1: {
        id: "A1",
        name: "B1"
    },
    2: {
        id: "A1",
        name: "B1"
    },
    3: {
        id: "A1",
        name: "B1"
    },
    4: {
        id: "A1",
        name: "B1"
    },
    5: {
        id: "A1",
        name: "B1"
    },
}

这就是我在表格中显示数据的方式。

<table class="table table-hover" st-table="DisplayCollection" st-safe-src="userCollections">
  <thead class="thead-inverse text-uppercase">
    <tr>
      <th st-sort="id" st-sort-default="true" class="cursor-pointer">Name</th>
      <th st-sort="name" class="cursor-pointer">Email</th>
    </tr>
  </thead>

  <tbody ng-repeat="(cname, cdata) in DisplayCollection">
    <tr class="cursor-pointer" ng-repeat="collection in cdata">
      <td ng-bind="collection.full_name | ellipsis:25:false"></td>
      <td ng-bind="collection.email | ellipsis:25:false"></td>
    </tr>
  </tbody>
</table>

st-sort无效。如何使它对象的对象起作用。

我无法将对象的对象转换为对象数组,因为我需要对象键进行某些操作。

任何方式实现排序?

1 个答案:

答案 0 :(得分:0)

Javascript对象没有订单且无法排序。

将数据映射到数组并将密钥添加为每个项的属性

var data = {
  1: {
    id: "A1",
    name: "B1"
  },
  2: {
    id: "A1",
    name: "B1"
  },
  3: {
    id: "A1",
    name: "B1"
  },
  4: {
    id: "A1",
    name: "B1"
  },
  5: {
    id: "A1",
    name: "B1"
  },
}
var newArray = Object.keys(data).map(function(key) {
  var obj = angular.copy(data[key]);
  obj.key = key;
  return obj;
});
console.log(newArray)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>