简体   繁体   中英

How to set a selectfield in Sencha Touch from array?

I am working in a Sencha Touch app and I would like to push in a combobox, values previously filtered from a store.

var store = Ext.getStore('Surveys');
var templatesAvailable = [];
store.filterBy(function (record) {
  console.log(record.get('templateName'));
  record.get('templateName'); --> I get value
  templatesAvailable.push(record.get('templateName')); --> into the array
});

Next step would be transfer array to a specified selector, for example and in my case...

this.getTemplateSelector

  {
    xtype       : 'selectfield',
    itemId      : 'selectSurveysTemplates',
    cls         : 'filterbar-selectfieldplus',
    displayField: 'value',  --> here is the secret  ;-)
    valueField  : 'id',
    autoCreate  : true
  },

What should it be the correct way for this implementation? I have tested with different options but It is not working for me..

Thank you in advance..

How about this one:

selectfield.setOptions(
    store.getRange().map(function(record) {
        return {
            text:record.get("templateName"),
            value:record.get("templateName")
        };
    })
);

This should do the following:

  • Selectfield.setOptions sets the value of Selectfield.options . According to the docs, options takes an array of objects with properties text and value , so I guess setOptions takes the same.
  • Store.getRange gives you an array of records.
  • Array.map that takes a function and converts every item in the input array using that function.

Voilà, you should be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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