开发者

jqgrid different editrules for when adding and editing

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-开发者_如何学Go 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


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


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


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


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.


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;
                        }
                    });
                }
            }
        },
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜