jqGrid default sort order?
Asked Answered
O

7

19

It appears that the jqGrid sortname and sortorder properties do not actually cause your data set to be sorted - they just cause the up/down arrows to be displayed.

How can you get your data set to sort on download?

The sort works well when you click the column headers but I want a default sort to be applied to our data.


Update: When we click the next button, the next request sorts the data.

This causes a slightly confusing UI where the data loads with a down arrow on a column - and the data isn't sorted - but when you click next the data is now sorted.

It seems if I omit sortname and sortorder that jqGrid still displays the sort icon - weird.

Orlando answered 16/9, 2010 at 19:49 Comment(0)
I
16

You might be giving the same parameter twice ( a very common mistake when copy paste progrraming :) no offenses. I do it too.)

Say you want to sort by OpeningDate descending

..... options .... 
sortname: "OpeningDate",
sortorder: "desc",  <---- assume you write this line and expect to sort descending
..... some other options .... 
sortorder: "asc",   <---- and this line may also be there but you may not be noticing it 
..... and other options .... 

the second "sortorder" option overrides the first and you will not be able to sort descending

Importune answered 25/5, 2011 at 15:10 Comment(0)
R
8

I was facing the same problem. Use this after loading the data:

$("#tableId").jqGrid('sortGrid','colName', false, 'asc');

or

$("#tableId").sortGrid('colName', false, 'asc');

The grid is reloaded if the boolean value is set to true. The last parameter can be 'asc' / 'desc', depending on the sort order.

Regnant answered 27/5, 2013 at 12:36 Comment(0)
B
2

Try to use

$("#list").jqGrid('setGridParam',{ page: 1 }).trigger("reloadGrid");

or

$("#list").jqGrid('setGridParam',{ rowNum: 10 }).trigger("reloadGrid");

(replace 10 to value which you define as rowNum parameter). If it will not help then post your code in your question.

Bawdyhouse answered 16/9, 2010 at 20:44 Comment(7)
Are you suggesting we load the grid twice? The initial load (not sorted) and a second load which will hopefully be sorted?Orlando
@Marcus: You don't insert any code in your question so I have to guess what do you do. It you load the JSON or XML data from the server the server sort the data, so you should fix your server code. If you load the data from the local source or load from the server as unsorted with "loadonce:true" you should sort the data once. To do this you have to refresh the data displayed in the current page ot the grid. You can do this with respect of trigger("reloadGrid") - in case of local data it is just "refreshing" the current page. Sometimes setting parameters like page or rowNum helpsBawdyhouse
We are loading unsorted data from the server with loadonce:true. Will try trigger...Orlando
I tried your solution but it does not to make any effect. I use the loadonce: true but I have plenty of parameters (subgrid, filter toolbars, scrollbars). i tried to put the trigger call in the loadComplete and outside the jqgrid declaration but no chance.Takeover
@ruffp: You should better describes your problem in a separate question where you post the definition of your jqGrid (I mean the corresponding JavaScript code). The old version 3.7 which introduced loadonce:true parameter was used in many cases in the wrong way. So it's better to analyse the current problems.Bawdyhouse
@Oleg: ok, I will ask my own question. I try not to create double questions but it seems this one is not more alive even unaccepted. ThxTakeover
This is the valid answer and it is working for me in a production website. Thanks for the help.Thoracotomy
S
2

You might have made the same mistake as myself, which was to base my client script off of an old example which used "sortName" instead of "sortname"

Serum answered 11/10, 2010 at 17:32 Comment(1)
This is the correct answer. My grid is passing the sort criteria the first time it is asking for data. The properties are sortname and sortorder.Listerism
S
1

the problem is (I believe) that the parameter sortName nor sidx are read when requesting the first data set

Serum answered 11/10, 2010 at 17:28 Comment(0)
N
0

/Cheesy Answer Alert

Why not hide the sorted icon the first time you load? First time someone sorts, unhide it and things work as intended. Sounds like it was your intent to load unsorted until the user chooses a column to sort on.

Nabokov answered 11/10, 2010 at 18:1 Comment(1)
I sort of agree with this solution...sort the first time around using server side code so it displays accurately when loaded if you want it sorted initially. Either set the default search column or hide/show (#2601976) the sort column after the user decides to sort based on which direction your going.Langham
P
0

The accepted solution did work sort of. Eventually I found that I had to sort my data at the server before I sent it to the client. After that, everything worked great.

Putandtake answered 20/4, 2023 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.