Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden Columns in jqGrid

Is there any way to hide a column in a jqGrid table, but have it show as read-only when the row is edited in the form editor modal dialog?

like image 663
maxpower47 Avatar asked Nov 02 '09 15:11

maxpower47


People also ask

How to hide columns in jqGrid?

setup your grid function as follows. $('#myGrid'). jqGrid({ ... colNames: ['Manager', 'Name', 'HiddenSalary'], colModel: [ { name: 'Manager', editable: true }, { name: 'Price', editable: true }, { name: 'HiddenSalary', hidden: true , editable: true, editrules: {edithidden:true} } ], ... };

How do you hide jqGrid?

Just set opacity:0 for jqgrid element. it will work also.


2 Answers

This feature is built into jqGrid.

setup your grid function as follows.

$('#myGrid').jqGrid({    ...    colNames: ['Manager', 'Name', 'HiddenSalary'],    colModel: [                               { name: 'Manager', editable: true },                { name: 'Price', editable: true },                { name: 'HiddenSalary', hidden: true , editable: true,                    editrules: {edithidden:true}                 }              ],    ... }; 

There are other editrules that can be applied but this basic setup would hide the manager's salary in the grid view but would allow editing when the edit form was displayed.

like image 100
Bobby Borszich Avatar answered Sep 20 '22 08:09

Bobby Borszich


I just want to expand on queen3's suggestion, applying the following does the trick:

editoptions: {                dataInit: function(element) {                            $(element).attr("readonly", "readonly");                          }               } 

Scenario #1:

  • Field must be visible in the grid
  • Field must be visible in the form
  • Field must be read-only

Solution:

colModel:[         {  name:'providerUserId',                index:'providerUserId',                 width:100,editable:true,                 editrules:{required:true},                 editoptions:{                              dataInit: function(element) {                                    jq(element).attr("readonly", "readonly");                               }                             }             }, ], 

The providerUserId is visible in the grid and visible when editing the form. But you cannot edit the contents.


Scenario #2:

  • Field must not be visible in the grid
  • Field must be visible in the form
  • Field must be read-only

Solution:

colModel:[            {name:'providerUserId',             index:'providerUserId',              width:100,editable:true,              editrules:{                          required:true,                           edithidden:true                       },             hidden:true,              editoptions:{                    dataInit: function(element) {                                                   jq(element).attr("readonly", "readonly");                            }                       }          },         ] 

Notice in both instances I'm using jq to reference jquery, instead of the usual $. In my HTML I have the following script to modify the variable used by jQuery:

<script type="text/javascript">     var jq = jQuery.noConflict(); </script> 
like image 28
chris Avatar answered Sep 22 '22 08:09

chris