2012-03-13 102 views
0

各位程序员!从Cookie中选择Combobox中的值

我想要做以下事情:在屏幕上,用户可以选择Comboboxes中的值。这些值存储在cookie中,以便稍后在会话中访问同一页时可以恢复它们。饼干工作发现,ExtJs的工作很好。 Comboboxes是服务器绑定的,并在打开它们时检索它们的记录。这意味着,在恢复cookie值时,所需记录可能不存在。从服务器检索记录是无法解决的,因为组合框被分页,并且确定我必须加载哪个页面会很麻烦。

我尝试以下解决方案:

if(cookie.cobblerContactId != null) { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Achternaam', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.cobblerContactId, 
      Achternaam: cookie.CobblerContactName 
     });   
     behandelaarStore.add(rec); 
     behandelaarCombo.setValue(cookie.cobblerContactId); 
     behandelaarCombo.render(); 
     //editForm.render(); 
    } 

正如你所看到的,我人为地创造了纪录,将它添加到存储,然后选择它。问题是Combobox不显示正确的值,它显示为未选中。打开后,它将从服务器中检索25条记录的第一页。我也尝试了 - 注释掉 - editForm.render(),但也没有奏效。直接在Combobox(cookie.CobblerContactName)中编写文本无效,我试过selectText(),但似乎有些不同。如果我只是使用selectValue(),它将显示Id,而不是文本,因为数据存储没有该Id的记录。

我一直在尝试两天的更好的部分,但不能得到它的工作。有人有解决方案吗?

回答

0

尝试设置forceSelection: false然后您可以设置不属于列表的文本。

+0

forceSelection似乎没有过的问题。只要我的8小时过期,我可以发布这个问题的答案,即实际运行的代码。 – Paul1977 2012-03-13 15:08:04

0

forceSelection似乎没有任何关系,因为我向随后选择的数据存储添加了一个真正的新记录。因此显示的文本对应于实际的记录。我现在已经掌握了它的工作。当用户按下搜索按钮

function getCookie() { 
    if(Ext.util.Cookies.get('ticketIndexFilter') == null) 
     return; 
    var filterSet = Ext.ComponentMgr.get('additionalFilterSet');  
    var cookie = Ext.decode(Ext.util.Cookies.get('ticketIndexFilter')); 
    txtNummer.setValue(cookie.TicketNr); 
    if(cookie.datumVan != null && cookie.datumVan != ""){ 
     datVan.setValue(cookie.datumVan.substr(0, 10)); 
     filterSet.expand(); 
    } 
    if(cookie.datumTm != null && cookie.datumTm != ""){ 
     datTm.setValue(cookie.datumTm.substr(0, 10)); 
     filterSet.expand(); 
    } 
    if(cookie.relationId != null && cookie.relationId != "") { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Name', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.relationId, 
      Name: cookie.relationName 
     }, cookie.relationId);   
     relatieStore.add(rec); 
     relatieCombo.setValue(cookie.relationId); 
    } 
    if(cookie.cobblerContactId != null && cookie.cobblerContactId != "") { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Achternaam', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.cobblerContactId, 
      Achternaam: cookie.cobblerContactName 
     }, cookie.cobblerContactId);   
     behandelaarStore.add(rec); 
     behandelaarCombo.setValue(cookie.cobblerContactId); 
     filterSet.expand(); 
    } 
    if(cookie.statusId != null && cookie.statusId != "") { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Naam', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.statusId, 
      Naam: cookie.statusName 
     }, cookie.statusId);   
     statusStore.add(rec); 
     statusCombo.setValue(cookie.statusId); 
     filterSet.expand(); 
    } 
    if(cookie.priorityId != null && cookie.priorityId != "") { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Naam', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.priorityId, 
      Naam: cookie.priorityName 
     }, cookie.priorityId);   
     prioriteitStore.add(rec); 
     prioriteitCombo.setValue(cookie.priorityId); 
     filterSet.expand(); 
    } 
    if(cookie.relationContactId != null && cookie.relationContactId != "") { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Achternaam', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.relationContactId, 
      Achternaam: cookie.relationContactName 
     }, cookie.relationContactId);   
     contactStore.add(rec); 
     contactCombo.setValue(cookie.relationContactId); 
     filterSet.expand(); 
    } 
    if(cookie.categoryId != null && cookie.categoryId != "") { 
     var recDef = Ext.data.Record.create([ 
      {name: 'Id', type: 'int'}, 
      {name: 'Naam', type: 'string'} 
     ]); 
     var rec = new recDef({ 
      Id: cookie.categoryId, 
      Naam: cookie.categoryName 
     }, cookie.categoryId);   
     categorieStore.add(rec); 
     categorieCombo.setValue(cookie.categoryId); 
     filterSet.expand(); 
    } 
    //finally, an easy one 
    opgelostBox.setValue(cookie.finalized); 
} 

function setCookie(){ 
    var filterPresets = { 
     TicketNr: txtNummer.getValue(), 
     datumVan: datVan.getValue(), 
     datumTm: datTm.getValue(), 
     relationId: relatieCombo.getValue(), 
     relationName: relatieCombo.getValue() == "" ? null : relatieStore.getById(relatieCombo.getValue()).get('Name'), 
     cobblerContactId: behandelaarCombo.getValue(), 
     cobblerContactName: behandelaarCombo.getValue() == "" ? null : behandelaarStore.getById(behandelaarCombo.getValue()).get('Achternaam'), 
     statusId: statusCombo.getValue(), 
     statusName: statusCombo.getValue() == "" ? null : statusStore.getById(statusCombo.getValue()).get('Naam'), 
     priorityId: prioriteitCombo.getValue(), 
     priorityName: prioriteitCombo.getValue() == "" ? null : prioriteitStore.getById(prioriteitCombo.getValue()).get('Naam'), 
     relationContactId: contactCombo.getValue(), 
     relationContactName: contactCombo.getValue() == "" ? null : contactStore.getById(contactCombo.getValue()).get('Achternaam'), 
     categoryId: categorieCombo.getValue(), 
     categoryName: categorieCombo.getValue() == "" ? null : categorieStore.getById(categorieCombo.getValue()).get('Naam'), 
     finalized: opgelostBox.getValue()    
    }; 
    Ext.util.Cookies.set('ticketIndexFilter', Ext.encode(filterPresets));   
} 

setCookie方法被调用。在Ext.OnReady中调用getCookie来预设控件。我花了将近10个小时的时间,所以我想我会分享。

的第一篇文章中包含一个错误:cookie.CobblerContactName而不是cookie.cobblerContactName