简体   繁体   English

记住selectfield煎茶

[英]Remember selectfield sencha touch

How to store the selectfield in sencha touch, such that when a field is selected, the value used by any method, but when the page refreshed, it is automatically select the previous selection. 如何在selectfield touch中store selectfield ,这样,当选择一个字段时,任何方法都使用该值,但是当页面刷新时,它会自动选择上一个选择。

My Code: 我的代码:

          {
                xtype: 'fieldset',
                items: [{
                    xtype: 'selectfield',
                    id: 'remem',
                    label: 'MY Label',
                    store: 'remem',
                    options: [
                    {
                        text: 'Option1',
                        value: 'a'
                    },
                     {
                        text: 'Option2',
                        value: 'b'
                    }]
}]
}

@developer, @jenson and I are suggesting you should save the value in a cookie so the value is persisted between browser refreshes. @ developer,@ jenson和我建议您将值保存在cookie中,以便在刷新浏览器之间保留该值。 Extjs offers a cookie manager class in the utilities namespace if I remember correct. 如果我记得正确的话,Extjs在实用程序名称空间中提供了一个cookie管理器类。

EDIT 编辑

For Sencha Touch you should actually make use of the LocalStorage proxy as per the docs here There's an example in the docs showing how to save user specific information for retrieval later on (after refreshes, etc) 对于Sencha Touch,您实际上应该按照此处的文档使用LocalStorage代理。文档中有一个示例,显示了如何保存用户特定信息以供以后检索(刷新等之后)

I have actually made use of this for my own application by saving user display preferences in my application so when they next login the UI is as they left it. 实际上,通过将用户显示偏好设置保存在我的应用程序中,我已经将其用于我自己的应用程序,以便他们下次登录时界面保持原样。

Store: 商店:

Ext.define('MyApp.store.UserPreferencesStore', {
    extend: 'Ext.data.Store',

    requires: [
        'MyApp.model.UserPreferencesModel',
        'Ext.data.proxy.LocalStorage'
    ],

    config: {
        model: 'MyApp.model.UserPreferencesModel',
        storeId: 'UserPreferencesStore',
        proxy: {
            type: 'localstorage',
            id: 'userpreferences'
        }
    }
});

Model 模型

Ext.define('MyApp.model.UserPreferencesModel', {
    extend: 'Ext.data.Model',

    requires: [
        'Ext.data.Field'
    ],

    config: {
       fields: [
            {
                name: 'userPrefKey'
            },
            {
                name: 'userPrefValue'
            }
        ]
    }
});

With this setup you can add a record as you normally would do for any other store 通过此设置,您可以像平常对任何其他商店所做的那样添加记录

store.add({userPrefKey: '01-showMap', userPrefValue: true});

or edit existing entries 或编辑现有条目

rec = store.findRecord('userPrefKey','01-showMap');
rec.set('userPrefValue',false);

You must also call sync() on the store whenever you want to save changes, and to retrieve the saved records from local storage just call load() like for any other store. 每当您要保存更改时,还必须在存储上调用sync(),并从本地存储中检索保存的记录,只需像其他任何存储一样调用load()即可。

Note that my code was written for Touch 2.3, so it may have changed slightly in the most recent 2.4.x version. 请注意,我的代码是为Touch 2.3编写的,因此在最新的2.4.x版本中可能略有更改。 But this should certainly get you on your way. 但这当然可以带您上路。

store select field value into localstorage and assign that value to selectField whenever you refresh page . 将选择字段值存储到localstorage中,并在刷新页面时将该值分配给selectField。 So you will get always previous selected value. 因此,您将始终获得先前选择的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM