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
583 views
in Technique[技术] by (71.8m points)

javascript - How to make cell editable dynamically in jqGrid

I am new to jqGrid and I need help with a scenario that I am not able to figure out.

I am able to make a cell un-editable using the following code:

jQuery("#updAssist").jqGrid('setCell',rowid,'precPsProg','','not-editable-cell');

Now I want to make the cell editable again based on some condition.

What class should I use to achieve that?

Is there a 'editable-cell' class that I can use?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should remove 'not-editable-cell' class from the cell (<td> element)

td.removeClass('not-editable-cell');

You should select all cells (<td> element) which you want make editable.

I made the demo which demonstrate how to do this. The most important code fragment from the demo is

var grid = $("#list");
var getColumnIndexByName = function(gr,columnName) {
    var cm = gr.jqGrid('getGridParam','colModel');
    for (var i=0,l=cm.length; i<l; i++) {
        if (cm[i].name===columnName) {
            return i; // return the index
        }
    }
    return -1;
};
var changeEditableByContain = function(gr,colName,text,doNonEditable) {
    var pos=getColumnIndexByName(gr,colName);
    // nth-child need 1-based index so we use (i+1) below
    var cells = $("tbody > tr.jqgrow > td:nth-child("+(pos+1)+")",gr[0]);
    for (var i=0; i<cells.length; i++) {
        var cell = $(cells[i]);
        //var cellText = cell.text();
        var unformatedText = $.unformat(cell,{rowId:cell[0].id,
                                        colModel:gr[0].p.colModel[pos]},pos);
        if (text === unformatedText) { // one can use cell.text() instead of
                                       // unformatedText if needed
            if (doNonEditable) {
                cell.addClass('not-editable-cell');
            } else {
                cell.removeClass('not-editable-cell');
            }
        }
    }
};
grid.jqGrid({
    datatype: "local",
    ...
    cellEdit: true,
    cellsubmit: 'clientArray',
    loadComplete: function() {
        changeEditableByContain(grid,'name','test',true);
    }
});
$("#doEditable").click(function(){
    changeEditableByContain(grid,'name','test',false);
});
$("#doNonEditable").click(function(){
    changeEditableByContain(grid,'name','test',true);
});

In the demo the cells from the 'Client' column having the text "test" will be marked as "non-editable". Later one can make the cells "editable" or "non-editable" be clicking on the corresponding button.


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

...