如何在jqGrid中动态编辑单元格

时间:2022-03-12 20:04:29

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

我是jqGrid的新手,我需要一些我无法弄清楚的方案的帮助。

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?

我可以使用“可编辑单元”类吗?

1 个解决方案

#1


8  

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

您应该从单元格中删除“not-editable-cell”类(元素)

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.

在演示中,具有文本“test”的“客户”列中的单元格将被标记为“不可编辑”。稍后可以单击相应的按钮使单元格“可编辑”或“不可编辑”。

#1


8  

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

您应该从单元格中删除“not-editable-cell”类(元素)

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.

在演示中,具有文本“test”的“客户”列中的单元格将被标记为“不可编辑”。稍后可以单击相应的按钮使单元格“可编辑”或“不可编辑”。