TruMan1
TruMan1

Reputation: 36078

Javascript API for jQuery Mobile components?

I am trying to attach events that would manipulate JQM components, but once I have a reference to the component, I do not know what actions I can apply to it. For example, with the new collapsible listview", how would I collapse all the items with a button click? Here is the demo of the collapsible listview: http://jquerymobile.com/demos/1.2.0-alpha.1/docs/lists/lists-collapsible.html

Upvotes: 0

Views: 481

Answers (1)

Jasper
Jasper

Reputation: 75993

For collapsible's you can utilize the expand and collapse events:

$('.ui-collapsible').trigger('expand');

For example you can collapse/expand all the collapsible widgets in the DOM based on their current state like this:

//attach the code to an event (click on a link)
$('a').on('click', function () {

    //cache all the widgets
    var $all       = $('.ui-collapsible');

    //iterate through each widget
    $.each($all, function () {

        //if the widget has the "collapsed" class, then expand it
        if ($(this).hasClass('ui-collapsible-collapsed')) {
            $(this).trigger('expand');
        } else {

          //otherwise collapse it
          $(this).trigger('collapse');  
        }
    });
});​

Here is a demo: http://jsfiddle.net/HLEss/1/

Here is an example collapsible widget's HTML to verify that you can select by the "collapsed" class:

<div data-role="collapsible" class="ui-collapsible ui-collapsible-collapsed">
    <h3 class="ui-collapsible-heading ui-collapsible-heading-collapsed">
        <a href="#" class="ui-collapsible-heading-toggle ui-btn ui-btn-up-c ui-fullsize ui-btn-icon-left ui-corner-top ui-corner-bottom ui-btn-active ui-btn-hover-null ui-btn-down-null" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="plus" data-iconpos="left" data-theme="null" data-mini="false">
            <span class="ui-btn-inner ui-corner-top ui-corner-bottom">
                <span class="ui-btn-text">Zero
                    <span class="ui-collapsible-heading-status"> click to expand contents</span>
                </span>
                <span class="ui-icon ui-icon-plus ui-icon-shadow">&nbsp;</span>
            </span>
        </a>
    </h3>
    <div class="ui-collapsible-content ui-collapsible-content-collapsed" aria-hidden="true">
        <p>Woohoo!</p>
    </div>
</div>

I hope that helps.

Update

Here is the documentation for these events (sparse as it is): http://jquerymobile.com/demos/1.1.1/docs/content/content-collapsible-events.html

Upvotes: 1

Related Questions