4

I wanted to change the grid column sequence dynamically. For e.g. By default the grid will be loaded in LoginId, FirstName and LastName sequence. Based on some condition, I need to change the FirstName and LastName sequence.

Is there any way I can do this?

I tried doing like:

{name:'UserName',index:'UserName',width:82,sortable:false},
if(true)
{
   {name:'FirstName',index:'FirstName',width:65,sortable:false},
   {name:'LastName',index:'LastName',width:65,sortable:false},
}
else
{
   {name:'LastName',index:'LastName',width:65,sortable:false},
   {name:'FirstName',index:'FirstName',width:65,sortable:false},   
}

but I could not get this work.

Amar
  • 257
  • 2
  • 6
  • 14

1 Answers1

8

You can use remapColumns function to do this. In the documentation of the function you will find the example which seems to be wrong, because indexes in the permutation array seem to be 1-based and not 0-based. Try to use:

$("#list").remapColumns([1,3,2],true,false);

or

$("#list").remapColumns([1,3,2,4,5,6,7,8,9],true,false);

if you want to change the order of the second and third from the total 9 columns.

Jess Stone
  • 677
  • 8
  • 21
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for the reply. I am using jqGrid 3.5.3 version library. Is this feature available in this version? – Amar Sep 08 '10 at 09:46
  • 1
    @Amar: I have no idea. Is it now work? You should post a code example which can reproduce your problem. I am only a user and not a developer of jqGrid. On the page http://www.trirand.com/jqgridwiki/doku.php?id=wiki:change#jqgrid_3.7.2_changes_and_fixes there are no information about changes in `remapColumns`, but I am not sure. It seems to me there are some bug fixes in the time. Why you don't download from http://www.trirand.com/blog/?page_id=6 and use the last version of jqGrid? – Oleg Sep 08 '10 at 10:34
  • Thanks Oleg for the quick reply. I checked for the remapColumns method and it is not present in the 3.5.3 version. I am upgrading my jqgrid to latest version now. Thanks once again. – Amar Sep 08 '10 at 11:14
  • @Amar: You welcome! I recommend you to test the program after the usage of `remapColumns` more carefull. I had seen a lot of bugs in the `columnChooser` (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#column_chooser) which use `remapColumns`. If you will find a new bug post it to the http://www.trirand.com/blog/?page_id=393/bugs/. – Oleg Sep 08 '10 at 11:35
  • 2
    I've playing around with `remapColumns` lately. In jQGrid 4.1.2, the permutation is really 0-based. But note that rownumber (rn), multiselect checkbox (cb) and other hidden columns are also counted. So when you do the permutation, remember to include these columns. – jackysee Sep 09 '11 at 05:45
  • @jackysee: Yes, the index in `remapColumns` is just the index in the `colModel` which contain all helper columns. The `colModel` can have up to tree helper columns at the first places: 'rn' (`rownumbers: true`), 'cb' (`multiselect: true`) and 'subgrid' (`subGrid: true`) and some columns at the end in case of treegrid (like `level`, `isLeaf`, `expanded` and so on). You can calculate easy get the index of the column (`getColumnIndexByName` function from [here](http://stackoverflow.com/questions/6575192/jqgrid-change-background-color-of-row-based-on-row-cell-value-by-column-name/6575634#6575634) – Oleg Sep 09 '11 at 06:15
  • 1
    @frabiacca: Sorry, but I don't full understand your question. Form editing uses just the same order of the fields as the current order in `colModel`. If you would use `recreateForm: true` (my default setting) the form will be created in the new order. – Oleg Feb 10 '12 at 14:07