jqgrid添加和编辑时的不同编辑规则

发布于 2024-11-08 22:31:50 字数 175 浏览 0 评论 0原文

我有一个用于编辑用户(权限、名称等)的网格。
我还有一个密码字段,在编辑模式下,该字段是只写的。
含义 - 不显示密码,但如果用户为此字段插入值 - 则密码会更改。
我的问题是,在编辑现有用户时,我显然希望密码字段是可选的。但是当添加新用户时,我想将此字段设为必填。
如何才能实现这一目标?
谢谢

I have a grid which is used for editing users (permissions, name etc.).
I also have a Password field, which, in editing mode, is write-only.
Meaning- the password is not displayed, but if a user inserts a value for this field- then the password is changed.
My issue is that when editing an existing user, I obviously want the password field to be optional. But when adding a new user, I want to make this field required.
How can this be acheived?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那小子欠揍 2024-11-15 22:31:50

我为此使用方法“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}}); 
}

I use method "setColProp" for this

......
{ //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}}); 
}
ㄟ。诗瑗 2024-11-15 22:31:50

对于您的问题,您可以在编辑和添加时使用不同的验证方法。

例子:
<代码>

function validate_add(posdata, obj)
{
   if(posdata.PASSWORD==null || posdata.PASSWORD=="" || posdata.PASSWORD==undefined)
    return [false, "Please enter the pasword"];

返回[真,“”]; )

函数 validate_edit(posdata, obj { //你可以忽略这个,因为你不想验证密码 }

// 在 jqgrid

grid.navGrid('#pager',{add:true,addtext:'添加',edit:true,edittext:'编辑',del:true,deltext:'删除', search:true,searchtext :'查找',refresh:true}, //选项 {width:700,reloadAfterSubmit:true, beforeSubmit:validate_edit}, // 编辑选项 {width:700,reloadAfterSubmit:true, beforeSubmit:validate_add}, // 添加选项 {}, //删除选项 {} //搜索选项 );

for your problem you can use different validation methods when editing and adding.

example:

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 );

深爱不及久伴 2024-11-15 22:31:50

Sandeep 发布了正确的代码,因为 beforeSubmit 可用于自定义验证。

有其他方法可以做你想做的事。人们无法定义不同的editrules< /a>,但可以更改 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;
        }
    }
);

Sandeep posted correct code because beforeSubmit can be used for the custom validation.

There are alternative approach to do what you want. One can't define different editrules, but one can change the value of the editrules objects inside of beforeCheckValues method for example or inside of some other form edit events called before validation check.

Here is the schema of the code which can change the 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;
        }
    }
);
笑忘罢 2024-11-15 22:31:50

我发现了一个有点脏的内联编辑解决方案:

function inlineCustomValidation(value, colname) {

        var savedRow = jQuery("#grid").getGridParam("savedRow");

        if (savedRow[0].some_required_field == "")

            //add operation validation

        } else {

           //edit operation validation

        }
    };

savedRow 数组。这是一个只读属性,在内联和单元格编辑模块中使用,用于在编辑行或单元格之前存储数据。请参阅单元格编辑和内联编辑。

I found a bit dirty solution for inline edit:

function inlineCustomValidation(value, colname) {

        var savedRow = jQuery("#grid").getGridParam("savedRow");

        if (savedRow[0].some_required_field == "")

            //add operation validation

        } else {

           //edit operation validation

        }
    };

savedRow array. This is a readonly property and is used in inline and cell editing modules to store the data, before editing the row or cell. See Cell Editing and Inline Editing.

寄意 2024-11-15 22:31:50

下面的脚本是验证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;
                        }
                    });
                }
            }
        },

The below script is to validate jqgrid inline edit cell, it won't allow user to enter any special characters except dot(.) used to specify decimal separator

{ 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;
                        }
                    });
                }
            }
        },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文