我正在使用ngTables以表格格式加载我的数据并显示分页。我的数据是从Firebase数据库加载的,目前长度为9.所以我想在每页显示三行。
下面是我的代码,当我的页面被加载时,所有九行都被加载到第一页,而我已经将计数设置为3,当我点击页面时,它也不会继续
还有另外一个问题,过滤也不起作用。
以下是HTML视图中的ng-table无法更改并且:
<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>
和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);
}
});
});
}]);
答案 0 :(得分:2)
page
param是额外的,因为默认值是1。filter
是额外的,因为默认情况下它是空的。total
是额外的,因为默认情况下它会自动获取数据集的长度getData
是额外的。您不需要自定义筛选。 ngTable进行过滤我建议更换此代码
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);
}
});
进入这个。
console.log(data.length); // data is loaded here, and the length is 9
$scope.tableParams = new ngTableParams({
count: 3, // count per page
}, {
dataset: data
});
并在HTML中将此ng-repeat="obj in mylist"
更改为此ng-repeat="obj in $data"
答案 1 :(得分:2)
这是工作解决方案:
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()));
}
});
});
}]);
答案 2 :(得分:1)
以下是有关如何使用ngTable
添加分页的简单示例。
显然,请确保添加对angularjs的引用。
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.js"></script>
添加对ngTable
javascript和css。
<link rel="stylesheet"; href="https://unpkg.com/ng-table@2.0.2/bundles/ng-table.min.css">
<script src="https://unpkg.com/ng-table@2.0.2/bundles/ng-table.min.js"></script>
创建ngApp
后,在视图中创建表格。
更新
<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>
在您的控制器上,从$scope.tableParams
移除data.$loaded()
并直接使用$scope.myList
。
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);
}
});
}]);