jqGrid-内联编辑和补漏白编辑规则
在我的情况下,我需要允许用户编辑网格中的各种单元格,然后稍后将整个网格保存到服务器。通过内联编辑和保存到“clientArray”,我基本上解决了这个问题。但是,我尝试使用editRules,遇到了一些问题 如果我使列可编辑,并使用编辑规则要求它为数字jqGrid-内联编辑和补漏白编辑规则,jqgrid,Jqgrid,在我的情况下,我需要允许用户编辑网格中的各种单元格,然后稍后将整个网格保存到服务器。通过内联编辑和保存到“clientArray”,我基本上解决了这个问题。但是,我尝试使用editRules,遇到了一些问题 如果我使列可编辑,并使用编辑规则要求它为数字 { name: 'Value', index: 'Value', width: 50, sortable: true,edittype: 'text', editable: true, editoptions:
{ name: 'Value', index: 'Value', width: 50, sortable: true,edittype: 'text',
editable: true, editoptions: { maxlength: 10 },
editrules:{number: true},
formatter:currencyFmatter, unformat:unformatCurrency },
我控制onsetrow
事件中的编辑和保存:
onSelectRow: function(id){
if(id && id!==lastSel){
jQuery("#Groups").saveRow(lastSel,true,'clientArray');
jQuery("#Groups").editRow(id,true);
}
lastSel=id
},
然后,我使用按钮单击事件来保存网格。一切都很好,直到我把一个非数字值放入值单元格,然后单击它下面的行。它会弹出警告框并停止保存,但它不会阻止我更改行。因此,我现在有两行打开进行编辑。是否有办法捕获editrule错误,以便在移动到下一行之前处理它。我曾尝试使用saveRow函数(succesfunc、aftersavefunc、errorfunc、afterrestorefunc),其中所有函数都表示数据保存到服务器后将触发,这似乎对“clientArray”不起作用 基本上,当保存到“clientArray”时,我需要找到一种方法来验证内联编辑中的数据,并且非常感谢信息、建议,尤其是示例 谢谢
玩了一段时间后,我决定编辑规则不能很好地与内联编辑配合使用。所以,正如你所建议的,我制定了自己的验证程序。诀窍是找出如何获得编辑行的值 我现在想弄清楚的一件事是如何让焦点回到Value列。回到文档
if(id && id!==lastSel){
//dont save if first click
if (lastSel != -1) {
//get val of Value to check
var chkval = jQuery("#"+lastSel+"_Value").val() ;
// verify it is a number
if (isNaN(chkval)) {//If not a number
//Send Validation message here
//Restore saved row
jQuery("#Grid").restoreRow(lastSel);
//Return to failed save row
jQuery("#Grid ").setSelection(lastSel,false);
//reopen for editing
jQuery("#Grid ").editRow(lastSel,true);
//Note - dont reset lastSel as you want to stay here }
else {
// If number is good, proceed to save and edit next
jQuery("#Grid ").jqGrid('saveRow',lastSel, checksave, 'clientArray', {}, null, myerrorfunc);
jQuery("#Grid ").editRow(id,true);
lastSel=id;
};
isDirty = true;
};
else {
//first click - open row for editing
alert("new Edit")
jQuery("#Grid ").editRow(id,true);
lastSel=id;}
}
为了解决这一问题,您可以编写自己的验证函数,
myRowIsValid
,如下例所示。然后,只需将此函数作为onsetrow
事件处理程序的一部分调用即可:
onSelectRow: function(id){
if(id && id!==lastSel){
if (lastSel != null && !myRowIsValid(lastSel) ) {
// Display validation error somehow
return;
}
jQuery("#Groups").saveRow(lastSel,true,'clientArray');
jQuery("#Groups").editRow(id,true);
}
lastSel=id
},
基本上,如果验证失败,请让用户知道,不要编辑下一行。为了解决这个问题,我使用了插件jquery.limitkeypress.min.js
onSelectRow: function(id){
if(id && id!==lastsel){
jQuery('#treegrid').jqGrid('restoreRow',lastsel);
jQuery('#treegrid').jqGrid('editRow',id, true);
$("input[name=Presupuesto]").limitkeypress({ rexp: /^[+]?\d*\.?\d*$/ });
lastsel=id;
}
}
其中,“Presupuesto”是允许向用户输入数据的列的名称
它工作得非常好…我制作了一个名为edit_list的数组,在回调oneditfunc中,我将rowid添加到列表中,在savefunc之后,我将其从列表中删除
这样,您可以检查回调中的edit_列表,以确定是否有一行正在编辑。OP第一次询问时,此解决方案可能不存在,但我使用最新的jqGrid(4.4.4)解决了此问题 我利用了一个事实,即存储行在成功时将返回true/false 希望这能有所帮助
function StartEditing($grd, id) {
var editparameters = {
"keys": true,
"url": 'clientArray'
};
$grd.jqGrid('editRow', id, editparameters);
$grd[0].LastSel = id;
}
onSelectRow: function(id) {
var $grd = $(this);
if (id && !isNaN($grd[0].LastSel) && id !== $grd[0].LastSel) {
if ($grd.jqGrid('saveRow', $grd[0].LastSel, { "url": 'clientArray' }))
StartEditing($grd, id);
else
$grd.jqGrid('setSelection', $grd[0].LastSel);
}
else
StartEditing($grd, id);
}
若要捕获存储行事件的编辑规则,可以使用内置的返回类型“saveRow”。如果要查找此项,请标记为“answer”
您的代码:
OnSetrow:功能(id){
if(id&&id!==lastSel){
jQuery(“#Groups”).saveRow(lastSel,true,'clientArray');
jQuery(“#Groups”).editRow(id,true);
}
lastSel=id
}
修改代码:
onSelectRow: function(id){
if(id && id!==lastSel){
var bool = jQuery("#Groups").saveRow(lastSel,true,'clientArray');
//bool true -->success,false -->invalid row /req field validations
if(bool)
{
//Returns true on sucessful save of record i.e to grid (when using client array)
//and set current selected row to edit mode
jQuery("#Groups").editRow(id,true);
}
else
{
//Set last selected row in edit mode and add required values
jQuery("#Groups").editRow(lastSel,true);
}
}
lastSel=id
}
谢谢你,贾斯汀。我试图沿着这条路径前进,但不确定如何从行中获取数据。我尝试了jQuery(“#rowid_myCol”).Val(),以获取我的值的值,但这不起作用。如何获取value的值以便测试它--DWOK,实际上需要首先调用saveRow(),以便正确访问行数据。我应该在上面澄清这一点。然后可以使用getRowData()获取该行的数据。这有帮助吗?如果我调用saveRow(),如果editrule失败并且没有保存。我想我很接近。我认为我需要使用jQuery(#lastSel_myCol”).val()。我打错了行。如果我答对了,我会发布。@Justin Ethier:什么是clientArray?如何访问数据clientArray???感谢根据:url:如果定义,此参数将替换选项数组中的editurl参数。如果设置为“clientArray”,则数据不会发布到服务器,而是保存仅保存到网格(可能是为了以后手动保存)。
。因此它不会保存到其他数组或任何东西-如果您想稍后提交更改,则需要跟踪客户端更改。它只是防止网格自动发布更改。