jqGrid中idPrefix的用法 [英] idPrefix usage in jqGrid

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

问题描述

给定一个填充了本地数据并使用idPrefix:"custTable"选项创建的jqGrid,所有生成的行均在html id中获得前缀,即custTableRow_1 custTableRow_2等.是否需要此idPrefixed版本的id传递给jqGrid方法,如果有的话?

Given a jqGrid populated with local data and created with the option of idPrefix:"custTable" , all the generated rows get the prefix in the html id i.e. custTableRow_1 custTableRow_2 etc. Does this idPrefix'ed version of the id need to be passed in to the jqGrid methods, if so which ones?

例如,使用deleteRowData删除行是否需要前缀ID? setRowData或addRowData怎么样?在第x行之后添加时,似乎需要为srcrowid参数添加前缀.多选行怎么样?

for example to delete a row with deleteRowData does it need the prefixed id? how about setRowData or addRowData? when adding after row x it seems to need the prefixed for the srcrowid parameter. How about multiselect rows?

如果我使用行的前缀ID删除行,则该行将从显示中消失,但是当我重新加载网格时,删除项再次出现在网格中,就像未被删除一样.不使用idPrefix时不会发生这种情况.

If I delete a row using the prefixed id of the row it disappears from the display but when I reload the grid the delete item shows up again in the grid, like it wasn't removed. This doesn't happen when idPrefix is not used.

感谢您的帮助.

推荐答案

引入了选项idPrefix来将ID 保存在HTML页面上是唯一的,即使页面上具有诸如rowid之类的ID从服务器加载.典型示例是从服务器加载数据的两个网格.让我们在数据库中有两个表,您可以在其中使用 IDENTITY PRIMARY KEY的定义中 AUTOINCREMENT .在这种情况下,主键将在表中自动生成,并且在表内是唯一的,但在表中 上没有唯一的键.因此,如果您将主键用作网格的ID并放置在一页两个网格上,则ID可以重复.

The option idPrefix was introduced to hold ids on the HTML page unique even you have on the page the ids like the rowids loaded from the server. Typical example is two grids with the data loaded from the server. Let us you have two tables in the database where you use IDENTITY or AUTOINCREMENT in the definition of the PRIMARY KEY. In the case the primary key will be generated automatically in the table and will be unique inside the table, but there are not unique over the tables. So if you would use the primary keys as ids of the grids and place on one page two grids you can have id duplicates.

要解决此问题,可以在第一个网格中使用idPrefix: "a"作为附加选项,在第二个网格中使用idPrefix: "b".在本地情况下,jqGrid将在各处都使用带有前缀的ID,但是如果将ID发送到服务器,则会删除前缀.

To solve the problem you can use idPrefix: "a" as additional option in the first grid and use idPrefix: "b" in the second grid. In the case locally jqGrid will uses everywhere ids with the prefix, but the prefix will be cut if the ids will be sent to the server.

因此,您会在所有回调(事件)和所有方法(例如setRowDataaddRowData等)中本地看到带有前缀 的id,但是在服务器端,id前缀将在发送到服务器之前立即删除.

So you will see locally in all callbacks (events) and in all methods (like setRowData, addRowData etc) the ids with the prefix, but on the server side the ids the prefixes will be removed immediately before sending to the server.

我建议您另外阅读另一个答案,了解我今天发布的ID的限制.

I recommend you additionally to read another answer about the restrictions in the ids which I posted today.

更新:我浏览了您在jsfiddle上编写的代码,并在代码中发现了一些明显的错误.您当前的代码

UPDATED: I looked through the code which you posed on jsfiddle and found some clear bugs in your code. You current code

1)使用错误的算法来生成新行的ID.例如以下代码

1) use wrong algorithm to generate id of the new row. For example the following code

// generic way to create an animal    
function newAnimal(collection, defaults) {
    var next = collection.length + 1;
    var newpet = {
        id : next,
        name: defaults.name + next,
        breed: defaults.breed
    };
    return newpet;
}

使用collection.length + 1作为新ID.如果允许删除项目是错误的.通过添加两个项目,从那里删除一个项目,然后再添加一个新项目一次,以确保ID重复.取而代之的是,使用一些只会递增的变量会更安全.您可以使用 $ .jgrid.randId ()例如,哪个代码非常简单.

use collection.length + 1 for the new id. It's wrong if you allows to delete the items. By adding of two items, deleting one from there and adding new item one more time follows to id duplicates. Instead of that it's more safe to use some variable which will be only incremented. You can use $.jgrid.randId() for example which code is very simple.

2)您调用addRowData,并手动添加前缀(请参见下面的dogsPrefix+newdog.id).这是错误的,因为jqGrid将前缀再一次添加到行中.

2) you call addRowData with adding a prefix manually (see dogsPrefix+newdog.id below). It's wrong because jqGrid adds the prefix one more time to the rows.

// add dog button actions    
$('#dogAddAtEnd').click(function() {
    var newdog = newAnimal(dogs, dogDefaults);
    dogs.push(newdog);
    dogAdded();        
    dogsTable.jqGrid('addRowData', dogsPrefix+newdog.id, newdog);
});

可能还有更多问题,但是至少这些问题可以解释您所描述的问题.

Probably there are more problems, but at least these problems can explain the problems which you described.

更新2 :我检查了您发布的新演示.仍然有台词

UPDATED 2: I examined new demo which you posted. It has still the lines

grid.jqGrid('addRowData', newanimal.id, newanimal,
            "after", prefix+ followingId);

dogsTable.jqGrid('addRowData', dogsPrefix+newdog.id, newdog);

必须固定为

grid.jqGrid('addRowData', newanimal.id, newanimal,
            "after", followingId);

dogsTable.jqGrid('addRowData', newdog.id, newdog);

尽管如此,我在更改后测试了该演示,并发现了addRowDatadelRowDatasetRowData的代码中的错误.问题出在该行 delRowData同一行(setRowData

Nevertheless I tested the demo after the changes and found bugs in code of addRowData, delRowData and setRowData. The problem are in the line of the delRowData and the same line of setRowData

var pos = $t.p._index[rowid];

可以固定为以下内容

var id = $.jgrid.stripPref($t.p.idPrefix, rowid), pos = $t.p._index[id];

addRowData里面,我建议包括这一行

Inside of addRowData I suggest to include the line

var id = rowid; // pure id without prefix

该行

rowid  = t.p.idPrefix + rowid;

addRowData

. 另一条拖曳线 addRowData

lcdata[t.p.localReader.id] = rowid;
t.p._index[rowid] = t.p.data.length;

应更改为

lcdata[t.p.localReader.id] = id;
t.p._index[id] = t.p.data.length;

将使用未添加前缀的ID.

where unprefixed id will be used.

您演示的修改后的代码使用了

The modified code of you demo which uses the fixed version of jquery.jqGrid.src.js you can test here.

我稍后将我的错误报告发布到 trirand 通知jqGrid的开发人员.我希望不久后,该错误修复程序将包含在jqGrid的主要代码中.

I will post my bug report to trirand later to inform the developer of the jqGrid. I hope that soon the bug fix will be included in the main code of jqGrid.

此外,我建议您使用$.jgrid.stripPref方法从行ID中删除前缀.例如函数

Additionally I recommend you to use $.jgrid.stripPref method to strip prefixes from the rowids. For example the function

//general delete selected 
function deleteSelectedAnimal(list, grid, prefix)
{
    var sel = grid.jqGrid('getGridParam', 'selrow');
    if (sel.length)
    {
        var gridrow = sel;  

        //get the unprefixed model id
        var modelid = gridrow; 
        if (prefix.length !== 0)
        {
           modelid = modelid.split(prefix)[1];     
        }                
        // make it a numeric
        modelid = Number(modelid);

        //delete the row in the collection  
        list = RemoveAnimal(list, modelid);

        //delete the row in the grid
        grid.jqGrid('delRowData', gridrow);
    }
}

来自您的演示可以重写为以下内容

from your demo can be rewritten to the following

//general delete selected
function deleteSelectedAnimal(list, grid)
{
    var sel = grid.jqGrid('getGridParam', 'selrow'),
        gridPrefix = grid.jqGrid('getGridParam', 'idPrefix');
    if (sel !== null)
    {
        //delete the row in the collection
        // ??? the gogs list will be not modified in the way !!!
        list = RemoveAnimal(list, $.jgrid.stripPref(gridPrefix, sel));

        //delete the row in the grid
        grid.jqGrid('delRowData', sel);
    }
}

我不确定行list = RemoveAnimal(list, $.jgrid.stripPref(gridPrefix, sel));或函数RemoveAnimal是否能满足您的要求,但是与jqGrid连接起来并不是问题.

I am not sure that the line list = RemoveAnimal(list, $.jgrid.stripPref(gridPrefix, sel)); or the function RemoveAnimal do what you want, but it's not a problem which connected with jqGrid.

关于您的代码的另一句话.您已经在将id属性添加到网格的对象中使用了.它与 localReader.id localReader.id .在这种情况下,来自id属性的数据将用作网格行(<tr>)的id属性.本地data参数会将id另外保存到其他属性,这些其他属性是从colModel项的name属性构建的.所以我认为没有必要定义隐藏列

One more small remark about your code. You use already in the objects which you add to the grid the id property. It's the same name as defined in the localReader.id. In the case the data from the id property will be used as id attribute of the grid rows (<tr>). The local data parameter will save the id additionally to other properties which are build from the name property of the items of colModel. So I see no sense to define hidden column

{ key: true, name: 'id', align: 'left', hidden: true }

您如何在演示完全正常和以前一样,如果您从使用的网格中删除 id列.

How you can see on the demo all stay works exactly as before if you remove id column from the grids which you use.

更新3 :如我所愿,我发布了相应的错误报告

UPDATED 3: As promised before I posted the corresponding bug report here.

这篇关于jqGrid中idPrefix的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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