JQGrid:未能通过rowId获得位置 [英] JQGrid: Failing to get position with rowId

查看:255
本文介绍了JQGrid:未能通过rowId获得位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid,并且有一段时间在特定位置插入新行.我有rowId,但是当我打电话确定什么位置(行的索引)时,我得到了空值.我正在处理桌子:

var grid = jQuery("#myTable");
grid.jqGrid({
    datatype: "local",
    colNames:['id','Type', 'Name', 'Total','In','Out'],
    colModel:[
            { name: 'id', index: 'id', hidden: true, align:"center"},     
            {name:'type',index:'type', width:10, sortable:true, align:"center"},
            {name:'name',index:'name', width:40, sortable:true, align:"center"},
            {name:'total',index:'total', width: 10, sortable:false, align:"center"},
            {name:'in',index:'in', width:10, sortable:true, align:"center"},
            {name:'out',index:'out', width:10, sortable:true, align:"center"}
    ],
    width: "600",
    height: "900"
  });

但是当我打电话时:

var dataIds = jQuery("#myTable").getDataIDs();

我得到以下信息:

Level 3.xpusdscdw,Level 3.scoach3,Level 3.xpusdscvs,Level 3.xpusdscah,Level 3.xpusdsctotem,Level 3.xpusdscsc

但是当我尝试获取其中任何一个的行索引时,结果将返回null.这就是我想要做的:

var position = jQuery("#myTable").getInd(rowId,false);
alert("Position is: "+position+" for "+rowId);

然后我检查并查看返回的内容(带有警报),看到的是:

Position is: false for Level 03.xpusdscvs

?

有什么作用?当您将false作为第二个参数传递时,getInd应该返回rowId的行索引. 有什么帮助吗?

谢谢

解决方案

我必须首先说明HTML中id的限制.不能是任何字符串.首先,它必须在页面上唯一.接下来,它应该遵循CSS和HTML的标识符要求.在不同版本的CSS和不同版本的HTML中,要求有所不同.例如,在CSS 2.1规范中,您可以阅读以下内容(请参阅这里):

在CSS中,标识符(包括元素名称,类和ID) 选择器)只能包含字符[a-zA-Z0-9]和ISO 10646 字符U + 00A0及更高版本,加上连字符(-)和下划线 (_);他们不能以数字,两个连字符或后面的连字符开头 一个数字.标识符也可以包含转义字符和任何 ISO 10646字符作为数字代码(请参阅下一项).例如, 标识符黑白"?可以写为"B \& W \?"或"B \ 26 W \ 3F".

您可以阅读HTML5规范(请参见此处)

id属性指定其元素的唯一标识符(ID).

该值在元素主目录中的所有ID中必须是唯一的 子树,并且必须至少包含一个字符.该值不能 包含任何空格字符.

此外,如果在jQuery选择器中使用特殊字符(如.或其他元字符),则必须使用两个反斜杠转义该字符:\\.有关更多详细信息,请参见此处.

jqGrid在内部使用方法 $ .jgrid.jqID 转义可能的元字符:

var myElement = $('#' + $.jgrid.jqID(someId));

如果想让他的生活更轻松,我建议您在ID内仅使用字符[A-Za-z],数字[0-9]_-.另外,我建议您不要使用仅区分大小写的ID. HTML在大多数情况下不区分大小写,因此最好使用[a-z][A-Z].另外,您应该(但不是必须)将字母用作id的第一个字符.简单的规则可以节省您将来的时间.

对于您来说,xpusdscdwscoach3xpusdscvsxpusdscahxpusdsctotemxpusdscsc 这样的ID(不含前缀 "Level 3")是完美的.

现在回到您的主要问题.如果在HTML表的变量rowid中有一行id,则可以使用以下代码来获取行索引:

var rowid = "xpusdscdw";
var myRow = $("#" + rowid);
alert("row index: " + myRow[0].rowIndex);

在代码中,我使用了rowid<table><tr>元素的ID的事实.请参见rowIndex 此处的说明.

I an using jqGrid, and I'm having a heck of a time inserting a new row in a specific location. I have the rowId, but when I make a call to determine what the position (the index of the row) I get a null back. I'm working with the table:

var grid = jQuery("#myTable");
grid.jqGrid({
    datatype: "local",
    colNames:['id','Type', 'Name', 'Total','In','Out'],
    colModel:[
            { name: 'id', index: 'id', hidden: true, align:"center"},     
            {name:'type',index:'type', width:10, sortable:true, align:"center"},
            {name:'name',index:'name', width:40, sortable:true, align:"center"},
            {name:'total',index:'total', width: 10, sortable:false, align:"center"},
            {name:'in',index:'in', width:10, sortable:true, align:"center"},
            {name:'out',index:'out', width:10, sortable:true, align:"center"}
    ],
    width: "600",
    height: "900"
  });

But when I call:

var dataIds = jQuery("#myTable").getDataIDs();

I get back the following:

Level 3.xpusdscdw,Level 3.scoach3,Level 3.xpusdscvs,Level 3.xpusdscah,Level 3.xpusdsctotem,Level 3.xpusdscsc

But when I try to get the row index of any of those, the result comes back null. This is what I'm trying to do:

var position = jQuery("#myTable").getInd(rowId,false);
alert("Position is: "+position+" for "+rowId);

And then I check and see what I got back (with the alert), I see this:

Position is: false for Level 03.xpusdscvs

?

What gives? getInd is supposed to return the row index of the rowId when you pass in false as the second parameter. A little help?

Thanks

解决方案

I have to start with remark what restrictions has id in HTML. It can't be any string. First of all it have to be unique on the page. Next it should follow requirements of CSS and HTML to identifiers. The requirements are a little different in different versions of CSS and different versions of HTML. For example in the specification of CSS 2.1 you can read the following (see here):

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

In HTML5 specification you can read (see here)

The id attribute specifies its element's unique identifier (ID).

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

Additionally if you use special characters like . or other meta-characters inside of jQuery selectorsyou must escape the character with two backslashes: \\. See here for more details.

jqGrid uses internally the method $.jgrid.jqID to escape the possible meta-characters:

var myElement = $('#' + $.jgrid.jqID(someId));

If you want to make his live easier I would recommend you to use only characters [A-Za-z], digits [0-9] or _ or - inside of ids. Additionally I would recommend you don't use ids which distinguished only in case. HTML is in most cases case insensitive, so it's better to use either [a-z] or [A-Z]. Additionally you should (but not must) to use the letter as the first character of id. The simple rule can save your time in the future.

In your case the ids like xpusdscdw, scoach3, xpusdscvs, xpusdscah, xpusdsctotem, xpusdscsc without the prefix "Level 3" are perfect.

Now come back to your main question. If you have id of a row in a variable rowid which is inside of the HTML table you can use the following code to get row index if the row:

var rowid = "xpusdscdw";
var myRow = $("#" + rowid);
alert("row index: " + myRow[0].rowIndex);

In the code I use the fact that rowid is the id of <tr> element of a <table>. See description of rowIndex here.

这篇关于JQGrid:未能通过rowId获得位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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