2016-11-30 43 views
0

我有一个复选框组,它将有一个动态数量的复选框。后端返回包含标签和输入值的数据。我循环这些记录并为每个记录生成一个复选框对象。但是当我将生成的数组传递给items数组时,没有任何反应。ExtJS - 动态生成复选框

这里是我的复选框组类的片段。

Ext.define("MyApp.view.form.field.CheckboxGroup",{ 
    extend:"Ext.form.CheckBoxGroup", 
    ... 
    ... 
    initComponent:function(){ 

    this.items = getCheckboxes(); 

    ... 

    this.callParent(arguments); 
    }, 

    getCheckboxes:function(){ 
    Ext.Ajax.request({ 
     url:"blah/getcheckboxes", 
     scope:this, 
     success:function(resp_){ 
      var resp = Ext.JSON.decode(resp_.responseText); 
      var checkboxesArr = []; 
      if(resp.data){ 
      for(var i=0; i<resp.data.length; i++){ 
       checkboxesArr.push({boxLabel:resp.data[i].label, inputValue:resp.data[i].id, ....}); 
      } 
     } 
     return checkboxesArr; 
    }); 

/*return checkboxesArr = [ 
     {boxLabel: 'Yes', name: this.name, inputValue: 'Y'}, 
     {boxLabel: 'No', name: this.name, inputValue: 'N'}   
    ];*/ 
} 

如果我取消与2个复选框静态checkboxesArr和返回相反,它的工作原理,但它不工作与checkboxesArr与后端反应生成。

感谢

回答

3

Ajax调用是异步的,所以你可以add的项目,而不是:

getCheckboxes: function() { 
    Ext.Ajax.request({ 
     url: "data1.json", 
     scope: this, 
     success: function (resp_) { 
      var resp = Ext.JSON.decode(resp_.responseText); 

      var checkboxesArr = []; 

      if (resp.data) { 
       for (var i = 0; i < resp.data.length; i++) { 
        checkboxesArr.push({ 
         boxLabel: resp.data[i].label, 
         inputValue: resp.data[i].id 
        }); 
       } 
      } 
      this.add(checkboxesArr); 
     } 
    }); 
} 

工作例如:https://fiddle.sencha.com/#view/editor&fiddle/1lgc