Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

highlight error cell or input when validation fails in jqgrid

I am using jqgrid inline editing with validation in grid using edit rules . i want to add class to highlight errors(eg: ui-state-error) for the input which fails in validation . i can set class to highlight error using this

jQuery('#'+grid_id).jqGrid('setCell',row_id,errfields[a],'','ui-state-error',{color: 'blue'});

But it is not working in jqgrid when inbuilt validation fails . How do i highlight the validation error triggered cell/input .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The demo shows how the probelm can be solved:

enter image description here

In the demo the columns "Amount", "Tax" and "Total" will be validated with the following validation rule:

editrules:{required:true,number:true}

On any validation error the first input field where the validation failed dditional class "ui-state-error" will be added. It is the standard jQuery UI CSS class. Addionally I set focus to the input field.

For the implementation I overwride (chain) the default implementation of the methods $.jgrid.checkValues and $.jgrid.hideModal. Here is the corresponding code:

var grid = $("#list");
grid.jqGrid({
    // define all jqGrid options
});

var originalCheckValues = $.jgrid.checkValues,
    originalHideModal = $.jgrid.hideModal,
    iColWithError = 0;
$.jgrid.checkValues = function(val, valref,g, customobject, nam) {
    var tr,td,
        ret = originalCheckValues.call(this,val, valref,g, customobject, nam);
    if (!ret[0]) {
        tr = g.rows.namedItem(editingRowId);
        if (tr) {
            $(tr).children('td').children('input.editable[type="text"]').removeClass("ui-state-error");
            iColWithError = valref; // save to set later the focus
            //error_td_input_selector = 'tr#'+editingRowId+' > td:nth-child('+(valref+1)+') > input.editable[type="text"]:first';
            td = tr.cells[valref];
            if (td) {
                $(td).find('input.editable[type="text"]').addClass("ui-state-error");
            }
        }
    }
    return ret;
};
$.jgrid.hideModal = function (selector,o) {
    var input, oldOnClose, td,
        tr = grid[0].rows.namedItem(editingRowId);
    if (tr) {
        td = tr.cells[iColWithError];
        if (td) {
            input = $(td).children('input.editable[type="text"]:first');
            if (input.length > 0) {
                oldOnClose = o.onClose;
                o.onClose = function(s) {
                    if ($.isFunction(oldOnClose)) {
                        oldOnClose.call(s);
                    }
                    setTimeout(function(){
                        input.focus();
                    },100);
                };
            }
        }
    }
    originalHideModal.call(this,selector,o);
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...