Extjs自动生成控件的问题各位 我有一个问题想请教大家。当我直接写控件在一个panel中 就可以显示
代码如下:        Ext.onReady(function() {
            var button=new Ext.Button({text: 'MyButton'});            var p = new Ext.Panel({
                title: 'My Panel',
                collapsible: true,
                renderTo: 'container',
                width: 400,
                html: '<div>15995815158</div>'
           ,
                items: [{
                    xtype: 'button',
                    text: 'MyButton'}]
            });
        });但是我试图对下面上边代码中items的值使用json内容时候,总出现控件不能显示的情况,请问怎么解决
Js代码:
        var loadData = function(params) {
            Ext.Ajax.request({
                url: 'Handlers/DTest.ashx',
                success: function(response, options) {
                    debugger;
                    var responseText = Ext.util.JSON.decode(response.responseText);
                    //读取数据,存入变量params中
                    params = responseText;
                }
            });
            return params;
        }        Ext.onReady(function() {
            var button=new Ext.Button({text: 'MyButton'});            var p = new Ext.Panel({
                title: 'My Panel',
                collapsible: true,
                renderTo: 'container',
                width: 400,
                html: '<div>15995815158</div>'
           ,
                items: loadData
            });
        });后台json:Response.Write("[{\"xtype\": \"button\",\"text\": \"MyButton\"}]");我反复修改后台json的格式 都没有出现button,不知道是json格式错误还是因为我的代码有问题 谢谢大家  国庆快乐

解决方案 »

  1.   

    <script> 
        Ext.onReady(function(){   
        Ext.QuickTips.init();
        Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
     
        var jsonData = {"city":"Beijing","street":" Chaoyang Road ","postcode":100025} var btn=  new Ext.Button({
                renderTo:Ext.getBody(),
                text:jsonData.city ,
    presswd:true,
                listeners:{
    "click":function()
    {
      Ext.Msg.alert("json value ",jsonData.city + '-'+jsonData.street +'-'+jsonData.postcode);
    }

            })
                 
     var p = new Ext.Panel({
      title: 'My test  Panel',
      collapsible: true,
      renderTo: 'container', //<div id='container'/>
      width: 400,
      html:'15995815158',
      item:btn
      //bbar : [  btn ]   //或者用这个,把上面的item注释掉
      }); 
       });
    </script>
      

  2.   

    自己测试下loadData是否能用loadData[index].text访问,即确定loadData是否数组。
      

  3.   

    loadData只是函数对象 怎么能作为item构造呢
    items: loadData()试试
    不知道这样行不行 因为ajax是异步的 
    如果不行 可以在var p 实例化之前先得到loadData()的返回值 然后直接赋值
    本人臆测 错的地方不要笑话呀
      

  4.   

    是滴,还有异步问题要考虑,建议在ajax完成后用add和doLayout()方法添加items
      

  5.   

    var loadData = function(callback)
    {
    Ext.Ajax.request({
    url : 'Handlers/DTest.ashx',
    success : function(response, options)
    {
    var responseText = Ext.util.JSON.decode(response.responseText);
    // 读取数据,存入变量params中
    callback(responseText)
    }
    });
    }Ext.onReady(function()
    {
    loadData(function(res)
    {
    var button = new Ext.Button({
    text : 'MyButton'
    }); var p = new Ext.Panel({
    title : 'My Panel',
    collapsible : true,
    renderTo : 'container',
    width : 400,
    html : '<div>15995815158</div>',
    items : res
    });
    })
    });