我用jqGrid加载数据,数据的json格式如下{'id':1000, 'name':'XXX', 'score':99}显示的时候要对id字段进行格式化colNames : ['', '名字', '分数'],
colModel : [
{name : 'id', width: 80, formatter: idFormatter},
{name : 'name', width: 80},
{score : 'score', width : 80}
]
...function idFormatter(cellvalue, optionals, rowData){
return '<input type="button" click="delete(' + rowData.id + ')" value="Delete" />';
}显示正常,没什么问题但我用jqGrid的getRowData方法取出来的数据是这样的{ 'id': '<input type="button" click="delete(1000)" value="Delete" />','name':'XXX', 'score':'99' }可以看出有两个问题
1. id字段变成了我格式化以后的值
2. score字段变成了字符串请问我要怎样才能取回原来的数据?
colModel : [
{name : 'id', width: 80, formatter: idFormatter},
{name : 'name', width: 80},
{score : 'score', width : 80}
]
...function idFormatter(cellvalue, optionals, rowData){
return '<input type="button" click="delete(' + rowData.id + ')" value="Delete" />';
}显示正常,没什么问题但我用jqGrid的getRowData方法取出来的数据是这样的{ 'id': '<input type="button" click="delete(1000)" value="Delete" />','name':'XXX', 'score':'99' }可以看出有两个问题
1. id字段变成了我格式化以后的值
2. score字段变成了字符串请问我要怎样才能取回原来的数据?
1.Do not use this method when you editing the row or cell. This will return the cell content and not the actuall value of the input element.
2.the performance of this method becomes an issue.Do not use this method in the body of “for” and “when”. (When calling this method, it will calculates the row datas one time.)
首先用getGridParam('data')获取jqGrid的数据列表
然后根据rowid到列表去取, getGridParam('data')[rowid - 1]关键是jsonReader{ id: idName }中的idName,在colModel中不能有一个字段名称跟跟idName一样
如果字段列表没有idName字段,则rowid为1, 2, 3...
如果字段列表中有idName字段,则rowid为该字段的值这时候就等循环getGridParam('data')去找出对应的数据了
var gars = jQuery("#list").jqGrid('getGridParam', 'selarrrow');
var rowIds = [];
for ( var rowId in gsrs) {
var row = jQuery('#list').jqGrid(
'getRowData', gsrs[rowId]);
rowIds.push(row.id);
}
try {
for ( var i = 0; i < rowIds.length; ++i) {
deleteEntry('URL', rowIds[i]);
}
} catch (err) {
var msg = "服务器内部错误,请重试。";
alert(msg);
}
getRowData得到的是格式化之後的數據。而且所有的屬性都會變成字符串問題已經解決了,方法在3樓