6

我刚刚开始使用 jqGrid,我想使用自定义删除按钮删除行。我正在使用下面的代码片段:

try {
        var cellValue;
        var id;
        jQuery("#editDataGridList").jqGrid({
            datatype: "local",
            width: 900,
            height: 270,
            colNames: ['Action', 'Interview id', 'Date of observation', 'Name of enumerator'],
            onSelectRow: function (id) {
                debugger;
                var rowData = jQuery(this).getRowData(id);                   
                cellValue = rowData['InterviewId'];
            },
            colModel: [                
                 {
                     name: 'actions', index: 'InterviewId', sortable: false,
                     formatter: function (rowId, cellval, colpos, rwdat, _act) {

                         return "<input type='button' id='btnid' value='delete' class='btn' onClick='deleteRecords(" + cellValue + ");' />";

                     }
                 },
                { name: 'InterviewId', index: 'InterviewId' },
                { name: 'Date', index: 'Date' },
                { name: 'NameOfEnum', index: 'NameOfEnum' }
            ],

            multiselect: false,
            caption: "Edit already entered data"
        });
    }
    catch (e) {
        alert(e.message);
    }

上面的代码使用这个函数调用来传递选中的行值进行删除

function deleteRecords(rowData) {
    alert(rowData);
}

不幸的是,rowData 值未定义。如何使用相同的结构来删除行?

4

3 回答 3

12

您可以使用删除行

$('#editDataGridList').jqGrid('delRowData',rowid);
于 2013-08-20T11:46:07.353 回答
3

我找到了解决我自己问题的方法。

formatter: function (rowId, cellval, colpos, rwdat, _act) {
       var rowInterviewId = colpos.InterviewId.toString();
       return "<input type='button' id='" + rowInterviewId + "' value='delete' class='btn'
       onClick='deleteRecords(this)' />";    
}

我只是将它作为参数传递给按钮 onclick 事件,在函数调用中,它具有我需要的按钮的所有属性,但最重要的是按钮 id,它是按钮所属行的采访 id。

于 2013-08-21T08:16:58.567 回答
2

该变量cellValue未在与您的删除格式化程序相同的范围内定义。你可以尝试两件事:

  1. rowId参数从格式化程序传递给删除函数,而不是cellValue.
  2. 在 BOTH 函数范围之外声明一个变量,然后将该变量设置为onSelectRow处理程序中所选行的 ID 值。
于 2013-08-20T12:33:10.473 回答