问题描述
我正在使用以下代码在jqGrid中添加行
I am using the below code to add row in jqGrid
已更新
我点击复选框,使用下面的代码查看id's
I click on the checkbox to see id's
by using your code below
$(document).delegate('#list1 .jqgrow td input', 'click', function ()
{
/*var grid = $("#list1 .jqgrow");
var rowid = grid.jqGrid('getGridParam', 'selrow');*/
var mydata = $("#list1").jqGrid('getGridParam','data');
var idToDataIndex = $("#list1").jqGrid('getGridParam','_index');
var id;
for (id in idToDataIndex) {
if (idToDataIndex.hasOwnProperty(id)) {
console.info(id+", "+mydata[idToDataIndex[id]]['cfgName']);
}
}
console.info("maxid "+id);
});
这是我在萤火虫中的输出
Here is my output in firebug
我如何获取jqg1
代替id?可能是造成问题的原因
How i am getting jqg1
in place of id? may be this is creating the problem
function addRow(cfgid,cfgname,hostname,cfgDesc,productId,cfgType,updateDate,emailAddress,absolutePath)
{
var myrow = {cfgid:cfgid, '':'', cfgName:cfgname, hostname:hostname, cfgDesc:cfgDesc, productId:productId,hostname:hostname,cfgType:cfgType,updateDate:updateDate,emailAddress:emailAddress,absolutePath:absolutePath};
$("#list1").addRowData(cfgid, myrow,"first");
$("#list1").trigger("reloadGrid");
$("#list1").sortGrid('updateDate', false, 'desc');
}
updateRow在我使用currentrow
时工作正常,但是如何使用max id在addRow
中添加新行?
updateRow works fine as i used currentrow
but how to use max id for adding a new row in addRow
?
function updateRow(cfgid,cfgname,hostname,cfgDesc,cfgType,updateDate,emailAddress,absolutePath)
{
$("#list1").delRowData( currentrow );
$("#list1").trigger("reloadGrid");
var myrow = {cfgid:cfgid, '':'', cfgName:cfgname, hostname:hostname, cfgDesc:cfgDesc, productId:updateproductid,hostname:hostname,cfgType:cfgType,updateDate:updateDate,emailAddress:emailAddress,absolutePath:absolutePath};
$("#list1").addRowData(currentrow , myrow);
$("#list1").sortGrid('updateDate', false, 'desc');
$("#list1").trigger("reloadGrid");
}
但是似乎在添加行时会得到重复的id
,因为当我尝试选择该行时,会选择2行.
but is seems when the row is added it gets a duplicate id
because when i try to select that row, 2 rows get selected.
我的完整jqGrid代码
My full jqGrid code
var xmlDoc = $.parseXML(xml);
$('#configDiv').empty();
$('<div width="100%">')
.attr('id','configDetailsGrid')
.html('<table id="list1" width="100%"></table>'+
'<div id="gridpager"></div>'+
'</div>')
.appendTo('#configDiv');
var grid = jQuery("#list1");
grid.jqGrid({
datastr : xml,
datatype: 'xmlstring',
colNames:['cfgId','','Name', 'Host', 'Description','Product', 'Type', 'Last Updated Time','Last Updated By',''],
colModel:[
{name:'cfgId',index:'cfgId', width:90, align:"right", hidden:true},
{name:'',index:'', width:15, align:"right",edittype:'checkbox',formatter: "checkbox",editoptions: { value:"True:False"},editable:true,formatoptions: {disabled : false}},
{name:'cfgName',index:'cfgName', width:90, align:"right"},
{name:'hostname',index:'hostname', width:90, align:"right"},
{name:'cfgDesc',index:'cfgDesc', width:90, align:"right"},
{name:'productId',index:'productId', width:60, align:"right"},
{name:'cfgType',index:'cfgType', width:60, align:"right"},
{name:'updateDate',index:'updateDate',sorttype:'Date', width:120, align:"right"},
{name:'emailAddress',index:'emailAddress', width:120, align:"right"},
{name:'absolutePath',index:'absolutePath', width:90, align:"right", hidden:true},
],
pager : '#gridpager',
rowNum:10,
scrollOffset:0,
height: 'auto',
autowidth:true,
viewrecords: true,
gridview: true,
xmlReader: {
root : "list",
row: "com\\.abc\\.db\\.ConfigInfo",
userdata: "userdata",
repeatitems: false
},
onSelectRow: function(id,status){
var rowData = jQuery(this).getRowData(id);
configid = rowData['cfgId'];
configname=rowData['cfgName'];
configdesc=rowData['cfgDesc'];
configenv=rowData['cfgType'];
var ch = jQuery(this).find('#'+id+' input[type=checkbox]').attr('checked');
if(ch) {
jQuery(this).find('#'+id+' input[type=checkbox]').attr('checked',false);
} else {
jQuery(this).find('#'+id+' input[type=checkbox]').attr('checked',true);
}
rowChecked=1;
currentrow=id;
},
onCellSelect: function(rowid, index, contents, event) {
if(index==2)
{
$(xmlDoc).find('list com\\.abc\\.db\\.ConfigInfo').each(function()
{
//alert($(this).find('cfgId').text()+" "+configid);
if($(this).find('cfgId').text()==configid)
{
configname=$(this).find('cfgName').text();
configdesc=$(this).find('cfgDesc').text();
configenv=$(this).find('cfgType').text();
filename=$(this).find('fileName').text();
updatedate=$(this).find('updateDate').text();
absolutepath=$(this).find('absolutePath').text();
productname=productMap[$(this).find('productId').text()];
}
});
}
}
});
grid.jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false});
我要去哪里错了?
推荐答案
您不能只使用与addRow
中的addRowData
的第一个参数始终相同的id="1"
.如果执行此操作,则会在HTML页面上不允许的页面上收到id
重复项.
You can't just use always the same id="1"
as the first parameter of addRowData
in the addRow
. If you do this you will receive the id
duplicates on the page which is not permitted on a HTML page.
cfgId
列在网格中似乎是唯一的.因此,如果页面上只有一个带有cfgId
列的网格,则可以将addRow
中的addRowData
修改为以下内容:
It seems that the cfgId
column are unique in the grid. So if you have only one grid with cfgId
column on your page you can modify the addRowData
in the addRow
to the following:
$("#list1").addRowData(cfgid, myrow,"first");
另一种方法是将$.jgrid.randId()
方法用作addRowData
Another way is to use $.jgrid.randId()
method as the rowid parameter of addRowData
$("#list1").addRowData($.jgrid.randId(), myrow,"first");
或undefined
值:
$("#list1").addRowData(undefined, myrow,"first");
在最后一种情况下,jqGrid将在内部调用$.jgrid.randId()
以生成唯一的rowid.
In the last case the jqGrid will call $.jgrid.randId()
internally to generate the unique rowid.
这篇关于在jqgrid中添加行后的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!