Thursday, April 25, 2013

Jquery Mobile accordions - Collapsible listviews

    "How do I add a collapsible list to my Jquery Mobile application?" If this is the question that's bothering you, then JQM has a solution to this and a pretty simple one. You can style the listviews as collapsibles very easily and you can have individual collapsible lists or a set of collapsible lists (accordions). We will take a look at how we can achieve both of these.

    Let us begin with the individual collapsible listviews. You can achieve a collapsible by merely adding a div with data-attribute data-role="collapsible". This will style the div as a collapsible block with a default "plus" icon positioned to the left end of the list. To add a heading to the collapsible list add a heading tag (h1-h6) and you will have a left-aligned title for your collapsible header. After you have added the header, you can now have any content that you wish to include in the collapsible.

    Let's take a look at the example below to better understand the collapsible list implementation. We have 3 individual collapsible lists with different data-theme attributes. You can have a list open by default by including data-collapsed="false" attribute. By adding the data-content-theme attribute, you will get a border to the collapsible container.


    Now that we have created individual collapsible lists, we can club them together to create a collapsible set or an accordion as it is more commonly know. To do this add a div with data-role="collapsible-set" attribute around the collapsible divs and you will get an accordion. In the collapsible-set or the accordion, only one collapsible list would be open at a time. Refer to the following example to understand the implementation of a collapsible-set or an accordion.


    Hope you have followed how to create a collapsible list and a collapsible set. Do drop a comment in case you have liked this post or have some suggestions on the same. Let me know in case you have any problems and I would be happy to help! In the next post we will see some of the various issues that I came across on the Jquery Mobile official forum and their resolutions.

If this post has helped you, leave a comment or show your love by liking the Spatial Unlimited Facebook page. You could even consider buying me a coffe! Till next time; happy coding!