2 * "listview" filter extension
5 (function( $, undefined ) {
7 $.mobile.listview.prototype.options.filter = false;
8 $.mobile.listview.prototype.options.filterPlaceholder = "Filter items...";
9 $.mobile.listview.prototype.options.filterTheme = "c";
10 $.mobile.listview.prototype.options.filterCallback = function( text, searchValue ){
11 return text.toLowerCase().indexOf( searchValue ) === -1;
14 $( ":jqmData(role='listview')" ).live( "listviewcreate", function() {
17 listview = list.data( "listview" );
19 if ( !listview.options.filter ) {
23 var wrapper = $( "<form>", {
24 "class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
27 search = $( "<input>", {
28 placeholder: listview.options.filterPlaceholder
30 .attr( "data-" + $.mobile.ns + "type", "search" )
31 .jqmData( "lastval", "" )
32 .bind( "keyup change", function() {
35 val = this.value.toLowerCase(),
37 lastval = $this.jqmData( "lastval" ) + "",
42 // Change val as lastval for next execution
43 $this.jqmData( "lastval" , val );
44 change = val.substr( 0 , lastval.length - 1 ).replace( lastval , "" );
46 if ( val.length < lastval.length || change.length != ( val.length - lastval.length ) ) {
48 // Removed chars or pasted something totally different, check all items
49 listItems = list.children();
52 // Only chars added, not removed, only use visible subset
53 listItems = list.children( ":not(.ui-screen-hidden)" );
58 // This handles hiding regular rows without the text we search for
59 // and any list dividers without regular rows shown under it
61 for ( var i = listItems.length - 1; i >= 0; i-- ) {
62 item = $( listItems[ i ] );
63 itemtext = item.jqmData( "filtertext" ) || item.text();
65 if ( item.is( "li:jqmData(role=list-divider)" ) ) {
67 item.toggleClass( "ui-filter-hidequeue" , !childItems );
72 } else if ( listview.options.filterCallback( itemtext, val ) ) {
75 item.toggleClass( "ui-filter-hidequeue" , true );
78 // There's a shown item in the bucket
83 // Show items, not marked to be hidden
85 .filter( ":not(.ui-filter-hidequeue)" )
86 .toggleClass( "ui-screen-hidden", false );
88 // Hide items, marked to be hidden
90 .filter( ".ui-filter-hidequeue" )
91 .toggleClass( "ui-screen-hidden", true )
92 .toggleClass( "ui-filter-hidequeue", false );
96 //filtervalue is empty => show all
97 listItems.toggleClass( "ui-screen-hidden", false );
99 listview._refreshCorners();
104 if ( $( this ).jqmData( "inset" ) ) {
105 wrapper.addClass( "ui-listview-filter-inset" );
108 wrapper.bind( "submit", function() {
111 .insertBefore( list );