gpt4 book ai didi

javascript - 在服务器上而不是在客户端对主干分页器结果进行排序

转载 作者:行者123 更新时间:2023-12-01 05:22:46 25 4
gpt4 key购买 nike

我使用https://github.com/backbone-paginator/backbone.paginator在其列可排序的表中显示数据。但是,当单击列的任何标题时,排序是在客户端完成的,而不是执行应包含用于对结果进行排序的属性(例如名称)的新服务器请求。

基类

module.exports = Backbone.PageableCollection.extend({
initialize: function (items, options) {
options || (options = {});
this.url = options.url || "/";
},
state: {
pageSize: 15,
firstPage: 0,
currentPage: 0
},
queryParams: {
sortKey: 'sort',
pageSize: 'size',
currentPage: 'page'
},

parseState: function (resp) {
return {totalRecords: resp && resp.length > 0 ? resp[0]['total_entries'] : 0};
},
parseRecords: function (resp) {
return resp && resp.length > 0 ? resp[1] : [];
},

model: Backbone.NestedModel

});

实例化示例

collections.myTasks = new collections.PagingCollection([], {
model: models.SyncModel.extend({
url: URLs.TASKS
}),
url: URLs.MY_TASKS,
state: {
pageSize: 30,
firstPage: 0,
currentPage: 0,
}
});

columns: [
{
name: "dueDate",
label: "Due Date",
cell: "date",
filterCell: FilterCell,
editable: false,
width: "80px"
},
{
name: "reminder",
label: "Reminder",
filterCell: FilterCell,
cell: Backgrid.StringCell.extend({
formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
fromRaw: function (rawValue, model) {
return DateHelper.format(
IntervalHelper.calculateBefore(model.attributes['dueDate'], rawValue)
);
}
})
}),
editable: false,
width: "80px"
},
{
name: "name",
label: "Subject",
cell: "string",
filterCell: FilterCell,
editable: false,
width: "auto"
},
{
name: "taskStatusCtlg.taskStatus",
label: "State",
filterCell: SelectFilterCell.extend({
filterField: 'taskStatus',
addAllOption: true
}),
cell: "string",
width: "75px"
},
{
name: "assignedTo.alfrescoUserName",
label: "Assigned To",
cell: "string",
filterCell: SelectFilterCell.extend({
filterField: 'assignee',
addAllOption: true
}),
editable: false,
width: "120px"
},
{
name: "taskTypeCtlg.taskType",
label: "Type",
cell: "string",
filterCell: SelectFilterCell.extend({
filterField: 'taskType',
addAllOption: true
}),
editable: false,
width: "70px"
},
{
name: "mainDocument.name",
label: "Case / Document",
link: "mainDocument.id",
cell: LinkCell,
filterCell: FilterCell,
editable: false,
width: '160px'
}
],

获取数据等顺利完成。但是,当单击插入符号时,排序是在客户端完成的。但我需要在单击列标题(在服务器上排序)时将属性“排序”和“顺序”附加到请求 URL。

当前请求:

http://localhost/tasks/user?page=0&size=30 

需要的请求:

http://localhost/tasks/user?page=0&size=30&sort=name&order=asc 

最佳答案

Paginator 提供 3 种获取和排序模式:

  • 客户端:全部在客户端。自己输入数据。
  • 服务器:从 API 获取数据(例如:collection.getFirstPage())并接收总页数。
  • 无限:类似于服务器模式,但最好用于未知数量的页面。就像来自外部 API 的搜索结果。

确保您使用 PageableCollectionmode 属性的 server 值。

var books = new Books([
{ name: "A Tale of Two Cities" },
{ name: "Lord of the Rings" },
// ...
], {
// Paginate and sort on the server side, this is the default.
mode: "server",
});

或者在你的集合类定义中:

module.exports = Backbone.PageableCollection.extend({
mode: "server", // this is the default
initialize: /*...*/

除此之外,这很可能可以在 Backgrid 中解决。

Backgrid默认是在客户端排序。对于自定义行为,您可以

使用Backbone.PageableCollection

设置collection property of the Grid .

var taskGrid = new Backgrid.Grid({
collection: collections.myTasks,
columns: [/*...*/],
});

覆盖HeaderCell View

You are allow to use a different header cell class on columns. There is no restriction on what a header cell must do. In fact, any Backbone.View class can be used. However, if you wish to modify how the sorter behaves, you must implement the sorting protocol. See the JSDoc for details.

var SelectAllHeaderCell = Backgrid.HeaderCell.extend({
// Implement your "select all" logic here
});

var grid = new Backgrid.Grid({

columns: [{
name: "selected",
label: "",
sortable: false,
cell: "boolean",
headerCell: SelectAllHeaderCell
}],

collection: col
});

注意(2017/01/30):链接到 API documentation文档中的内容不是最新的,这正在 issue #664 中讨论.

关于javascript - 在服务器上而不是在客户端对主干分页器结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41944745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com