jQuery 如何删除jqgrid中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18333994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete rows in jqgrid
提问by JoseLuke
I have just begun using jqGrid, and I want to delete rows using a custom delete button. I am using the code snippet below:
我刚刚开始使用 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);
}
The above code uses this function call to pass the selected row value for deletion
上面的代码使用这个函数调用传递选中的行值进行删除
function deleteRecords(rowData) {
alert(rowData);
}
Unfortunately the rowData value is undefined. How can I use the same structure to delete the rows?
不幸的是,rowData 值未定义。如何使用相同的结构来删除行?
采纳答案by JoseLuke
I found a solution to my own problem.
我找到了解决我自己问题的方法。
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)' />";
}
i just pass this as a parameter to the button onclick event and on the function call this has all the properties i needed from the button but the most important one being the button id which is the interview ids of the row that the button belongs to.
我只是将它作为参数传递给按钮 onclick 事件,在函数调用中,它具有按钮所需的所有属性,但最重要的是按钮 ID,它是按钮所属行的面试 ID。
回答by Waqas Memon
you can delete row using
您可以使用删除行
$('#editDataGridList').jqGrid('delRowData',rowid);
回答by AJ.
The variable cellValue
is not defined in the same scope as your delete formatter is. You could try two things:
该变量cellValue
未在与删除格式化程序相同的范围内定义。你可以尝试两件事:
- Pass the
rowId
argument from your formatter to the delete function instead ofcellValue
. - Declare a variable outside of the scope of BOTH functions and then set that variable to the ID value of the selected row in your
onSelectRow
handler.
- 将
rowId
参数从格式化程序传递给 delete 函数而不是cellValue
. - 声明一个超出 BOTH 函数范围的变量,然后将该变量设置为
onSelectRow
处理程序中所选行的 ID 值。