2

If I used remapColumns in columnChooser's done event, then I don't have problem to sort the columns. But when I moved remapColumns to beforeRequest with a new perm information, see below:

beforeRequest: function(){                                              
    perm =   {0:2,1:0,2:1,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10};
    myGrid.jqGrid("remapColumns", perm,  true);
}

The columns are correctly reordered, and I can search column value, but the column sorting function was just disabled. The click event seems not working. Can somebody help? How can I get it back? Really appreciate it.

Thanks very much!

Yan

Oleg
  • 220,925
  • 34
  • 403
  • 798
user1342336
  • 967
  • 2
  • 16
  • 28

1 Answers1

5

In the answer you can find an example how to use remapColumns and I hope another helpful information for you. In the demo I use remapColumns once inside of loadComplete. I don't understand why you want to change the column order on every request to the server.

UPDATED: The modified demo don't have the effect which you described. The reason was that one called in the old demo the method remapColumns with empty ([]) permutation parameter and remapColumns works incorrect in the case. So I included additional test for myColumnsState.permutation.length > 0.

UPDATED 2: Free jqGrid provides the method remapColumnsByName additionally to remapColumns. The first parameter of remapColumnsByName is array of column names, which should be on the grid. Columns "subgrid", "cb" and "rn" could be included or not. The understanding of the usage of remapColumnsByName is much more easy as the usage of remapColumns especially in situations where the order of columns could be changes by the user (by columnChooser, wor example). It's strictly recommended to use remapColumnsByName instead of remapColumns.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg, I can not believe that you answered my question so fast. Actually in my project, I will change the column order when the columns order is different with the original one/default one. I have saved both original perm and new perm info to compare before using remapColumns in beforeRequest function, loadComplete is also fine, but it seems the columns sorting doesn't work in either one. I have read many of your posts which are really useful. Thank you. - Yan – user1342336 Apr 19 '12 at 21:11
  • @user1342336: You are welcome! I am glad that my old answers could help too. Because of sorting I didn't seen any problems. Try to play with [the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_single.htm) for example (see [the answer](http://stackoverflow.com/a/8547852/315935)). It seems work good. I find the usage of `remapColumns` internal parameter of jqGrid and the method with the same name a little uncomfortable, but if one have a working demo than one can implement the needed feature relatively quickly. If you do find some problems post me about there. – Oleg Apr 19 '12 at 21:23
  • Oleg, I played with your demo, and it just works like a charm :). You even can save search, page, column hidden etc info. Actually I need to save all of these info to DB for different users. You are using localStorage, do you think I can convert your codes for DB saving? How can I do that, can you give me some hint? I saved them in very different way as you did, and reloading fine except the column sorting, and code is a little ugly when setting the new hidden columns info. I really appreciate your help. Thanks. -- Yan – user1342336 Apr 19 '12 at 22:09
  • @user1342336: You are welcome! You can sand the current grid state to the server per ajax inside of `saveObjectInLocalStorage` (see my code). If it will be asynchronous request it will produce traffic, but it will not upset the user. You can reduce the number of send of the grid state to the server. Moreover you can consider whether the saving of state in DB really required. On another computer it could be another screen resolution and the column widths could looks not so good. In any ways I hope the code will be helpful for you. – Oleg Apr 19 '12 at 22:51
  • Thanks so much, Oleg, your suggestion is very helpful, I will try it. Since it is my first time to vote a answer, how should I do that? I think you are terrific, really :) -- Yan – user1342336 Apr 19 '12 at 23:45
  • Thanks Oleg, I am reading your code and testing the demo, most of the time, the columms sorting works perfectly, however I got a moment that the columns sorting was disabled. First I selected 5 rows per page, then clicked the column header for sorting, didn't work. Do you know the reason? It is kind of strange. – user1342336 Apr 20 '12 at 13:24
  • @user1342336: Could you describe step by step how the problem could be reproduced on [the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_single.htm) starting with click on "X" to clear all previously saved settings? – Oleg Apr 20 '12 at 13:30
  • Hi Oleg, I think I know how did I get the problem. If I don't touch columnchooser, and just select row num per page first, then reflesh the link/page, then test column sorting, and it seems not working. But if I touched columnchooser first, there is no problem. I know this is a little complicated. I hope I was wrong. – user1342336 Apr 20 '12 at 13:36
  • @user1342336: OK, I can reproduce the problem. Thanks! I will later analyse the problem and write the the solution on the problem. – Oleg Apr 20 '12 at 13:55
  • Oleg, you may also try selecting a row, then refresh the page. I am looking forward your solution. Thank you. -- Yan – user1342336 Apr 20 '12 at 14:39
  • @user1342336: I fond the problem and fixed the code. See "UPDATED" part of my answer. – Oleg Apr 20 '12 at 15:04
  • Great! Oleg, I would like to play the demo soon. Thanks :) --Yan – user1342336 Apr 20 '12 at 15:19
  • @user1342336: You are welocme! – Oleg Apr 20 '12 at 15:34