无法将组件添加到dojox / layout / GridContainer

时间:2014-09-11 12:02:23

标签: javascript dojo

我创建了一个dojo gridcontainer,如下所示

 <body class='claro'>
 <button onclick="add()"> Add </button>
    <div id='border' data-dojo-type='dijit/layout/BorderContainer'>
        <div id='grid'></div>
    </div>
    <script type='text/javascript'>
    require(
        ['dojo/ready', 'dojo/parser', 'dojox/layout/GridContainer', 'dojox/widget/Portlet'],
        function (ready, Parser, GridContainer, Portlet) {
            ready(function () {
                Parser.parse().then(function () {
                    // create grid and put into border container
                    var grid = new GridContainer({nbZones:3, id: 'gc'}, 'grid');

                    // create cells and put into grid
                    for (var i = 0; i < 30; i++) {
                        grid.addChild(new Portlet({
                            closable:false,
                            content:'(' + Math.floor(i/3) + ', ' + (i%3) + ')'
                        }));
                    }
                    grid.startup();
                });
            });
        });
    </script>
</body>

它工作正常,但是当我在点击这个代码的添加按钮时添加一个孩子时很累

 <script type="text/javascript">
    function add()
    {
        var grid1 = dojo.byId("gc");     
        require(
             ['dojo/ready', 'dojo/parser', 'dojox/layout/GridContainer', 'dojox/widget/Portlet'],
             function (Parser, GridContainer, Portlet) 
             {
                 grid1.addChild(new Portlet({
                 closable:false,
                 content:''
             }));
        });
    };
    </script>

将给出错误 grid1.appendchild()不是函数

这有什么问题?

1 个答案:

答案 0 :(得分:0)

dojo.byId只能为你提供dom对象,但GridContainer是一个dijit / widget。试试这个:

<script type="text/javascript">
    function add()
    {
        require(
             ['dojo/ready', 'dojo/parser', 'dojox/layout/GridContainer', 'dojox/widget/Portlet', 'dijit/registry'],
             function (Parser, GridContainer, Portlet, dijit) 
             {
                 var grid1 = dijit.byId("gc");     
                 grid1.addChild(new Portlet({
                 closable:false,
                 content:''
             }));
        });
    };
</script>