2

In jqGrid, I'm working in inline-edit mode.

When the user try to edit a row(click on the pen action icon) I want to prevent({editable: false}) the editing of a specific editable row's cell based on another cell's content in this row.

grid.setColProp('myColumn',{editable:false}); is not good for me because this will disable the editing of 'myColumn' in all the grids's rows and I want to apply it only on the currently edited row.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • Look at this old answer: [How to make cell editable dynamically in jqGrid][1] Maybe it'll be helpful [1]: http://stackoverflow.com/questions/5092571/how-to-make-cell-editable-dynamically-in-jqgrid – Fseee Oct 18 '12 at 13:32
  • @Franky this answer is relevant for `cellEdit: true` mode only. I'm working in `inline-edit` mode as I mentioned above. – Yair Nevet Oct 18 '12 at 13:36

1 Answers1

11

The value of the property editable is common for all rows, but the value will be used only by editRow method which initialize inline editing. So you can change the value of editable property dynamically with respect of setColProp (like in the answer). It's important that you set the correct value of the editable property before every call of editRow. In the answer you can see corresponding code example and the demo.

UPDATED: If you use formatter: "actions" then you can "subclass" the $.fn.fmatter.rowactions called in onclick handler. Below you can see an example of the corresponding code

var orgRowActions = $.fn.fmatter.rowactions;
$.fn.fmatter.rowactions = function (rid, gid, act, pos) {
    var $grid = $("#" + $.jgrid.jqID(gid)),
        rowData = $grid.jqGrid("getLocalRow", rid),
        isNonEditable = false,
        result;
    // we can test any condition and change
    // editable property of any column
    if (act === "edit" && parseFloat(String(rowData.tax)) <= 20) {
        $grid.jqGrid("setColProp", "note", {editable: false});
        isNonEditable = true;
    }
    result = orgRowActions.call(this, rid, gid, act, pos);
    if (isNonEditable) {
        // reset the setting to original state
        $grid.jqGrid("setColProp", "note", {editable: true});
    }
    return result;
}

The corresponding demo you will find here. The "note" column is editable in the demo only if the value from the "tax" column is <= 20:

enter image description here

If you would have datatype: "json" or datatype: "xml" without usage of loadonce: true you should replace call of getLocalRow to the call of getRowData or getCell in the above code.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • the responsibly of calling the `editRow` command is not in my hands, I'm working with the `Actions` formatter (save, edit, delete, cancel). Any way to interfere before & after the `editRow` invocation with some events overriding? – Yair Nevet Oct 21 '12 at 07:12
  • @YairNevet: I added to my answer the "actions" specific implementation of the solution. – Oleg Oct 21 '12 at 10:30
  • doing the job properly! Thank you very much indeed. – Yair Nevet Oct 21 '12 at 11:22
  • @Oleg Since $.fn.fmatter.rowactions no longer uses four arguments and now just uses the action argument in newer versions of jqGrid. Is there a way to do this same specific row cell editing in newer versions of jqGrid? I am working with version 4.5.2 – Snipe656 Jan 07 '14 at 16:52
  • @Snipe656: I am not sure which problem exactly you have in the implementation of "subclassing" in jqGrid 4.5.2. The replacement method `$.fn.fmatter.rowactions` should have the same number of parameters as the original. `result = orgRowActions.call(this, ...);` should use the parameters. To get information like `rowid` you can use `this`. See first lines of old implementation [here](https://github.com/tonytomov/jqGrid/blob/v4.5.2/js/jquery.fmatter.js#L256-L258). – Oleg Jan 07 '14 at 17:43
  • @Oleg The issue I am running into is in 4.5.2 $.fn.fmatter.rowactions only has argument. Line 5471 of jquery.jqGrid.src.js shows the function only is fed the "act" argument and in my tests that is just the action. So I am having a hard time figuring out how to apply the solution from above when it seems like $.fn.fmatter.rowactions is not fed a row ID or any information specific to the grid. It looks to me that your suggested rewrite of the function from below was applied to the core files: https://github.com/OlegKi/jqGrid/commit/e23c95e281b2eea52f4e1aa36d73b5f83cc91dec – Snipe656 Jan 07 '14 at 18:09
  • @Oleg I guess another way I could word my comment is if I download the source from your example above that is found at http://www.ok-soft-gmbh.com/jqGrid/SubclassActions.htm and if i change the jqGrid version to 4.5.2 the solution no longer makes the Note column non editable. When I try to debug why that is, it appears to me it no longer works because the $.fn.fmatter.rowactions no longer is using multiple arguments but instead just the action value. – Snipe656 Jan 07 '14 at 18:18
  • @Oleg Thanks, I figured out what I was doing wrong after re-reading your comments and re-evaluating my own code. I am going to update my stackoverflow question on this so it contains the solution. – Snipe656 Jan 07 '14 at 18:55
  • @Snipe656: OK! I'm glad to read that the problem is solved now. You are welcome! – Oleg Jan 07 '14 at 19:22
  • please somebody tell me that where to insert this code - in in `$( document ).ready(function()` or in `onSelectRow : function(rowid, status, e)` and what are the values of the arguments {i.e. rid, gid, act, pos} of the function? – VijayD Apr 24 '15 at 12:49
  • @Victor: Which version of jqGrid you use? – Oleg Apr 24 '15 at 12:50
  • `version : "4.5.1"` I found this in jquery.jqGrid.js file. I guess this is the version of JQGrid. – VijayD Apr 24 '15 at 12:55
  • @Victor: You can use right mouse click to open context menu and to choose "View page source" to see the source code of [the demo](http://www.ok-soft-gmbh.com/jqGrid/SubclassActions.htm). I posted you my answer where I recommend you to update to use free jqGrid 4.8 or better to download the latest sources from [github](https://github.com/free-jqgrid/jqGrid). In the case you will be able to solve the problem very easy. – Oleg Apr 24 '15 at 12:59
  • Oleg, thanks for this. Do you know if such methodology works with the tree grid as well? I need to edit certain cells based on where they are in a tree grid - technically I think it has to be only leaves of the tree rows to be edited. (That is the realistic and tentative, but not the final requirement) – Dima R. Aug 26 '15 at 16:19
  • @DimaR.: Yes, the same idea works with TreeGriid too, but the implementation efforts depends on the version of jqGrid which you use. Since the end of 2014 I develop [free jqGrid](https://github.com/free-jqgrid/jqGrid), which is fork of jqGrid. See readmes for more information about the reasons, why I started my own fork. I implemented many new features described in [many wiki articles](https://github.com/free-jqgrid/jqGrid/wiki). One can for example define `editable` as function (see [here](https://github.com/free-jqgrid/jqGrid/wiki/editable-property-of-colModel-as-function) which simplify all – Oleg Aug 26 '15 at 16:40