问题
I am trying to add a new AccordionPane to a existing container, but for the life of me I can't get it to work.
Is anyone able to suggest where I am going wrong?
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js" djConfig="parseOnLoad: true"> </script>
<script type="text/javascript">
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.AccordionContainer");
</script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.4.0/dijit/themes/tundra/tundra.css">
<script type="text/javascript">
function AddNewPane() {
var accordPane = new dijit.layout.AccordionPane({"title": "test", "content":"hello"});
dijit.layout.AccordionContainer("myacc").addChild(accordPane);
accordPane.startup();
//select the new Pane
accordPane.selected = true;
}
</script>
</head>
<body>
<button type="button" onclick="AddNewPane();" >Add</button>
<div dojoType="dijit.layout.AccordionContainer" id="myacc" class="tundra" >
<div dojoType="dijit.layout.AccordionPane" title="Origional Acc 1" >
Testing One
</div>
<div dojoType="dijit.layout.AccordionPane" title="Origional Acc 2" >
Testing Two
</div>
</div>
<script>
document.getElementById("myacc").style.width = '200px';
document.getElementById("myacc").style.height = '200px';
</script>
</body>
</html>
回答1:
Got it working, thanks.
function Testing() {
var accordion = dijit.byId("myacc");
var d = new dijit.layout.AccordionPane({id:'newpane', title:'hello', content: 'testing'});
accordion.addChild(d, 0);
dijit.byId('myacc').selectChild(dijit.byId('newpane'));
}
回答2:
As the original poster said, to add the new AccordionPane to the TOP of the AccordionContainer, use 0 for the insertIndex. If you'd rather add the new AccordionPanel to the BOTTOM of the AccordionContainer, just remove the insertIndex from the .addChild as seen below:
function Testing() {
var accordion = dijit.byId("myacc");
var d = new dijit.layout.AccordionPane({id:'newpane', title:'hello', content: 'testing'});
accordion.addChild(d);
accordion.selectChild(dijit.byId('newpane'));
}
Also, in my case I was wanting to add a new AccordionPane to the AccordionContainer with content loaded from another page on the same server. Code below for anyone who finds this in the future wanting to do the same:
function Testing() {
var accordion = dijit.byId("myacc");
var d = new dijit.layout.AccordionPane({id:'newpane', title:'hello', href: "location/of/page.php", preload: true});
accordion.addChild(d);
accordion.selectChild(dijit.byId('newpane'));
}
Also, if you want to enable animation when selecting the child, add true to the animate property:
accordion.selectChild(dijit.byId('newpane'), true);
来源:https://stackoverflow.com/questions/3562594/programmatically-add-a-new-dojo-accordionpane-to-existing-accordioncontainer