【发布时间】:2015-10-17 02:28:09
【问题描述】:
我知道如何使用自定义格式化程序并在 jqGrid 中调用 JavaScript 函数。我也知道我可以使用 gridComplete 函数来绑定一个 jQuery 事件。我的问题类似于以下问题——但不一样。
Custom formatter in jqGrid which calls jQuery function
好的,在上面问题中提到的方法中,我们可以对格式化程序返回的元素的点击事件使用 jQuery 函数——但它需要遍历所有行。
在jqGridjqGrid 中是否有更好的方法可以在不循环的情况下将当前行值放入jQuery event handler?
注意:请注意,我需要调用 jQuery event handler 来处理当前行值——而不是简单的 javascript 函数。
我的代码如下。
<script type="text/javascript">
function clickme(rowID) {
alert("hi");
}
$(document).ready(function() {
$("#grid").jqGrid({
url: "GamesList.aspx/GetMyGames",
mtype: 'POST',
postData: {
gameSearch: $('#txtGameName').val(),
ownerSearch: $('#txtOwner').val(),
createdDateFrom: $('#txtCreatedFrom').val(),
createdDateTo: $('#txtCreatedTo').val(),
liquidAmountFrom: $('#txtLmiquidAmountFrom').val(),
liquidAmountTo: $('#txtLmiquidAmountTo').val()
},
datatype: "local", //json if want to load initially
ajaxGridOptions: {
contentType: 'application/json; charset=utf-8'
},
serializeGridData: function(postData) {
return JSON.stringify(postData);
},
jsonReader: {
repeatitems: false,
root: function(obj) {
return obj.d;
}
},
colNames: ['GameID', 'GameName', 'GameOwner', 'PlannedCloseDate', 'CreatedOnDate', 'GameLiquidAmount', 'Join'],
colModel: [{
name: 'GameID',
index: 'GameID'
}, {
name: 'GameName',
index: 'GameName'
}, {
name: 'GameOwner',
index: 'GameOwner'
}, {
name: 'PlannedCloseDate',
index: 'PlannedCloseDate',
formatter: 'date',
formatoptions: {
srcformat: 'm/d/Y',
newformat: 'm/d/Y'
}
}, {
name: 'CreatedOnDate',
index: 'CreatedOnDate',
formatter: 'date',
formatoptions: {
srcformat: 'm/d/Y',
newformat: 'm/d/Y'
}
}, {
name: 'GameLiquidAmount',
index: 'GameLiquidAmount'
}, {
name: 'Join',
index: 'GameID',
width: 30,
formatter: function(cellvalue, options, rowObject) {
return '<span class="ui-icon ui-icon-folder-open app-custom-button-join" title="click to open" onclick="clickme(' + options.rowId + ') ;"></span>';
}
}],
rowNum: 10,
/*rowList: [10, 20, 30],*/
pager: '#pager2',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption: "Games",
gridview: true,
height: "auto",
loadonce: true,
recordtext: "Records {0} - {1} of {2}",
emptyrecords: "No records to view",
loadtext: "Loading...",
pgtext: "Page {0} of {1}",
gridComplete: function() {
//Assign a click event to custom button [after gridComplete]
$(".app-custom-button-join").click(function() {
alert("HELLO");
});
}
});
$("#btnSearch").click(function(e) {
$("#grid").jqGrid('setGridParam', {
datatype: 'json'
}).trigger('reloadGrid');
e.preventDefault();
});
});
</script>
参考资料:
【问题讨论】: