jQuery 使用 DataTables 插件的服务器端分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25924546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server side pagination using DataTables plugin
提问by user1234
The server is returning 15 records per page and the total records are over 2000. I'd like to display first 15 records and then on every click of the 'Next' button , display the remaining all records, (15 per page). For this do we do a server side pagination or client side???
服务器每页返回 15 条记录,总记录超过 2000 条。我想显示前 15 条记录,然后在每次单击“下一步”按钮时显示剩余的所有记录(每页 15 条)。为此,我们是在服务器端分页还是在客户端????
Here is my table and the attributes I'm using for pagination in DataTables:
这是我的表格和我在DataTables 中用于分页的属性:
var tableData = self.accountCollection.getData();
var tableColumns = this.accountCollection.getColumns();
var totalRecs = this.accountCollection.length;
//create the UI grid containing the list of items
this.resultsTable = tableEl.dataTable( {
"bServerSide": true,
"sEcho": 3,
"iTotalRecords": totalRecs,
"iTotalDisplayRecords": 15,
"aaData": tableData,
"aoColumns": tableColumns,
"aaSorting": [[1,'asc']],
});
getData: function () {
var returnData = [];
$.each(this.models, function (idx, accountModel) {
returnData.push(accountModel.attributes);
});
return returnData;
},
The returnData
will return me an Object that has fields I will be populating I a table.
在returnData
将返回我有我的字段将填充我的表对象。
Object returned (roughly):
返回的对象(大致):
Object
accountName: "No Company"
address1: "1234 asdf"
address2: ""
billingAcctId: null
billingSystem: null
city: "mountain view"
comments: null
country: "USA"
The getData() function will be then called to return the data from the database using:
然后将调用 getData() 函数以使用以下方法从数据库返回数据:
var tableData = this.accountCollection.getData()
So basically tableData will have the necessary fields and values to display in table. Now I may have more than 1000 records returned from the server. Hence I needed pagination.
所以基本上 tableData 将有必要的字段和值来显示在表中。现在我可能有超过 1000 条记录从服务器返回。因此我需要分页。
The one in fiddle is what I tried and does not have any impact on the paginatin.
小提琴中的那个是我尝试过的,对分页没有任何影响。
I think I have the basic pagination that comes with the DataTables, but now I need to have a server side, to get only 15 records to display at a time, and on click of 'next' and 'prev' button i should be able to make ajax calls to get the remaining records 15 per page.
我想我有 DataTables 附带的基本分页,但现在我需要一个服务器端,一次只显示 15 条记录,点击“下一步”和“上一个”按钮我应该能够进行ajax调用以获取每页15条剩余记录。
I hope this helps you understand better. Please let me know if you need more details.
我希望这可以帮助您更好地理解。如果您需要更多详细信息,请告诉我。
How can I achieve pagination using DataTables?
如何使用 DataTables 实现分页?
Thanks
谢谢
采纳答案by Pranav Labhe
Pagination works total displayed record you need to perform following minimum changes.
分页工作总显示记录您需要执行以下最小更改。
"iTotalDisplayRecord" will be total filtered records
“iTotalDisplayRecord”将是总过滤记录
回答by Jake Zieve
This looks up your alley -> http://datatables.net/examples/data_sources/js_array.htmlIt's purely client-side
这会查找您的小巷-> http://datatables.net/examples/data_sources/js_array.html这纯粹是客户端
Though, as far as I know, the only way to achieve actual pagination (making it faster because you're only fetching 15 records from the database at a time) is by ajax-ing with your server side (i.e. http://datatables.net/examples/data_sources/server_side.html)
虽然,据我所知,实现实际分页的唯一方法(使其更快,因为您一次只从数据库中获取 15 条记录)是通过服务器端的 ajax-ing(即http://datatables .net/examples/data_sources/server_side.html)
It doesn't look to me like you're doing that. Unless... self.accountCollection.getData() is an ajax callback, but in any case when you instantiate the DataTable you should use "ajax: tableData" not "aaData: tableData".
在我看来,你不像这样做。除非... self.accountCollection.getData() 是一个ajax 回调,但在任何情况下,当您实例化DataTable 时,您应该使用“ajax: tableData”而不是“aaData: tableData”。
You may be confusing the JSON that datatables returns, with the datatables API that you use to interact/initialize with the datatable.
您可能会将数据表返回的 JSON 与用于与数据表交互/初始化的数据表 API 混淆。
Sorry, that was a bit much lol does any of that make sense?
对不起,这有点大声笑这有道理吗?