[英]Remember selectfield sencha touch
如何在selectfield
touch中store
selectfield
,這樣,當選擇一個字段時,任何方法都使用該值,但是當頁面刷新時,它會自動選擇上一個選擇。
我的代碼:
{
xtype: 'fieldset',
items: [{
xtype: 'selectfield',
id: 'remem',
label: 'MY Label',
store: 'remem',
options: [
{
text: 'Option1',
value: 'a'
},
{
text: 'Option2',
value: 'b'
}]
}]
}
@ developer,@ jenson和我建議您將值保存在cookie中,以便在刷新瀏覽器之間保留該值。 如果我記得正確的話,Extjs在實用程序名稱空間中提供了一個cookie管理器類。
編輯
對於Sencha Touch,您實際上應該按照此處的文檔使用LocalStorage代理。文檔中有一個示例,顯示了如何保存用戶特定信息以供以后檢索(刷新等之后)
實際上,通過將用戶顯示偏好設置保存在我的應用程序中,我已經將其用於我自己的應用程序,以便他們下次登錄時界面保持原樣。
商店:
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'
}
}
});
模型
Ext.define('MyApp.model.UserPreferencesModel', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field'
],
config: {
fields: [
{
name: 'userPrefKey'
},
{
name: 'userPrefValue'
}
]
}
});
通過此設置,您可以像平常對任何其他商店所做的那樣添加記錄
store.add({userPrefKey: '01-showMap', userPrefValue: true});
或編輯現有條目
rec = store.findRecord('userPrefKey','01-showMap');
rec.set('userPrefValue',false);
每當您要保存更改時,還必須在存儲上調用sync(),並從本地存儲中檢索保存的記錄,只需像其他任何存儲一樣調用load()即可。
請注意,我的代碼是為Touch 2.3編寫的,因此在最新的2.4.x版本中可能略有更改。 但這當然可以帶您上路。
將選擇字段值存儲到localstorage中,並在刷新頁面時將該值分配給selectField。 因此,您將始終獲得先前選擇的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.