javascript - JqG​​rid 更新特定行

标签 javascript jquery jqgrid

您好,我正在使用数据类型为“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 - JqG​​rid 更新特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37945625/

相关文章:

javascript - 定位一个复选框并使用 Jquery 删除它

javascript - 制作响应式 D3 分层条形图并显示工具提示

javascript - 匹配数组中的 3 个或更多相同元素并将它们添加到列表中

javascript - Jasmine 和 Backbone karma 的麻烦

jquery - jqgrid 重新加载时不更新数据

javascript - 将自定义按钮添加到 jqGrid ColumnChooser 对话框中

javascript - 如何在Jquery中向回调函数传递参数?

javascript - 在for循环中绑定(bind)点击事件

jquery - 无法使用ajax和linq将数据保存到数据库

jquery - 刷新数据后如何重置jqgrid