动态地将json对象添加到Sencha selectfield?

时间:2013-03-18 15:12:00

标签: javascript json sencha-touch sencha-touch-2

我有一个json对象。如何将此添加到sencha selectfield?

{

   "info":{
      "sd-1dd2-11b2-0000-242d50cf1f9f":"root",
      "sd-8d30-11e2-98f5-bf6df0e83168":"WS123",
      "sd-8d31-11e2-956a-8f722b3d14b8":"test"
   }
}

将项目添加到选择字段的代码

 {

                        xtype: 'selectfield',
                        autoLoad: true,

                        label:'Parent category',
                        name: 'parent_uuid',

                        options : [


                            {text: 'Select category',  value: 'select'},
                            {text: 'Root',  value: 'sd-1dd2-11b2-0000-242d50cf1f9f'}
    ]

}

如何将我的json对象转换为此格式{text: 'Root', value: 'sd-1dd2-11b2-0000-242d50cf1f9f'}

1 个答案:

答案 0 :(得分:3)

你去吧

var json = {
   "info":{
      "sd-1dd2-11b2-0000-242d50cf1f9f":"root",
      "sd-8d30-11e2-98f5-bf6df0e83168":"WS123",
      "sd-8d31-11e2-956a-8f722b3d14b8":"test"
   }
},
    options = [],
    i;

for (i in json.info) {
  options.push({text: json.info[i], value: i})
}

然后您只需要获取选择字段并使用add函数添加选项

selectfield.add(options);

希望这有帮助