Search code examples
javascriptjqueryjqgridjqgrid-formatter

Show or hide columns in JQGird loading


Am using JQ grid. I want to show/hide columns when grid is loading.

Scenario:

When Grid loads I want to check the condition if condition is true, show the columns else hide.

Example:

if(GlopalId==1){
   $("#grid").jqGrid('hideCol', ["name","invdate"]);
}
else{
   $("#grid").jqGrid('showCol', ["name","invdate"]);
} 

How to check this condition when grid loading time or after grid load completed.

This my grid structure.

$("#grid").jqGrid({
        datatype: "local",
        data:mydata,
        colModel: [
                   { label: 'Inv No', name: 'id', key:true },
                   { label: 'Date', name: 'invdate' },
                   { label: 'Client', name: 'name', width: 250, editable:true,edittype:'textarea'  },
                   { label: 'Amount', name: 'amount',editable:true },
                   { label: 'Tax', name: 'tax' },
                   { label: 'Total', name: 'total' },
                   { label: 'Notes', name: 'note'}
               ],
               page: 1,
               width: 1000,
               height: 250,
               rowNum: 20,
               viewrecords: true,
               scroll: 1, // set the scroll property to 1 to enable paging with scrollbar - virtual loading of records
               emptyrecords: 'Scroll to bottom to retrieve new page', // the message will be displayed at the bottom 
               pager: "#jqGridPager",
               shrinkToFit: false,
               hoverrows: false, 
               onSelectRow: editRow, // the javascript function to call on row click. will ues to to put the row 
            //   'cellEdit' : true,
             // cellurl:calMeth
    });

Solution

  • You use datatype: "local", data: mydata. So you have full data, which need be loaded in the grid, before creating the grid. Why you not just analyse the data and use hidden: true property in come columns of jqGrid directly during creating the grid? Alternatively you can use hidden: isHidden for example in colModel for some columns and to set the value of isHidden variable before the grid will be created.