您好,我正在使用数据类型为“jsonstring”的 jqGrid
现在我想根据第一列中的值更新特定行。 我的意思是
JqGrid定义
$("#grid").empty().jqGrid({
datatype: 'jsonstring',
datastr: { rows: outputStr },
colNames: ['EmpName', 'Val1', 'Val2'],
colModel: [
{ name: 'EmpName', classes: 'EmpName', index: 'EmpName', width: 100, header: 'Currency' },
{ name: 'Val1',classes: 'Val1', index: 'Val1', width: 160 },
{ name: 'Val2', classes: 'Val2',index: 'Val2', width: 50 },
jsonReader: {
repeatitems: false
},
rowNum: 10,
pager: '#pager',
viewrecords: true,
hoverrows: false,
caption: ""
}).navGrid('#pager',
{ search: true, edit: false, add: false, del: false, refresh: false }, {}, {}, {},
{ recreateFilter: true, overlay: true, multipleSearch: true, multipleGroup: true }
);
将我的网格数据视为,
Johny 122 3123 Tom 3123 5856 Susan 124 434 Cetra 242 2111
This is sample data in the grid. Now there is a function which gets updated data, just the data which is updated not the entire grid data. I mean when there is an update for 'Cetra', I receive update for 'Cetra' only
The js function looks like
function getDetailUpdate(key, value)
{
var data = JSON.parse(value);
var row = $('#grid tr:contains(' + key + ')'); //here I get I am getting update data for who, e.g. 'Cetra'
var rowVal = $(row).closest('tr').attr('id'); //Here I get the row id
for (let i in data) {
// Below statement is not working
$("#grid").jqGrid('setRowData', row, { 'Val1': data[1], 'Val2': data[0]});
//Below statement works and updates the changes but it is not updating in proper row cell, I have also set class names for the three td, and which value to update(which cell) I am getting in 'i')
$(row).closest('tr').children('td.' + i).text(data[i]);
}
}
非常感谢任何帮助。谢谢
最佳答案
而不是使用
var row = $('#grid tr:contains(' + key + ')');
我正在使用
var row = $("td").filter(function () {
return $(this).text() == key;
}).closest("tr");
所以这工作正常
按照 @Amete Blessed 的建议,通过查找 tr 的 id 来完成此操作是一种更好的方法。
var rowId = $(row).closest('tr').attr('id');
var rowData = $('#my-jqgrid-table').jqGrid('getRowData', rowId);
rowData.Val1= '12321';
$('#my-jqgrid-table').jqGrid('setRowData', rowId, rowData)
希望对大家有帮助
关于javascript - JqGrid 更新特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37945625/