无法从 jqgrid 中删除一行
unable to delete a row from jqgrid
情况: 我正在开发一个 spring mvc webapp,因为我试图为 jqgrid 中的每一行提供一个删除按钮,当用户点击删除按钮执行 javascript 函数,向控制器发送请求,控制器从服务器的数据库中删除该行,然后从 jqgrid 中删除该行,为此,我的 html 如下:
<table id="grid"></table>
<div id="pager"></div>
和javascript如下:
var mydata = [{
name: "Toronto",
country: "Canada",
continent: "North America"
}, {
name: "New York City",
country: "USA",
continent: "North America"
}, {
name: "Silicon Valley",
country: "USA",
continent: "North America"
}, {
name: "Paris",
country: "France",
continent: "Europe"
}]
$("#grid").jqGrid({
data: mydata,
datatype: "local",
colNames: ["Name", "Country", "Continent","action"],
colModel: [{
name: 'name',
index: 'name',
editable: true,
}, {
name: 'country',
index: 'country',
editable: true,
}, {
name: 'continent',
index: 'continent',
editable: true,
},{
name: 'conti',
index: 'cont',
formatter : hello,
editable: true,
}],
pager: '#pager'
});
function hello(cellvalue, options, rowObject)
{
return '<a href="javascript:deleteRow(\'' + rowObject.name
+'/');">delete</a>';
//code to remove this row from jqgrid
}
问题: 现在,当按下删除按钮时,行已从服务器的数据库中删除,但我无法弄清楚如何从 jqgrid 中删除行,我尝试使用 delRowData
但它需要 rowid
而我只有 rowobject
,谁能告诉我如何使用 rowobject
从 jqgrid 中删除相关行?
通常建议在输入数据中包含 id
属性。 id
将用于将 id
属性的值分配给网格的行(<tr>
元素)。
重要的是要知道自定义格式化程序的 options
参数包含可能对您有帮助的属性:options.rowId
和 options.colModel
。您可以将 options.rowId
作为附加参数转发给 deleteRow
函数。
我找到了一个解决方案,我们可以从 options
对象中检索 rowId
,如下所示:
function hello(cellvalue, options, rowObject)
{
return '<a href="javascript:clickFunction(\'' + options.rowId + '\');">' + "DELETE" + '</a>';
}
然后当用户点击超链接时 "Delete" clickFunction()
被执行,因为 delRowData()
接受 rowId
并删除相关行。
function clickFunction(rowid)
{
alert(rowid);
$('#grid').delRowData(rowid);
}
文档: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_formatter
情况: 我正在开发一个 spring mvc webapp,因为我试图为 jqgrid 中的每一行提供一个删除按钮,当用户点击删除按钮执行 javascript 函数,向控制器发送请求,控制器从服务器的数据库中删除该行,然后从 jqgrid 中删除该行,为此,我的 html 如下:
<table id="grid"></table>
<div id="pager"></div>
和javascript如下:
var mydata = [{
name: "Toronto",
country: "Canada",
continent: "North America"
}, {
name: "New York City",
country: "USA",
continent: "North America"
}, {
name: "Silicon Valley",
country: "USA",
continent: "North America"
}, {
name: "Paris",
country: "France",
continent: "Europe"
}]
$("#grid").jqGrid({
data: mydata,
datatype: "local",
colNames: ["Name", "Country", "Continent","action"],
colModel: [{
name: 'name',
index: 'name',
editable: true,
}, {
name: 'country',
index: 'country',
editable: true,
}, {
name: 'continent',
index: 'continent',
editable: true,
},{
name: 'conti',
index: 'cont',
formatter : hello,
editable: true,
}],
pager: '#pager'
});
function hello(cellvalue, options, rowObject)
{
return '<a href="javascript:deleteRow(\'' + rowObject.name
+'/');">delete</a>';
//code to remove this row from jqgrid
}
问题: 现在,当按下删除按钮时,行已从服务器的数据库中删除,但我无法弄清楚如何从 jqgrid 中删除行,我尝试使用 delRowData
但它需要 rowid
而我只有 rowobject
,谁能告诉我如何使用 rowobject
从 jqgrid 中删除相关行?
通常建议在输入数据中包含 id
属性。 id
将用于将 id
属性的值分配给网格的行(<tr>
元素)。
重要的是要知道自定义格式化程序的 options
参数包含可能对您有帮助的属性:options.rowId
和 options.colModel
。您可以将 options.rowId
作为附加参数转发给 deleteRow
函数。
我找到了一个解决方案,我们可以从 options
对象中检索 rowId
,如下所示:
function hello(cellvalue, options, rowObject)
{
return '<a href="javascript:clickFunction(\'' + options.rowId + '\');">' + "DELETE" + '</a>';
}
然后当用户点击超链接时 "Delete" clickFunction()
被执行,因为 delRowData()
接受 rowId
并删除相关行。
function clickFunction(rowid)
{
alert(rowid);
$('#grid').delRowData(rowid);
}
文档: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_formatter