如何在智能表中将较小的数字排序为较大的数字?

时间:2018-01-08 02:54:11

标签: javascript angularjs smart-table

以下是我的代码,当用户点击 标题 ID 时 它将按ID号排序 (1→2→3→-11-> 23)
但结果是(1-> 11-> 2-> 23-> 3)
我希望结果是通过较小的数字排序到较大的数字或较大的数字排序到较小的数字.....我不想在单个数字中添加零.....

<div class='container' ng-app='smarttabledemo' ng-controller='smarttabledemo'>
      <h3>Smart-Table</h3>
      <table st-table='data' class='table'>
        <thead>
          <tr>
            <th st-sort='ID'>ID</th>
            <th st-sort='firstName'>First Name</th>
            <th st-sort='lastName'>Last Name</th>
            <th st-sort='phone'>Phone Number</th>
            <th st-sort='hometown'>Hometown</th>
          </tr>
        </thead>
        <tbody>
          <tr st-select-row='row' ng-repeat='row in data'>
          <td>{{row.ID}}</td>
            <td>{{row.firstName}}</td>
            <td>{{row.lastName}}</td>
            <td>{{row.phone}}</td>
            <td>{{row.hometown}}</td>
          </tr>
        </tbody>
      </table>

    </div>

我的json文件

  $scope.data = [
    { ID: '3',firstName: 'Sam', lastName: 'Evans', phone: 'Not Provide', hometown: 'Anytown, ST' },
    { ID: '23',firstName: 'Saul', lastName: 'Evans', phone: '555-555-1234', hometown: 'Anytown, ST' },
    { ID: '1',firstName: 'Charlie', lastName: 'Anders', phone: '555-555-9876', hometown: 'Springfield, ST' },
    { ID: '2',firstName: 'Jessica', lastName: 'Cortez', phone: 'Not Provide', hometown: 'Springfield, ST' },
    { ID: '11',firstName: 'Amy', lastName: 'Wood', phone: '555-555-1348', hometown: 'Metroville, ST' },
  ]

enter image description here

以下是我的小提琴演示,谢谢你 Fiddle

2 个答案:

答案 0 :(得分:4)

问题是你的ID是一个字符串,所以它排序alpha而不是数字。

将所有ID更改为数字,或者如果您不想这样做,请在分配数组后插入:

$scope.data.forEach(o=>o.ID=+o.ID)

https://jsfiddle.net/qwc5s6qt/6/

否则,请更新数据或Angular getter以获取自定义排序功能。

答案 1 :(得分:-4)

您可以使用

$scope.data.sort((a, b) => a.ID - b.ID)
相关问题