Javascript 如何在jqGrid中添加更多行?
我有以下表格:Javascript 如何在jqGrid中添加更多行?,javascript,jquery,jqgrid,Javascript,Jquery,Jqgrid,我有以下表格: 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: [
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流量捕获
初始荷载:
答复:
全部显示单击:
答复:
我希望,我正确地理解了你的问题。这个问题在我看来很容易。jqGrid根据默认情况向服务器发送一些参数(
page
,行
,sidx
,sord
等等)。因为您实现了服务器端分页,所以已经使用了这些参数。您写道,您在开头加载前15行。这意味着对服务器的请求包含page=1
和rows=15
。值15
是jqGrid的rowNum
参数的值
要加载所有行,只需将rowNum
参数的值更改为一些较大的值,如10000,然后重新加载网格。相应的代码可以是以下代码
$(“#grid”).jqGrid(“setGridParam”,{rowNum:10000})
.trigger(“reloadGrid”,[{page:1,current:true}]);
有关
reloadGrid
的参数,请参阅。我使用上面的page:1
,以确保在单击“全部显示…”链接之前,不会在寻呼机中更改当前页面。我实际上没有实现任何分页。网格仅支持两种模式:显示前15行或显示所有行。我要寻找的是一种在显示所有行之前清除网格的方法。您能否发布代码,说明如何使用jqGrid以及如何填充它?我认为您使用jqGrid的方式是错误的,您试图解决根本不存在的问题。我添加了一些代码,希望能让事情变得更清楚。@MikeChristensen:如果您使用datatype:'json'
您必须为请求数据指定url
参数。jqGrid以任何方式发出请求。哪些参数支持web服务?您编写了“调用web服务以获取前15行”,但没有发布您使用的Ajax调用。jqGrid有很多选项,允许您发出几乎任何请求并处理任何响应。我确实发布了我使用的Ajax调用:TPM.GetHomepageData(函数(结果)//Ajax web服务加载数据
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 ...}}