分页不起作用,将JsonRest与EnhancedGrid一起使用 [英] Paging is not working, using JsonRest with EnhancedGrid
问题描述
我正在为IBM BusinessSpace创建小部件,我在分页时遇到了一些困难。
数据从数据库(使用restlet)成功返回并显示在网格中。导航也显示在网格下方(下一页,上一页,转到页面,页数等)。
例如,如果我有3页,每页5行,并且要导航到第二页,当我点击页码2时,数据重新加载(看起来像restlet再次被调用),然后第一个这一行也显示5行(第一页显示)。如果我选择任何其他导航选项(下一页,...),同样的事情发生。底线,每次点击都会从我的数据库中获得前5行。
如何解决这个问题的任何线索?
I'm creating widget for IBM BusinessSpace, and I'm having some difficulties with paging. Data are succesfully returned from database (using restlet) and displayed in a grid. Navigation is also displayed below the grid (next page, previous page, go to page, number of pages, etc). If I, for example, have 3 pages, 5 rows per page, and want to navigate to second page, when I click on the page number 2, data reloads (it seems like restlet is called again), and first 5 rows (displayed on the first page) are showed on this one too. If I choose any other navigation option (next page, ...), same thing happeneds. Bottom line, every click results in first 5 rows from my database. Any clue on how to resolve this issue?
以下是关于此的代码:
dojo.require("dojo.data.ObjectStore");
dojo.require("dojox.grid.enhanced.plugins.Pagination");
dojo.require("dojox.grid.EnhancedGrid");
dojo.require("dojo.store.JsonRest");
var restStore = new dojo.store.JsonRest({target:"https://localhost:9443/Application/hello"});
var dataStore = dojo.data.ObjectStore({objectStore: restStore});
dojo.ready(function(){
var grid = new dojox.grid.EnhancedGrid({
store: dataStore, <br>
structure: [
{name:"Value", field:"value", width: "auto"},
{name:"RequestID", field:"requestId", width: "auto"},
{name:"ID", field:"id", width: "auto"},
{name:"Name", field:"name", width: "auto"}
],
columnReordering: true,
clientSort: true,
rowSelector: '20px',
rowsPerPage: 5,
autoHeight: true,
plugins: {
pagination: {
pageSizes: ["5", "10", "15", "All"], // page length menu options
description: true, // display the current position
sizeSwitch: true, // display the page length menu
pageStepper: true, // display the page navigation choices
gotoButton: true, // go to page button
position: "bottom" // position of the pagination bar
}
}
}, "gridDiv");
grid.startup();
});
推荐答案
在网格中使用jsonRest时,滚动没有显示的记录尚未加载,此时,jsonRest请求显示所需的数据,为了这个寻呼机按预期工作,在您的休息实现中,您必须使用dojo创建的头(范围:0-24) ,这个由dojo发送,所以你知道所需数据的偏移量和限制,对于响应,你必须返回一个头(Content-Range:items 0-24 / 66),这个数字告诉dojo从哪里到哪里它应该是显示和总记录的数量。
When using jsonRest in a grid, when you scroll the records not shown are not loaded yet, at this moment jsonRest makes a request for the data needed to show, for this pager to work as expected, in your rest implementation you have to consume a header that dojo creates (Range: 0-24), this one is sent by dojo so you know the offset and limit for the data needed, for the response, you have to return a header (Content-Range: items 0-24/66), the numbers tell dojo from where to where it should be showing and how much total record are.
这是php中的一个例子(假设db和响应是实际对象):
This is an example in php (assuming that db and response are actual objects):
$range = $request->headers->get('Range');
preg_match_all ('/.*?(\d+).*?(\d+)/is', $range, $matches);
$limit = $matches[2][0];
$offset = $matches[1][0];
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT {$limit} OFFSET {$offset}";
$result = $db->execute($sql);
$sql2 = "SELECT FOUND_ROWS()";
$count = $db->execute($sql2);
$response->setContent($result);
$response->headers->set('Content-Range', "items $offset-$limit/{$count}");
在jsonRest 文档有一些信息。
In jsonRest documentation there is some information.
这篇关于分页不起作用,将JsonRest与EnhancedGrid一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!