Angular ng-table not loading dataset?

独自空忆成欢 提交于 2019-12-14 01:55:56

问题


I'm trying to use the ng-table directive. Having a really hard time just getting the demo code to work properly in my local project.

http://ng-table.com

In my controller, I set up my data.

    var dataset = [
        {name: "Moroni50", age: 50},
        {name: "Moroni49", age: 49},
        {name: "Moroni48", age: 48},
        {name: "Moroni47", age: 47},
        {name: "Moroni46", age: 46},
    ];

    $scope.tableParams = new NgTableParams({}, {dataset: dataset});

When I console.log($scope.tableParams) it seems the object has been created successfully. However the it shows data is an empty array with 0 length.

The render code then just shows the table header with no data.

<table ng-table="tableParams" class="table" show-filter="true">
    <tr ng-repeat="user in $data">
        <td title="'Name'" sortable="'name'">{{ user.name }}</td>
        <td title="'Age'" sortable="'age'">{{ user.age }}</td>
    </tr>
</table>

No idea what's going on, is there something simple I'm missing?


回答1:


I think the documentation may be out of date. I got it working changing "dataset" to "data", like this (using v0.8.3):

$scope.tableParams = new NgTableParams({}, {data: dataset});

You can also add a count (otherwise it just displays one item per page).

$scope.tableParams = new NgTableParams({
    count: 10 
}, {
    data: dataset
});

Working demo: http://plnkr.co/edit/Sbaqgzo6QT3sjxEduRFO?p=preview




回答2:


As Rob said, it looks like the documentation is referencing the latest 1.x beta versions. At 1.0.0-beta.4 they changed settings().data to settings().dataset. So, if you're using an older version use data.

https://github.com/esvit/ng-table/releases?after=1.0.0-beta.5



来源:https://stackoverflow.com/questions/34787456/angular-ng-table-not-loading-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!