admin管理员组文章数量:1384131
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
2 Answers
Reset to default 3Version 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.
本文标签: javascriptjqgrid Sort based on two columnsStack Overflow
版权声明:本文标题:javascript - jqgrid: Sort based on two columns - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744441923a2606477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论