I have a listview with collapsible content. The filter automatically expand the collapsible content. This is done through javascript. After I reset the search filter, all items list remain expanded. How can I reset to initial state (collapsed)?
Here is the code: JsFiddle
<script>
$(document).on('pageinit', function() {
$("#iamalist").on('updatelayout', function() {
if (($('#iamalist li').length - $('#iamalist .ui-screen-hidden').length) < $('#iamalist li').length) {
$('#iamalist li').each(function() {
$(this).find('.ui-collapsible-content').removeClass('ui-collapsible-content-collapsed');
$(this).find('.ui-icon-plus').addClass('ui-icon-minus');
$(this).find('.ui-icon-plus').removeClass('ui-icon-plus');
});
}
});
});
</script>
<div data-role="content" id="iama">
<ul data-role="listview" id="iamalist" data-inset='true' data-filter="true" data-filter-placeholder="Search term is...">
<li>
<div data-role="collapsible" data-inset='false'>
<h3>Title</h3>
<p>eu</p>
</div>
</li>
<li>
<div data-role="collapsible" data-inset='false'>
<h3>Title</h3>
<p>tu</p>
</div>
</li>
<li>
<div data-role="collapsible" data-inset='false'>
<h3>Title</h3>
<p>voi</p>
</div>
</li>
<li>
<div data-role="collapsible" data-inset='false'>
<h3>Title</h3>
<p>Answer.</p>
</div>
</li>
<li>
<div data-role="collapsible" data-inset='false'>
<h3>Title</h3>
<p>Answer.</p>
</div>
</li>
</ul>
</div>
You can do this in one function. Instead of the updatelayout on the list, use the filterable widgit's Filter event. This event fires after every filter action. Within the handler check for the class .ui-screen-hidden
within the list. If none exist, collapse all collapsibles otherwise expand them.
The collapsible widget has methods for collapse and expand that you can use.
$("#iamalist").on("filterablefilter", function( event, ui ) {
if ($("#iamalist .ui-screen-hidden").length == 0){
$('#iamalist [data-role="collapsible"]').collapsible( "collapse" );
} else {
$('#iamalist [data-role="collapsible"]').collapsible( "expand" );
}
});
Updated FIDDLE