Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 930
Abhi Avatar asked Feb 23 '11 14:02

Abhi


People also ask

How to enable edit functionality on particular column in jqgrid?

You can also add editable: true property that will enable edit functionality on particular column, if you don’t want show a column in edit modal window, you need to set editable: false against that column. Now i am enabling edit functionality on userId, title and body column, the jQgrid column code will look like below,

How to add row data to a jqgrid using jQuery?

jQuery ("#rowed5").jqGrid ('addRowData', mydata2 [i].id, mydata2 [i]); Now run the application. Click the link "about". It will look like the following figure: Now click the particular cell or row of a particular grid.

How do I edit a cell in a grid?

Tab key is pressed - Selects the right cell. If the Grid is in edit mode, saves the edit cell's value, closes its editor, selects the right cell and opens its editor. Shift+Tab keys are pressed - Selects the left cell. If the Grid is in edit mode, saves the edit cell's value, closes its editor, selects the left cell and opens its editor.

How to use the editor in the grid?

The editor’s value is equal to the cell’s value.. If the user starts typing text, the cell’s content is replaced with the text entered from the user. Left Arrow key is pressed - Selects the left cell, when the Grid is not in edit mode. Otherwise, the key stroke is handled by the editor.


1 Answers

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.

like image 78
Oleg Avatar answered Sep 28 '22 13:09

Oleg