在 Sencha Touch 中进行自定义排序存储

发布于 2024-11-01 05:16:25 字数 519 浏览 2 评论 0 原文

我有一个商店 + 模型,它连接到第 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:如何为商店进行自定义排序?

I have a store + model which is connected to a 3rd party plugin (Ext.ux.TouchGridPanel). The plugin calls the store's sort() method properly with the relevant mapping. Everything is working fine, and the store sorts itself. However, I would prefer to add customer sorting to the store. I have tried adding a sortType field into my model:

Ext.regModel("Transactions", {
    fields: [
        {
            name: 'unit',
            type: 'string',
            sortType: function(value) {
                console.log('PRINT GDAMNIT');
                return 0;
            }
        },

        ...

    ]
});

This, however, is not working, and the sortType is not getting called.

TLDR: How to make custom sorting work for stores?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓬勃野心 2024-11-08 05:16:25

您的商店需要添加一个排序器,该排序器将在调用 sortType 函数之前对该字段进行排序。

var store = new Ext.data.Store({ 
   model: 'Transactions',
   sorters: [
      {
        property: 'unit',
        direction: 'DESC'
      }
   ]}
);

排序类型 将一个字段的值转换为另一个字段的值值以确保正确排序。如果您没有对该字段进行排序,则没有理由调用该函数。您可以将 sortDir 添加到字段,该字段将仅根据字段的类型将字段排序为升序/降序。

Your store will need a sorter added that will sort on that field before it will call the sortType function.

var store = new Ext.data.Store({ 
   model: 'Transactions',
   sorters: [
      {
        property: 'unit',
        direction: 'DESC'
      }
   ]}
);

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.

樱花坊 2024-11-08 05:16:25

解决方法可能是(我知道这听起来效率低下,但请耐心等待)向模型实例添加一个额外的字段(我们称之为 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.

毁梦 2024-11-08 05:16:25

Sencha Touch 2 中的自定义 SortType 会相应地工作,按照 http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes

Ext.apply(Ext.data.SortTypes, {
    asPerson: function(person){
        // expects an object with a first and last name property
        return person.lastName.toUpperCase() + person.firstName.toLowerCase();
    }
});

Ext.define('Employee', {
    extend: 'Ext.data.Model',
    config: {
        fields: [{
            name: 'person',
            sortType: 'asPerson'
        }, {
            name: 'salary',
            type: 'float' // sortType set to asFloat
        }]
    }
});

The custom SortType in Sencha Touch 2 works accordingly, as per http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes:

Ext.apply(Ext.data.SortTypes, {
    asPerson: function(person){
        // expects an object with a first and last name property
        return person.lastName.toUpperCase() + person.firstName.toLowerCase();
    }
});

Ext.define('Employee', {
    extend: 'Ext.data.Model',
    config: {
        fields: [{
            name: 'person',
            sortType: 'asPerson'
        }, {
            name: 'salary',
            type: 'float' // sortType set to asFloat
        }]
    }
});
香草可樂 2024-11-08 05:16:25

您尝试做的事情可能很棘手。默认情况下,调用 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 the MixedCollection store.sorters.

Secondly, to call the sort method with a custom sort function, you would need to pass a specific sorterFn instead of property to the Sorter (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文