admin管理员组文章数量:1356808
I am using ngTables to load my data in table format and show pagination. My data is loaded from Firebase database and currently the length is 9. So I want to show three lines per page.
Here is my code below, when my page is loaded, all nine lines are loaded on the first page whereas I have set the count to 3, and also when I click on the pages, it doesn't move on.
There is another problem with this, the filtering is not working either.
Here is my ng-table in HTML view doesn't change and :
<div ng-controller="mycontroller">
<strong>Filter:</strong> {{tableParams.filter()|json}}
<table ng-table="tableParams" show-filter="true" class="table table-striped">
<tr ng-repeat="obj in mylist">
<td data-title="'Department'" filter="{ 'name': 'text' }">{{ obj.department }}</td>
<td data-title="'Lastname'" >{{ obj.lastname }}</td>
<td data-title="'City'">{{ obj.city }}</td>
</tr>
</table>
</div>
and mycontroller.js
:
app.controller('mycontroller', ["$scope", "$filter", "ngTableParams", "DatabaseRef", "$firebaseArray",
function ($scope, $filter, ngTableParams, DatabaseRef, $firebaseArray) {
//get all data from firebase database
var mydb = DatabaseRef.ref("projects").orderByKey();
$scope.mylist = $firebaseArray(mydb);
var data = $scope.mylist;
data.$loaded().then(function(data) {
console.log(data.length); // data is loaded here, and the length is 9
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 3, // count per page
filter: {
name: '' // initial filter
}
}, {
total: data.length, // length of data
getData: function ($defer, params) {
// use build-in angular filter
var orderedData = params.filter() ? $filter('filter')(data, params.filter()) : data;
$scope.users = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
params.total(orderedData.length);
// set total for recalc pagination
$defer.resolve($scope.users);
}
});
});
}]);
I am using ngTables to load my data in table format and show pagination. My data is loaded from Firebase database and currently the length is 9. So I want to show three lines per page.
Here is my code below, when my page is loaded, all nine lines are loaded on the first page whereas I have set the count to 3, and also when I click on the pages, it doesn't move on.
There is another problem with this, the filtering is not working either.
Here is my ng-table in HTML view doesn't change and :
<div ng-controller="mycontroller">
<strong>Filter:</strong> {{tableParams.filter()|json}}
<table ng-table="tableParams" show-filter="true" class="table table-striped">
<tr ng-repeat="obj in mylist">
<td data-title="'Department'" filter="{ 'name': 'text' }">{{ obj.department }}</td>
<td data-title="'Lastname'" >{{ obj.lastname }}</td>
<td data-title="'City'">{{ obj.city }}</td>
</tr>
</table>
</div>
and mycontroller.js
:
app.controller('mycontroller', ["$scope", "$filter", "ngTableParams", "DatabaseRef", "$firebaseArray",
function ($scope, $filter, ngTableParams, DatabaseRef, $firebaseArray) {
//get all data from firebase database
var mydb = DatabaseRef.ref("projects").orderByKey();
$scope.mylist = $firebaseArray(mydb);
var data = $scope.mylist;
data.$loaded().then(function(data) {
console.log(data.length); // data is loaded here, and the length is 9
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 3, // count per page
filter: {
name: '' // initial filter
}
}, {
total: data.length, // length of data
getData: function ($defer, params) {
// use build-in angular filter
var orderedData = params.filter() ? $filter('filter')(data, params.filter()) : data;
$scope.users = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
params.total(orderedData.length);
// set total for recalc pagination
$defer.resolve($scope.users);
}
});
});
}]);
Share
Improve this question
edited Oct 26, 2016 at 0:53
adjuremods
2,9982 gold badges14 silver badges17 bronze badges
asked Oct 25, 2016 at 22:43
cpluscplus
1,1154 gold badges25 silver badges57 bronze badges
3 Answers
Reset to default 2Here is working solution:
app.controller('mycontroller', ["$scope", "$filter", "ngTableParams", "DatabaseRef", "$firebaseArray",
function ($scope, $filter, ngTableParams, DatabaseRef, $firebaseArray) {
//get all data from firebase database
var mydb = DatabaseRef.ref("projects").orderByKey();
$scope.mylist = $firebaseArray(mydb);
var data = $scope.mylist;
data.$loaded().then(function(data) {
console.log(data.length); // data is loaded here, and the length is 9
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 3, // count per page
filter: {
name: '' // initial filter
},
sorting: { city: "asc" }
}, {
filterSwitch: true,
total: 0, //data.length, // length of data
getData: function ($defer, params) {
// use build-in angular filter
var filteredData = params.filter() ?
$filter('filter')(data, params.filter()) :
$scope.mylist;
var orderedData = params.sorting() ?
$filter('orderBy')(filteredData, params.orderBy()) : filteredData;
params.total($scope.mylist.length);
// set total for recalc pagination
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
});
}]);
page
param is extra because default is anyway 1.filter
is extra because by default it is empty as you have.total
is extra because by default it automatically takes the length of datasetgetData
is extra. You do not need custom filtering. ngTable does its filtering
I suggest replacing this code
console.log(data.length); // data is loaded here, and the length is 9
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 3, // count per page
filter: {
name: '' // initial filter
}
}, {
total: data.length, // length of data
getData: function ($defer, params) {
// use build-in angular filter
var orderedData = params.filter() ? $filter('filter')(data, params.filter()) : data;
$scope.users = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
params.total(orderedData.length);
// set total for recalc pagination
$defer.resolve($scope.users);
}
});
into this.
console.log(data.length); // data is loaded here, and the length is 9
$scope.tableParams = new ngTableParams({
count: 3, // count per page
}, {
dataset: data
});
and in HTML change this ng-repeat="obj in mylist"
into this ng-repeat="obj in $data"
Here's a simple example on how to add pagination using ngTable
.
Obviously, make sure you add reference to angularjs.
<script src="//cdnjs.cloudflare./ajax/libs/angular.js/1.4.2/angular.js"></script>
Add reference to ngTable
javascript and css.
<link rel="stylesheet"; href="https://unpkg./[email protected]/bundles/ng-table.min.css">
<script src="https://unpkg./[email protected]/bundles/ng-table.min.js"></script>
After creating your ngApp
, create your table inside your view.
Update:
<strong>Filter:</strong> {{tableParams.filter()|json}}
<table ng-table="tableParams" show-filter="true" class="table table-striped">
<tr ng-repeat="obj in myList">
<td data-title="'Department'" filter="{ department: 'text' }" sortable="'department'">{{ obj.department }}</td>
<td data-title="'Lastname'" >{{ obj.lastname }}</td>
<td data-title="'City'">{{ obj.city }}</td>
</tr>
</table>
On your controller, remove $scope.tableParams
from data.$loaded()
and use $scope.myList
directly.
app.controller('mycontroller', ["$scope", "$filter", "ngTableParams", "DatabaseRef", "$firebaseArray",
function ($scope, $filter, ngTableParams, DatabaseRef, $firebaseArray) {
//get all data from firebase database
var mydb = DatabaseRef.ref("projects").orderByKey();
$scope.mylist = $firebaseArray(mydb);
$scope.mylist.$loaded().then(function(data) {
console.log(data.length); // data is loaded here, and the length is 9
$scope.arrayLength = data.length;
});
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 3, // count per page
filter: {
department: '' // initial filter
}
}, {
total: $scope.arrayLength, // length of data
getData: function ($defer, params) {
// use build-in angular filter
$scope.mylist = $defer.resolve(params.filter() ? $filter('filter')($scope.mylist, params.filter()) : $scope.mylist);
$scope.users = $scope.mylist.slice((params.page() - 1) * params.count(), params.page() * params.count());
// set total for recalc pagination
params.total($scope.mylist.length);
}
});
}]);
本文标签:
版权声明:本文标题:javascript - AngularJS ngTable the pagination not working, showing all data on one page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744027535a2578306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论