javascript Extjs按字母数字字段排序存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16093825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extjs sorting store by alphanumeric field
提问by Grahame A
I've got a bunch of records whose names are like "Itemtype #", and when they are outputted into a tree view, they're sorted incorrectly, like so:
我有一堆名称类似于“Itemtype #”的记录,当它们输出到树视图中时,它们的排序不正确,如下所示:
- Item 1
- Item 10
- Item 11
- Item 12
- Item 13
- Item 2
- Item 3
- Item 4
- 第 1 项
- 第 10 项
- 第 11 项
- 第 12 项
- 第 13 项
- 第 2 项
- 第 3 项
- 第 4 项
My model fields are defined as follows, and I am sorting on "Name":
我的模型字段定义如下,我正在对“名称”进行排序:
fields: [
{ name: 'Id', defaultValue: 0, type: 'int', mapping: 'Id' },
{ name: 'Name', defaultValue: '', type: 'string', mapping: 'Name', sortType: Ext.data.SortTypes.asUCString },
{ name: 'Type', defaultValue: 0, type: 'int', mapping: 'Type' },
{ name: 'CreationDate', type: 'date', mapping: 'CreationDate' }
],
Can anybody point me in the right direction? I'm using extjs 4.0.5
有人能指出我正确的方向吗?我正在使用 extjs 4.0.5
采纳答案by wes
Try calling TreeStore.sortwith a sorter config with a sorting callback. A simple field sort won't do in this case because the naive alpha sort isn't what you want. The example at the top of the Ext.util.Sorter docshows how to do it in a store, but you can just as easily add it to the sorters
param of your model.
尝试使用带有排序回调的排序器配置调用TreeStore.sort。在这种情况下,简单的字段排序不起作用,因为简单的 alpha 排序不是您想要的。Ext.util.Sorter 文档顶部的示例展示了如何在商店中执行此操作,但您也可以轻松地将其添加到模型的参数中。sorters
回答by Adidi
In store you have to set up how the data will display by sorters property:
在商店中,您必须通过 sorters 属性设置数据的显示方式:
var store = Ext.create('Ext.data.JsonStore', {
.
.
.
remoteSort: false, //true for server sorting
sorters: [{
property: 'Name',
direction: 'DESC' // or 'ASC'
}],
.
.
.
})