我在我的项目中有一个jqGrid
,我想添加自定义按钮,但没有按钮出现在网格中,据我所见,没有语法问题。我试图简化我的网格,但按钮仍然没有出现。我使用Velocity
模板引擎,这就是为什么我的代码中有$
符号,它并不涉及jQuery。 我正在使用jqGrid 3.8.2。jqGrid navButtonAdd不工作
这是我如何构建网格:
<script type="text/javascript">
function loadCompleteHandler(){
jQuery("#listTable").jqGrid('setGridHeight', Math.min(500,parseInt(jQuery(".ui-jqgrid-btable").css('height'))));
}
function select1() {
return ": ;false:no;true:yes";
}
function select2(){
return ": ; 1:Select1;2:Select2;3:Select3";
}
var colModelData = [
{name:'1', index:'1',width:'5%', hidden: true, align:'center', sortable: false, resizable:false},
{name:'2', index:'2',width:'10%', align:'center', formatter: 'checkbox', sortable: false, editable: true, stype: 'select', edittype: 'checkbox', editoptions: {value: "Yes:No"}, searchoptions: { value: select1()}, resizable:false},
{name:'3', index:'3', width:'35%', sortable: false, resizable:false, defaultValue: ''},
{name:'4', index:'4', width:'30%', sortable: false, resizable:false, defaultValue: ''},
{name:'5', index:'5', width:'20%', sortable: false, defaultValue: '$selectedValue', stype: 'select', searchoptions: {value: select2()}, resizable:false},
]
jQuery(function(){
jQuery("#listTable").jqGrid({
url: '$urlManager.getURL("/myPath/My.json")' + '?param1=param1Value¶m2=param2Value',
datatype: 'json',
postData: {
filters: '{"groupOp":"AND","rules":' +
'[{"field":"field3","op":"eq","data":"$selectedValue"}]}'
},
mtype: 'POST',
colNames:['', '<input type="checkbox" id="selectionCheckbox" onclick="checkboxClick(this)">', 'column3', 'column4', 'column5'],
colModel : colModelData,
width:'768',
height: 500,
pager: '#pagerDiv',
gridview: true,
rowNum: 50,
rowTotal: 500,
sortorder: 'desc',
onSelectRow: function(id){
if (id){
if (id !== lastSel) {
jQuery('#listTable').restoreRow(lastSel);
jQuery('#listTable').editRow(id, true);
lastSel=id;
} else {
jQuery('#listTable').editRow(id, true);
}
}
},
editurl: '$urlManager.getURL("myPath/MyScreen.vm")',
caption: 'Caption',
hidegrid: false,
viewrecords: true,
loadComplete: loadCompleteHandler,
ignoreCase: true,
search: true,
page: $page
});
jQuery(function(){
jQuery("#listTable").jqGrid('filterToolbar',{
stringResult: true,
searchOnEnter: false });
});
jQuery("#listTable").jqGrid('navGrid',"#pagerDiv",{edit:false,add:false,del:false})
.jqGrid('navButtonAdd', '#pagerDiv', {caption: "Edit",
onClickButton:function(){
console.log('Button clicked');
}});
<script type="text/javascript">
<table id = "listTable"> </table>
<div id = "pagerDiv"></div>
在此先感谢。
谢谢奥列格。你对语法错误是正确的,当我复制和重写代码时,我犯了一些错误。主要错误是缺少'});'正如你所提到的那样。再次感谢! – Atticus
@Atticus:不客气!我建议你使用[JSLint](http://www.jslint.com/)。如果您在JSLint中从我的演示中剪切并粘贴JavaScript代码,则不会有错误。如果您不喜欢JSLint使用的限制,则可以在JavaScript代码的开头添加许多选项。 JSLint可以提高代码的质量。许多其他开发人员(如jQuery的开发人员)会永久使用JSLint。 – Oleg
我还有一个问题,如果你仍然遵循这个线程。为什么将jqGrid定义包含在'jQuery(function(){});'中很重要?如果我将它包含在单独的'jQuery(function(){});'中,我会发现以前的解决方案会起作用。 – Atticus