使用嵌套json时,排序在ngTable中不起作用

时间:2014-10-30 09:24:57

标签: javascript json angularjs sorting ngtable

我使用ngTable在angularjs中创建了一个应用程序,该应用程序工作正常,但排序不起作用。我的json结构是嵌套的,但值正确地与表

一起使用

任何人都可以告诉我一些解决方案吗

我的代码如下所示

JSFiddle

HTML

<div ng-controller="IndexCtrl">
    <table border="1" ng-table="mytable">
         <tbody ng-repeat="peop in peoples">
        <tr ng-repeat="people in peop">
            <td sortable="'id'" data-title="'Id'">{{people.id}}</td>
            <td sortable="'desig'" data-title="'Desig'">{{people.desig}}</td>
            <td sortable="'name'" data-title="'Name'">{{people.name}}</td>
            <td sortable="'place'" data-title="'Place'">{{people.place}}</td>
        </tr>
        </tbody>
    </table>
</div>

脚本

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

app.controller('IndexCtrl', function ($scope, $filter, ngTableParams) {
  $scope.peoples = {
    "ime123": [{"id": 145, 
                "desig": "doctor",
                "name": "Manu",
                "place": "ABCD"
               }],
    "ime148": [{"id": 148,
                "desig": "engineer",
                "name": "John",
                "place": "POLK"
               },
               {
                "id": 150,
                "desig": "scientist",
                "name": "Mary",
                "place": "USE"
               }]
  };    
        $scope.mytable = new ngTableParams({
        sorting: {
            name: 'desc'
        }
    }, {
        getData: function($defer, params) {
        $scope.peoples = $filter('orderBy')( $scope.peoples, params.orderBy());
        $defer.resolve( $scope.peoples);
        }
    });
});

1 个答案:

答案 0 :(得分:2)

ngtable中使用嵌套数组的方式不合适,在你的情况下,你可以再次使数组变暗,并允许指令进行灌注

HTML

<table border="1" ng-table="mytable">
        <tbody ng-repeat="peop in $groups">
            <tr ng-repeat="people in peop.data">
                <td sortable="id" data-title="'Id'">{{people.id}}</td>
                <td sortable="desig" data-title="'Desig'">{{people.desig}}</td>
                <td sortable="name" data-title="'Name'">{{people.name}}</td>
                <td sortable="place" data-title="'Place'">{{people.place}}</td>
            </tr>
        </tbody>
    </table>

位指示

$scope.mytable = new ngTableParams({
        page: 1,            // show first page
        count: 10,          // count per page
        sorting: {
            name: 'desc'
        }
    }, {
        total: peoples.length,
        groupBy:'group',
        getData: function ($defer, params) {
            peoples = $filter('orderBy')(peoples, params.orderBy());
            $defer.resolve(peoples);
        }
    });

数据

var peoples = [{
        "id": 145,
            "desig": "doctor",
            "name": "Manu",
            "place": "ABCD",
            "group": "ime123"  //for grouping
    }, {
        "id": 148,
            "desig": "engineer",
            "name": "John",
            "place": "POLK",
            "group": "ime148" //for grouping
    }, {
        "id": 150,
            "desig": "scientist",
            "name": "Mary",
            "place": "USE",
            "group": "ime148"  //for grouping
    }];

这里几乎正常工作jsfiddle。 默认desc尚未工作(版本0.3.1)