我有以下网格:
var gridMyTasks = $('#gridMyTasks').jqGrid({
jsonReader: { root: 'rows', repeatitems: false, id: 'ID' },
datatype: 'json',
colNames: ['Task ID', 'Task Name', 'Project Name', 'Task Stage', 'Doc Type', 'Due Date'],
colModel: [
{ name: 'ShortCode', width: 70, jsonmap: 'ShortCode', sortable: false },
{ name: 'TaskName', width: 200, jsonmap: 'TaskName', formatter: 'fmtTaskName', sortable: false },
{ name: 'ProjName', width: 200, jsonmap: 'ProjName', formatter: 'fmtName', sortable: false },
{ name: 'TaskStage', width: 100, jsonmap: 'TaskStage', sortable: false },
{ name: 'DocType', width: 130, jsonmap: 'DocType', sortable: false },
{ name: 'DueDate', width: 70, jsonmap: 'DueDate', sortable: false }
],
rowNum: 0,
height: 'auto',
autowidth: true,
forceFit: true,
multiselect: false,
caption: '',
altclass: 'zebra',
altRows: true,
hoverrows: false,
gridview: true,
sortable: false,
grouping: true,
groupingView: { groupField: ['ProjName'], groupDataSorted: true }
});
当我的页面加载时,我调用一个 Web 服务来获取前 15 行并将其添加到网格中:
TPM.GetHomepageData(function (results) // AJAX web service to load data
{
gridMyTasks[0].addJSONData({ rows: results.Tasks });
if (results.Tasks.length >= 15) $('#divTaskFooter').show(); // Enable "Show All" link
gridMyTasks.show();
}, null);
这很好用。但是,对于拥有超过15 行数据的用户,我有一个“全部显示...”链接。这将再次调用 Web 服务,但传入一个参数以指示我想要所有行。这连接如下:
var loadGrid = function (limit)
{
TPM.GetMyTasks(limit, curSort, curDir, function (results)
{
grid.clearGridData(true); // should clear the existing rows first?
grid[0].addJSONData({ rows: results }); // *all* rows, not sure new ones
link.html(expanded ? 'Show less...' : 'Show all...');
}, null);
};
moreLink.click(function (event) // When user clicks "Show All", load all the data
{
expanded = !expanded;
loadGrid(expanded ? null : 15);
event.preventDefault();
});
在本例中,results
是一个 18 行的数组,并且这个数据是正确的。然而,发生的事情是原来的 15 行仍然存在,添加了 18 行,然后我总共有 33 行。换句话说,网格没有被首先清除。
如果我注释掉这一addJSONData
行:
grid.clearGridData(true);
//grid[0].addJSONData({ rows: results });
然后,网格将清除,我将看到零行。所以,就好像网格被清除了,然后旧数据像一堆不死僵尸行一样复活,重复的行被钉上去。我一定做错了什么。
更新:为 Oleg 添加 HTTP 流量捕获
初始负载:
POST http://oursite.com/TPM.svc/GetHomepageData HTTP/1.1
Accept: */*
Accept-Language: en-us
Referer: oursite.com
x-requested-with: XMLHttpRequest
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)
Host: oursite.com
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache
Cookie: SMSESSION=Unimportant
回复:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 893
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 25 Jul 2013 16:57:43 GMT
{"d":{ ... A bunch of JSON here ...}}
全部显示 点击:
POST http://oursite.com/TPM.svc/GetMyTasks HTTP/1.1
Accept: */*
Accept-Language: en-us
Referer: oursite.com
x-requested-with: XMLHttpRequest
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)
Host: oursite.com
Content-Length: 42
Connection: Keep-Alive
Pragma: no-cache
Cookie: SMSESSION=Unimportant
{"limit":null,"orderBy":null,"desc":false}
回复:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 1762
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
set-cookie: SMSESSION=...
Set-Cookie: SMSESSION=...
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 25 Jul 2013 17:01:55 GMT
{"d":{ ... A bunch of JSON here ...}}