McPhelpsius
McPhelpsius

Reputation: 253

Dynamically created collapsible-set in jQuery Mobile

Okay, once i see the answer to this, I will feel stupid. I'm certain of that.

I've created this exactly the way I want to before, but I am refactoring my code for a new version right now. I am trying to dynamically create collapsible sets in jQuery Mobile, but my html does not render right.

  <div data-role="header">
        <h2>Playground</h2>
    </div>
    <div data-role="content">
        <div data-role="button" id="addprimary" data-inline="true">Add 5</div>
        <div data-role="collapsible">             
            <h4>Collapsible</h4> 
            <form id="makecollapsible">
            </form>
        </div>
    </div>
    <div data-role="footer">
        <h4>Please, no applause</h4>
    </div>
</div>

<script>
$('#addprimary').on('click', function () {
    Markup.Collapsible();
});

var Markup = new Object();


Markup.Collapsible = function () {

$('#makecollapsible')
.append($('<div>')
    .attr({ 'data-role': 'collapsible-set', 'id': 'primary' })
 );
for (i = 0; i < 5; i++) {
    ($('<div>')
         .attr({ 'data-role': 'collapsible', 'data-content-theme': 'c', 
              'data-collapsed': 'true' })
         .html('<h4>' + i +'</h4>'))
    .appendTo('#primary');
   }
}
</script>

Could somebody please take a look at this http://jsfiddle.net/c2jLY/ and tell me what I have wrong? My <div>s with data-role='collapsible' are not rendering as collapsibles, which is also having an effect on the HTML I am trying to put in them later on.

Help is appreciated, thanks!

Upvotes: 2

Views: 6105

Answers (2)

Kir Wong
Kir Wong

Reputation: 1

what i show here is a simple one but working:

<script type="text/javascript">
//dynamically make 10 collapsible items and append them to the collapsible-set
var jj = "SUPER item added..";

$('[data-role="content"]').append('<div id="set" data-role="collapsible-set"></div>');
var count;
	for (count=0; count < 10; count++) {  // div id should be from id='c0' to 'c9'
		$("#set").append('<div id="c' + count + '" data-role="collapsible">');
		$("#c" + count.toString()).append('<h3>Adding element_' + count +'</h3>');
		$("#c" + count.toString()).append(jj + 'count ' + count + '</div>');

	}

// either one is tested working below:
//		$('[data-role="content"]').trigger('create');
		$( "#set" ).collapsibleset( "refresh" );

</script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<link href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
	<!------------------------page 1 ListView template-->
		<div data-role="page"  id="page01">
		<div data-role="header" data-theme="b" data-position="fixed">			
			<h2>-- DEMO -- &nbsp;&nbsp;</h2>
		</div>
		<div data-role="content"  id="content">
  
    </div>
</body>

Upvotes: 0

Omar
Omar

Reputation: 31732

Inside Markup.Collapsible function and at the end of it, add the below. For collapsible-set, you need to tell jQM that you're enhancing a .collapsibleset() and combine it with .trigger('create').

$('#makecollapsible').collapsibleset().trigger('create');

Demo


I forgot to mention that when appending items dynamically, call enhancement methods on parent element; doing so, will enhance children elements. Thus, you don't need to use .collapsible().trigger('create') for each collapsible appended.

Upvotes: 5

Related Questions