我有一个网格,用于编辑用户(权限、名称等)。
我还有一个密码字段,在编辑模式下,它是只读的
含义-不显示密码,但如果用户为此字段插入值,则密码会更改
我的问题是,在编辑现有用户时,我显然希望密码字段是可选的。但是在添加新用户时,我希望将此字段设为必填字段
如何实现这一点
感谢
我对此使用方法"setColProp"
......
{ //Edit dialog options
beforeCheckValues: function(postdata, formid, mode) {
grid.setColProp('Password', {editrules: {required: false}});
},
{ //Add dialog options
beforeCheckValues: function(postdata, formid, mode) {
grid.setColProp('Password', {editrules: {required: true}});
}
对于您的问题,您可以在编辑和添加时使用不同的验证方法。
示例:
function validate_add(posdata, obj)
{
if(posdata.PASSWORD==null || posdata.PASSWORD=="" || posdata.PASSWORD==undefined)
return [false, "Please enter the pasword"];
return [true, ""]; }
function validate_edit(posdata, obj) { //you can ignore this because you dont want to verify password }
// in jqgrid
grid.navGrid('#pager',{add:true,addtext:'Add',edit:true,edittext:'Edit',del:true,deltext:'Del', search:true,searchtext:'Find',refresh:true}, //options {width:700,reloadAfterSubmit:true, beforeSubmit:validate_edit}, // edit options {width:700,reloadAfterSubmit:true, beforeSubmit:validate_add}, // add options {}, //del options {} //search options );
Sandeep发布了正确的代码,因为beforeSubmit
可以用于自定义验证。
有其他方法可以做你想做的事。不能定义不同的编辑规则,但可以在beforeCheckValues方法内部更改editrules
对象的值,例如,在验证检查之前,可以在名为的其他表单编辑事件内部更改。
以下是可以更改editrules
:的代码的模式
var grid = $("#list"),
getColumnIndexByName = function(columnName) {
var cm = grid.jqGrid('getGridParam','colModel'), // grid[0].p.colModel
i=0, l=cm.length;
for (; i<l; i++) {
if (cm[i].name===columnName) {
return i; // return the index
}
}
return -1;
},
addEditrulesPassword={required:true /*some other settings can follow*/},
editEditrulesPassword={required:false /*some other settings can follow*/};
// ... first of all we define the grid
grid.jqGrid({
// all parameters including the definition of the column
// with the name 'Password' inside of `colModel`
});
grid.jqGrid(
'navGrid','#pager',{/*navGrid options*/},
{//Edit dialog options
},
{//Add dialog options
beforeCheckValues:function(postdata,$form,oper) {
// get reference to the item of colModel which correspond
// to the column 'Password' which we want to change
var cm = grid[0].p.colModel[getColumnIndexByName('Password')];
cm.editrules = addEditrulesPassword;
},
onclickSubmit:function(ge,postdata) {
// get reference to the item of colModel which correspond
// to the column 'Password' which we want to change
var cm = grid[0].p.colModel[getColumnIndexByName('Password')];
cm.editrules = editEditrulesPassword;
}
}
);
我发现了一个有点脏的内联编辑解决方案:
function inlineCustomValidation(value, colname) {
var savedRow = jQuery("#grid").getGridParam("savedRow");
if (savedRow[0].some_required_field == "")
//add operation validation
} else {
//edit operation validation
}
};
savedRow数组。这是一个只读属性,用于内联和单元格编辑模块,用于在编辑行或单元格之前存储数据。请参见单元编辑和内联编辑。
下面的脚本是为了验证jqgrid内联编辑单元格,它不允许用户输入任何特殊字符,除了用于指定十进制分隔符的句点(.)
{ name: 'Amount', width: 22, label: 'Cost', editable: true, align: "right", sorttype: 'int', search: false, formatter: 'currency', formatoptions: { prefix: "$", thousandsSeparator: ",", decimalPlaces: 2, defaultValue: '0.00' },
editoptions: {
dataInit: function(element) {
$(element).keypress(function (e) {
if (e.which != 8 && e.which != 0 && e.which != 46 && (e.which < 48 || e.which > 57 )) {
return false;
}
});
}
}
},