gpt4 book ai didi

ExtJS Grid 呈现的行数多于 pagesize 中指定的行数

转载 作者:行者123 更新时间:2023-12-02 00:10:33 25 4
gpt4 key购买 nike

我根据Sencha给出的例子实现了一个无限网格。基本上,代理使用 jsonp 获取 json 文件并填充网格。我希望网格大小为 10,并且我希望代理仅在用户向下滚动到接近第 10 条记录时才发出后续请求。每条记录在 GridView 中垂直占据足够的空间,不滚动只能看到大约 5 条记录(在我的 14 英寸笔记本电脑上,分辨率为 1366 x 768)。我已将页面大小设置为 10,我的期望是只有当我向下滚动到可见记录之外时网格才会刷新。然而,即使没有滚动,网格也通过发出 5 个单独的请求来加载所有数据(总共 50 条记录)。这是代码:

Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.util.*',
'Ext.grid.plugin.BufferedRenderer'
]);

Ext.onReady(function() {
Ext.define('ContentModel', {
extend : 'Ext.data.Model',
fields : [ 'content_id', 'content' ],
idProperty: 'content_id'
});

var store = Ext.create('Ext.data.Store', {
id : 'store',
model : 'ContentModel',
buffered: true,
pageSize: 10,
proxy : {
type : 'jsonp',
url : 'http://website.com/Top1.json',
reader: {
root: 'contents',
totalProperty: 'totalCount'
}
},
autoLoad : true
});

var grid = Ext.create('Ext.grid.Panel', {
renderTo : Ext.getBody(),
width : '100%',
height : '100%',
title : 'Reader Panel',
id : 'reader',
store : store,
columns : [ {
id : 'content_id',
text : 'Content ID',
dataIndex : 'content_id',
width : '10%'
},
{
id : 'content',
text : 'Content',
dataIndex : 'content',
width : '90%'
} ]
});

Ext.define('JSONP Proxy', {
override: 'Ext.data.proxy.JsonP',
buildUrl: function(request) {
var me = this, url = this.getUrl(request);
console.log(url);
request.url = 'http://website.com/Top' + request.params.page + '.json';
return me.callParent([request]);
}
});

});

json文件的内容是:

Top1.json:

Ext.data.JsonP.callback1({"totalCount":"50", "contents":[{"content_id" : 1, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 2, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 3, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 4, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 5, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 6, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 7, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 8, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 9, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"},{"content_id" : 10, "content" : "<div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br><div> Hello World 1</div><br>"}]});

其他 json 文件遵循相同的格式,具有唯一的内容 ID,并具有正确的回调(callback2、callback3、callback4 和 callback5)。

有人能告诉我为什么所有 5 个 jsonp 请求都会立即触发,而不是等待我滚动吗?

谢谢,巴塔普

最佳答案

感谢大家给我指明了正确的方向。所以,这是最终起作用的:

Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.util.*',
'Ext.grid.plugin.BufferedRenderer'
]);

Ext.onReady(function() {
Ext.define('ContentModel', {
extend : 'Ext.data.Model',
fields : [ 'content_id', 'content' ],
idProperty: 'content_id'
});

var store = Ext.create('Ext.data.Store', {
id : 'store',
model : 'ContentModel',
buffered: true,
pageSize: 10,
trailingBufferZone: 5,
leadingBufferZone: 5,
purgePageCount: 0,
scrollToLoadBuffer: 10,
proxy : {
type : 'jsonp',
url : 'http://website.com/Top' + 0 + '.json',
reader: {
root: 'contents',
totalProperty: 'totalCount'
}
},
autoLoad : true
});

var grid = Ext.create('Ext.grid.Panel', {
renderTo : Ext.getBody(),
width : '100%',
height : '100%',
title : 'Reader Panel',
id : 'reader',
store : store,
plugins : [{
ptype: 'bufferedrenderer',
trailingBufferZone: 5,
leadingBufferZone: 5,
scrollToLoadBuffer: 10,
onViewResize: function(view, width, height, oldWidth, oldHeight) {
// Only process first layout (the boxready event) or height resizes.
if (!oldHeight || height !== oldHeight) {
var me = this,
newViewSize,
scrollRange;

// View has rows, delete the rowHeight property to trigger a recalculation when scrollRange is calculated
if (view.all.getCount()) {
// We need to calculate the table size based upon the new viewport size and current row height
delete me.rowHeight;
}
// If no rows, continue to use the same rowHeight, and the refresh handler will call this again.

// Calculates scroll range. Also calculates rowHeight if we do not have an own rowHeight property.
// That will be the case if the view contains some rows.
scrollRange = me.getScrollHeight();
//newViewSize = Math.ceil(height / me.rowHeight) + me.trailingBufferZone + me.leadingBufferZone;
newViewSize = 18;
me.viewSize = me.setViewSize(newViewSize);
me.stretchView(view, scrollRange);
}
}
}],
columns : [ {
id : 'content_id',
text : 'Content ID',
dataIndex : 'content_id',
width : '10%'
},
{
id : 'content',
text : 'Content',
dataIndex : 'content',
width : '90%'
} ]
});

Ext.define('JSONP Proxy', {
override: 'Ext.data.proxy.JsonP',
buildUrl: function(request) {
var me = this, url = this.getUrl(request);
console.log(url);
request.url = 'http://website.com/Top' + request.params.page + '.json';
return me.callParent([request]);
}
});

});

诀窍是在商店和网格的 bufferedrenderer 插件中正确设置缓冲区设置,并覆盖插件的 onViewResize 方法。 bufferedrenderer 的 ViewSize 属性是根据行高计算的,出于某种原因,它始终是默认值 21,因此导致 ViewSize 为 60,这大于服务器中的记录总数 (50) .这导致立即获取所有行。一旦我将 viewsize 属性覆盖为 18,viewsize 相应地改变了,现在我第一次只获取了 30 条记录。按需加载在滚动时也能完美运行 :)

关于ExtJS Grid 呈现的行数多于 pagesize 中指定的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660870/

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