Search code examples
jqueryjqgridediting

jqGrid: Disable form fields when editing


I'm currently developing a web application designed for the administration of vending machines and such. I decided to use jQuery, jQuery UI and jqGrid for this project, so I can easily provide a great and highly customizable user interface.
Unfortunately, the jqGrid documentation is pretty outdated and doesn't cover all the features of this great plug-in (cause I do really like it, even though the documentation is rather poor).

Anyway, enough background information, I suppose. Let's get to the point:
I use the navbar which is built-in to jqGrid to Add, Edit and Delete items from the grid.
I've got this working like a charm, except for one thing: some fields may only be enabled (or visible) when adding a new item and not when in editing-mode (they should be hidden and/or disabled).

Example:
The company I'm working for sells vending towers and there are several types (different sizes and stuff) of these towers. When a new tower is added to a location and entered into the system, the type must be set. But the tower doesn't magically change over time, so this field may not be edited later on.

Does anyone know if this behavior can be accomplished by changing some initialization parameters?
Perhaps it's an undocumented edit-option (editoptions) or form-option (formoptions)?
Or maybe you've got a simple solution for this?

I'd love to hear your suggestion / solutions!
Thanks =)


Solution

  • You can implement your requirements in different ways. For example, inside of beforeShowForm event you can hide or show the

    jQuery("#list").jqGrid({
        colModel: [
            { name: 'Name', width: 200, editable: true },
       //...
    
    }).jqGrid('navGrid','#pager', { edit: true, add: true, del: false},
              { // edit option
                  beforeShowForm: function(form) { $('#tr_Name', form).hide(); }
              },
              { // add option
                  beforeShowForm: function(form) { $('#tr_Name', form).show(); }
              });
    

    where the id "tr_Name" is constructed from "tr_" prefix and "Name" - the name property of the column from the colModel.

    UPDATED: In the answer and in another one are shown one more way how the properties can be changed dynamically immediately before the editing will be initialized.

    UPDATED 2: Free jqGrid allows to define editable as callback function or as "disabled", "hidden" or "readonly". See the wiki article. It allows to implement the same requirements more easy.