jqGrid - rowObject不一致? [英] jqGrid - rowObject inconsistencies?

查看:128
本文介绍了jqGrid - rowObject不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jqgrid rowObject的结果的第一页返回预期数据,但随后返回结果后续页面的不完整数据。为什么?

结果的第一页:
rowObject [3]将等于2

First page of results: rowObject[3] will equal "2"

后续结果页:
rowObject [3]将等于undefined,返回结果的第一页现在也将等于undefined。

Subsequent pages of results: rowObject[3] will equal "undefined" and returning to the first page of results will also now equal "undefined".

更多细节和一些代码:

使用jqGrid,如果要实现自定义格式化程序,可以使用名为rowObject的参数行数据。因此,例如,一行rowObject可能是这样的:

With jqGrid, if you want to implement a custom formatter you use a parameter called rowObject that contains the row data. So for instance, one row of rowObject could be something like:

["18", "133", "Betelguese", "3", "photo.jpg", "", "0", ""]

所以我的自定义格式化程序使用其中一些数据来准备链接,如下所示:

So my custom formatter uses some of this data to prepare a link as follows:

var newval = '<a href="/proj/' + rowObject[3] + '/images/' + imgval + '">' + imgval + '</a>';

这给了我一个网址:

<a href="/proj/3/images/photo.jpg">photo.jpg</a>

到目前为止一切顺利。我的问题是当我进入jqgrid的下一页结果时,我丢失了一些这样的数据并得到:

So far so good. My problem is that when I go to the next page of results in the jqgrid I lose some of this data and get:

<a href="/proj/undefined/images/photo.jpg">photo.jpg</a>

如果我加载显示所有结果的页面一切正常,但是如果我使用分页,只有结果的第一页将具有rowObject [3]的正确值,而后续页面上的每个其他结果都不具有该rowObject值!

If I load the page with all of the results displayed everything works fine, however if I use paging, only the first page of results will have the correct value for rowObject[3] while every other result on subsequent pages will not have that rowObject value!

那么为什么rowObject包含当网格结果的下一页出现时,关于最初加载到网格中的内容以及看似丢失数据的正确数据?

So why is rowObject containing the correct data on what is initially loaded into the grid and seeming to lose that data when the next page of grid results appears?

我在Firebug中看到的一件事我不明白...当页面最初加载时我得到:

One thing I'm seeing in Firebug that I don't understand... when the page initially loads I get:

console.log(rowObject); 
["18", "133", "Betelguese", "3", "photo.jpg", "", "0", ""]

在结果的下一页,事情就像我预期的那样停止工作,我看到了

On the next page of results, where things stop working as I expect, I see

console.log(rowObject);
Object { photo_id="18", site_id="133", more...} 

为什么要改变?第一个结果是json,为什么我现在得到这个对象?

Why the change? The first result is json so why am I now getting this Object?

推荐答案

我想你使用 loadonce :true 选项。这是一个带来许多问题的选项(主要是理解问题)。在jqGrid版本3.7中引入新的本地排序,分页和过滤(搜索)功能之前,本地和远程数据之间的分离是明确的。从jqGrid版本3.7开始, loadonce:true 选项允许您在首次加载时拥有的远程数据与稍后的本地数据之间进行混合。在另一个答案中,已经讨论了密切问题。在加载过程结束时,如果 loadonce:true 选项用法,jqGrid的数据类型将更改为 '本地'。之后很多事情都有所不同。

I suppose you use loadonce:true option. It is one from the option which bring many problem (mostly understanding problems). The separation between local and remote data was clear before the introducing in the version 3.7 of jqGrid the new local sorting, paging and filtering (searching) features. Starting with jqGrid version 3.7 the loadonce:true option allows you to have a mix between the remote data which you have at the first load and the local data which you have later. In another answer the close problem was already discussed. At the end of the loading process in case of the loadonce:true option usage the datatype of jqGrid will be changed to 'local'. After that many things works different.

我建议你使用 jQuery.isArray(rowObject)作为一种快速有效的方法来确定是否应该每个整数索引访问 rowObject rowObject [3] (如果您访问远程数据)或每个命名属性 rowObject.projectId

I suggest you to use jQuery.isArray(rowObject) as a quick and effective way to determine whether you should access rowObject per integer index rowObject[3] (if you access remote data) or per named property rowObject.projectId.

您可以使用 $(#list)来查看整个本地数据.jqGrid('getGridParam','data'),它返回所有本地的数组 rowObject

You can see the whole local data with $("#list").jqGrid('getGridParam','data'), which returns the array of all local rowObject.

如果您需要访问的数据( rowObject [3] 未在某些列的jqGrid中保存,那么您将无法在 rowObject 中看到这些信息。在这种情况下,您可以为数据使用额外的隐藏列,或者在任何<的 loadComplete:function(data){...} 内的第一次加载时保存数据strong>外部对象。你可以测试 $(#list)。jqGrid('getGridParam','datatype')'json'(或'xml'取决于您的服务器数据)如果为true,您可以保存从您需要的服务器返回的所有数据(从第3列开始)在外部对象数组中的数据数组。因此,您可以稍后在自定义格式化程序中访问数据。

If the data which you need to access (rowObject[3]) are not saved in the jqGrid in some column then you will not be able to see the information in the rowObject. In the case you can use an additional hidden column for the data or save the data at the first load inside of loadComplete: function(data) { ... } in any external object. You can test that $("#list").jqGrid('getGridParam','datatype') is 'json' (or 'xml' depend on your server data) and if it true, you can save all data returned from the server which you need (from the 3-th column of the data array) in an external array of object. So you will be able to access the data later inside your custom formatter.

更新:问题已在 jqGrid的免费jqGrid 分支。自定义格式化程序(和 cellattr rowattr )包含 rowObject 出于兼容性原因的参数,但存在其他 rowData 属性,其中已解析的数据保存为命名属性:

UPDATED: The problem is solved in free jqGrid fork of jqGrid. The custom formatter (and cellattr, rowattr) contains still rowObject parameter for compatibility reasons, but there are exist additional rowData properties where the parsed data are saved as named properties:

formatter: function (cellValue, options, rowObject) {
    // either rowObject[1] or rowObject.site_id,
    // but options.rowData.site_id works ALWAYS
}

可以使用

cellattr: function (rowid, cellValue, rowObject, cm, item) {
    // either rowObject[1] or rowObject.site_id,
    // but item.site_id works ALWAYS
}

in cellattr

这篇关于jqGrid - rowObject不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆