我正在尝试在jqgrid中使用多选功能来选择多行并将其传递给控制器。我创建了一个按钮,如下所示,但是当我选择行并单击按钮时,未触发单击按钮事件。我猜我正在向函数传递错误的参数。以下是javascript代码......
$("#request").jqGrid('navButtonAdd', '#requestpager',
{ caption: "Add", buttonicon: "ui-icon-info", title: "Add", //position: "first",
onClickButton: function (ids) {
var grid = $("#request");
var rowids = grid.jqGrid('getGridParam', 'selarrrow');
var count = rowids.length;
var rowData, colData;
for (var i = 0; i < count; i++) {
rowData = $("#request").getRowData(rowids[i]);
colData = rowData.Name;
}
jQuery("#request").jqGrid({ url: "/Home/Create/" + colData });
有没有人对我做错了什么有任何建议?
答案 0 :(得分:1)
在jqgrid中实现multiselect并不困难,我将给你一个我在项目中实现的工作示例。
HTML
<table id="grid" cellpadding="0" cellspacing="0"></table>//your grid
<div id="pagerGrid" style="text-align:center;"></div><br />//pager
<div><span><button type="button" id="sendMe" class="send" >Send Me To Controller</button>//button which will take the data of all multiselect rows to controller
<table id="grid" cellpadding="0" cellspacing="0"></table>//your grid
<div id="pagerGrid" style="text-align:center;"></div><br />//pager
<div><span><button type="button" id="sendMe" class="send" >Send Me To Controller</button>//button which will take the data of all multiselect rows to controller
只启用multiselect:true
并编写此javascript函数
$('#sendMe').click(function(){
var selRowIds = $('#grid').jqGrid('getGridParam', 'selarrrow');
if(selRowIds.length>0)
{
for( var i=0;i<selRowIds.length;i++){
var Id=getCellValue(selRowIds[i],'Id');
var Name=getCellValue(selRowIds[i],'Name');
var Company=getCellValue(selRowIds[i],'Company');
$.ajax({
type: 'POST',
url: '@Url.Action("AddMe")',
contentType: 'application/json; charset=utf-8',
data:JSON.stringify({Id: Id,Name:Name,Company:Company}),
dataType: "json",
success:function(){
$('#grid').trigger("reloadGrid");
}
error: function () {
}
});
}
}
});
你的控制器方法会像这样
$('#sendMe').click(function(){
var selRowIds = $('#grid').jqGrid('getGridParam', 'selarrrow');
if(selRowIds.length>0)
{
for( var i=0;i<selRowIds.length;i++){
var Id=getCellValue(selRowIds[i],'Id');
var Name=getCellValue(selRowIds[i],'Name');
var Company=getCellValue(selRowIds[i],'Company');
$.ajax({
type: 'POST',
url: '@Url.Action("AddMe")',
contentType: 'application/json; charset=utf-8',
data:JSON.stringify({Id: Id,Name:Name,Company:Company}),
dataType: "json",
success:function(){
$('#grid').trigger("reloadGrid");
}
error: function () {
}
});
}
}
});
我希望这会有所帮助,这是一个有效的例子......
如果有帮助的话,请将其标记为答案。