0

I am looking for a way to sort columns programmatically.

Please bare in mind, I do not mean to sort the grid as you normally would with sortGrid() or whatnot. I mean to arrange the row order from left to right programmatically, after the grid has been rendered.

So, I am aware that with:

$("#myGrid").jqGrid({sortable: true});

I am able to sort the columns by dragging and dropping them in order. The idea is to allow users to sort their column, and to have a callback save the order in the database, so each user can arrange their own columns however they want.

I am doing something similar with showing and hiding columns, but have been unable to reorder the columns programmatically without actually dragging and dropping them.

Any help would be highly appreciated.

francisco.preller
  • 6,559
  • 4
  • 28
  • 39

1 Answers1

1

You can use remapColumns method to reorder the column programmatically without usage drag & drop.

In the answer you will find the code which shows how to save user specific choice of the column order in localStorage. I personally prefer the way as the saving of the same information on the server side in the database. Nevertheless you can implement saving of the same information in the database instead of localStorage. To do this you need just change the implementation of saveObjectInLocalStorage and getObjectFromLocalStorage functions and include the ajax call instead of usage window.localStorage.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg, think I missed that method as it was very late that night :P I think I would prefer to store them in the database in the server side too as storing it locally would not do too well for people logging onto the web app from different computers, no? – francisco.preller Oct 04 '12 at 10:32
  • @francisco.preller: You are welcome! The place for saving the information is the matter of the taste. Possible problem could be the changes required to do in the saved state in case of changing of JavaScript code of jqGrid. The same problem exist in both cases. In case of usage of `localStorage` I use `myColumnStateName = function (grid) {return window.location.pathname + '#' + grid[0].id;}` (see the code of [the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_.htm)). To use new version I need just change the name of the page of the id of the grid. – Oleg Oct 04 '12 at 11:07