In order to load Accordion widget to your YUI instance, the easiest way is to let the Loader do it:
In this example, we will create markup for accordion itself and three items.
Since YUI 3.1, there is no need to define contentBox and boundingBox. You have to define only Widget's srcNode:
If your appication runs in quirks mode, you may have to set position, height and "overflow:hidden" for IE.
"yui3-accordion-item"
in the main item's div:........
When building Accordion items from markup, it is preferable to add the required markup for the header. In this way, if JavaScript is disabled, the headers will look in the same way, as if it would be enabled.
To do that, place inside standard Widget's header definition the following HTML elements with their classes.
This is optional, you can omit all the HTML in header definition (even the whole header) and AccordionItem will create it. Also, you can set only some of these HTML elements. What is missing, will be created when AccordionItem renders.
In the example below we set the label of the item - "Label 1" also.
There are three ways to set properties to AccordionItem from markup:
"yuiConfig"
custom attribute"data"
attribute. In this case you must prefix Accordion's config attributes with "data-".
If you are using yuiConfig
attribute, you can set every property, which AccordionItem supports. If you are using fake class names, you can set the following properties:
expanded
alwaysVisible
contentHeight
closable
If you are using "data" attribute, you can set the following properties:
label
expanded
alwaysVisible
contentHeight
closable
Note: expanded
, alwaysVisible
and closable
are boolean properties. If you are setting the values to true
by using data
attribute, you may do this in three ways:
"expanded"
property
In order to set "expanded"
property to true
(it is false by default), you may set fake class "yui3-accordion-item-expanded" in the main item's div:
or you may set "expanded" to true in yuiConfig
attribute:
........
or set "expanded" to true via data
attribute:
........
"alwaysVisible"
propertyUse "yui3-accordion-item-alwaysvisible" class name to make this item always visible:
........
or set {"alwaysVisible": true}
in yuiConfig
attribute:
........
or set alwaysVisible
to true via data
attribute:
........
"closable"
propertyUse "yui3-accordion-item-closable" class name in order to make the item closable:
........
or set "closable"
to true in yuiConfig
attribute:
........
or set "closable"
via data
attribute:
........
yuiConfig
attribute, or data
attribute:Using fake class names:
"yui3-accordion-item-contentheight-auto"
"yui3-accordion-item-contentheight-fixed-[pixels]"
, where "[pixels]" is number, for example 150"yui3-accordion-item-contentheight-stretch"
Using "yuiConfig"
custom attribute:
yuiConfig='{"contentHeight": {"method":"auto"}}'
yuiConfig='{"contentHeight": {"method":"fixed", "height":[pixels]}}'
, where "[pixels]" is number, for example 150yuiConfig='{"contentHeight": {"method":"stretch"}}'
Using "data"
attribute:
data-contentheight='auto'
data-contentheight='fixed-[pixels]'
, where "[pixels]" is number, for example 150data-contentheight='stretch'
yuiConfig
attribute:........
data
attribute:........
You can define it in header's markup:
or set it in "yuiConfig"
attribute:
or set it via "data"
attribute:
Step three: instantiate the Accordion
When YUI3 finishes loading all scripts, it will execute a function, passed as last argument of YUI "use" function. This is the right place you have to start creating instances, including those of the Accordion. You can instantiate Accordion as any other widget in YUI3:
var accordion = new Y.Accordion({ srcNode: "#acc1", useAnimation: true, collapseOthersOnExpand: true });Step four (optional): add/modify item params right before adding them to Accordion
If you subscribe to "beforeItemAdd" event, you will be able to overwrite all parameters, added from markup or to add new parameters:
accordion.on( "beforeItemAdd", function( attrs ){ var item, id; item = attrs.item; id = item.get( "id" ); if( id === "item1" ){ item.set( "contentHeight", { method: "fixed", height: 150 }); item.set( "alwaysVisible", true ); item.set( "closable", true ); } else if( id === "item2" ){ item.set( "label", "Label2" ); item.set( "contentHeight", { method: "stretch" }); item.set( "expanded", true ); } else if( id === "item3" ){ item.set( "label", "Label3, overwritten" ); item.set( "contentHeight", { method: "auto" }); item.set( "expanded", true ); } }, this );Step five: render the Accordion
The last step is to render the accordion. Render process will look for some items and if found, they will be created authomatically. In this example, there will be created three items.
accordion.render();See this example in new window.