在 Sencha Touch 中进行自定义排序存储
我有一个商店 + 模型,它连接到第 3 方插件 (Ext.ux.TouchGridPanel)。该插件使用相关映射正确调用商店的 sort() 方法。一切都运转良好,商店自行整理。不过,我更愿意在商店中添加顾客分类功能。我尝试将 sortType 字段添加到我的模型中:
Ext.regModel("Transactions", {
fields: [
{
name: 'unit',
type: 'string',
sortType: function(value) {
console.log('PRINT GDAMNIT');
return 0;
}
},
...
]
});
但是,这不起作用,并且 sortType 没有被调用。
TLDR:如何为商店进行自定义排序?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的商店需要添加一个排序器,该排序器将在调用 sortType 函数之前对该字段进行排序。
排序类型 将一个字段的值转换为另一个字段的值值以确保正确排序。如果您没有对该字段进行排序,则没有理由调用该函数。您可以将 sortDir 添加到字段,该字段将仅根据字段的类型将字段排序为升序/降序。
Your store will need a sorter added that will sort on that field before it will call the sortType function.
Sort type converts the value of a field into another value to ensure proper ordering. If you aren't sorting on that field than there is no reason to call that function. You could add the sortDir to the field which would sort the field into ascending/descending order based on the type of the field alone.
解决方法可能是(我知道这听起来效率低下,但请耐心等待)向模型实例添加一个额外的字段(我们称之为 sortField)并将其用于排序功能。然后,您可以应用自定义排序算法循环遍历商店中的模型实例,并将排序值 0、1、2、3、4、5 等分配给 sortField。然后在您的商店中,您可以添加
'sorters: 'sortField'
...希望这能有所帮助,我目前正在经历类似的事情。A workaround might be to (I know this sounds inefficient but bear with me) add an extra field to your model instances (lets call it sortField) and use that for your sorting function. You can then loop through your model instances in your store applying your custom sorting algorithm and assign a sort value of 0,1,2,3,4,5 etc.. to sortField. Then in your store, you can add
'sorters: 'sortField'
... Hope this helps a bit, I'm going through something similar at the current moment.Sencha Touch 2 中的自定义 SortType 会相应地工作,按照 http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes:
The custom SortType in Sencha Touch 2 works accordingly, as per http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes:
您尝试做的事情可能很棘手。默认情况下,调用
store.sort()
会删除所有现有排序器(根据 Sencha Touch API 文档)。要保留现有排序器,您需要将排序器添加到MixedCollection
store.sorters
中。其次,要使用自定义排序函数调用排序方法,您需要将特定的
sorterFn
而不是property
传递给Sorter
(同样) ,请参阅 API 了解更多详细信息) - 但是这可能会很棘手,因为排序调用由插件启动。不确定这是否有助于解决您的问题,但也许它可以帮助您找到正确的方向。
What you're attempting to do might be tricky. Calling
store.sort()
removes all existing sorters by default (according to Sencha Touch API documentation). To keep the existing sorters you would need to add the sorter to theMixedCollection
store.sorters
.Secondly, to call the sort method with a custom sort function, you would need to pass a specific
sorterFn
instead ofproperty
to theSorter
(again, see the API for more details) - but this might prove tricky since the sort call is initiated from the plugin.Not sure if this helps to solve your problem, but maybe it assists you to look at the right direction.