最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jqgrid: Sort based on two columns - Stack Overflow

programmeradmin4浏览0评论

I am trying to sort a column in jqgrid by two different columns. For instance, I have a column "StoreName" and its sort will be determined by the columns "d0_key" and "d0_tot_key" (both integers). I understand that sorttype in the colmodel can be use to create a custom sort, but I'm not finding an algorithm that takes into account both columns. Below is a simple representation of the data.

StoreName   d0_key  d0_tot_key
92nd Gessner    1   0
92nd Gessner    1   0
Total Sum       1   10
Total Avg       1   20
Annco           2   0
Annco           2   0
Total Sum       2   10
Total Avg       2   20

I am trying to sort a column in jqgrid by two different columns. For instance, I have a column "StoreName" and its sort will be determined by the columns "d0_key" and "d0_tot_key" (both integers). I understand that sorttype in the colmodel can be use to create a custom sort, but I'm not finding an algorithm that takes into account both columns. Below is a simple representation of the data.

StoreName   d0_key  d0_tot_key
92nd Gessner    1   0
92nd Gessner    1   0
Total Sum       1   10
Total Avg       1   20
Annco           2   0
Annco           2   0
Total Sum       2   10
Total Avg       2   20
Share Improve this question asked Nov 6, 2013 at 22:31 Thomas MondelliThomas Mondelli 1491 gold badge2 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Version 4.5.0 of the jqGrid added multi-sort support. You should be able to do something like this:

Just make sure you've turned multiSort on:

$('#gridId').jqGrid({
    ...
    multiSort: true,
    ...
});

That will give you the ability to click multiple headers. If you want to sort programmatically, this should work:

$('#gridId')
    .jqGrid('sortGrid', 'd0_key', true, 'asc')
    .jqGrid('sortGrid', 'd0_tot_key', true, 'asc');

There are other additional properties that may be of interest to you that go on the column model objects, such as firstsortorder ("asc"/"desc") and sortable (true/false).

Also, it looks like there were some bug fixes in 4.5.1 and 4.5.4 for multi-sorting, so I would just go ahead and download the latest version.

If you need that sorting by StoreName column should by made based on the values from the columns "d0_key" and "d0_tot_key" you should define sorttype as function in the StoreName column. Before sorting by column StoreName will be started the sorttype column will be called. The value returned by the function will be used instead of the value from the StoreName column during determine the order or sorted grid. So you can construct some function like d0_key * 1000 + d0_tot_key which corresponds your requirements. If I understand you correct then it can be about the following

{
    name: "StoreName",
    sorttype: function (cell, obj) {
        return parseInt(obj.d0_key) * 1000 + parseInt(obj.d0_tot_key);
    },
    ...
}

The answer, this one and another one provides examples of usage sorttype as function. This one seems to me mostly close to your requirements.

发布评论

评论列表(0)

  1. 暂无评论