1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Extends the listview to add a search box to filter lists
3 //>>label: Listview: Filter
7 define( [ "jquery", "./jquery.mobile.listview", "./jquery.mobile.forms.textinput" ], function( $ ) {
8 //>>excludeEnd("jqmBuildExclude");
9 (function( $, undefined ) {
11 $.mobile.listview.prototype.options.filter = false;
12 $.mobile.listview.prototype.options.filterPlaceholder = "Filter items...";
13 $.mobile.listview.prototype.options.filterTheme = "c";
14 $.mobile.listview.prototype.options.filterCallback = function( text, searchValue ){
15 return text.toLowerCase().indexOf( searchValue ) === -1;
18 $( document ).delegate( ":jqmData(role='listview')", "listviewcreate", function() {
21 listview = list.data( "listview" );
23 if ( !listview.options.filter ) {
27 var wrapper = $( "<form>", {
28 "class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
31 search = $( "<input>", {
32 placeholder: listview.options.filterPlaceholder
34 .attr( "data-" + $.mobile.ns + "type", "search" )
35 .jqmData( "lastval", "" )
36 .bind( "keyup change", function() {
39 val = this.value.toLowerCase(),
41 lastval = $this.jqmData( "lastval" ) + "",
46 // Change val as lastval for next execution
47 $this.jqmData( "lastval" , val );
48 if ( val.length < lastval.length || val.indexOf(lastval) !== 0 ) {
50 // Removed chars or pasted something totally different, check all items
51 listItems = list.children();
54 // Only chars added, not removed, only use visible subset
55 listItems = list.children( ":not(.ui-screen-hidden)" );
60 // This handles hiding regular rows without the text we search for
61 // and any list dividers without regular rows shown under it
63 for ( var i = listItems.length - 1; i >= 0; i-- ) {
64 item = $( listItems[ i ] );
65 itemtext = item.jqmData( "filtertext" ) || item.text();
67 if ( item.is( "li:jqmData(role=list-divider)" ) ) {
69 item.toggleClass( "ui-filter-hidequeue" , !childItems );
74 } else if ( listview.options.filterCallback( itemtext, val ) ) {
77 item.toggleClass( "ui-filter-hidequeue" , true );
80 // There's a shown item in the bucket
85 // Show items, not marked to be hidden
87 .filter( ":not(.ui-filter-hidequeue)" )
88 .toggleClass( "ui-screen-hidden", false );
90 // Hide items, marked to be hidden
92 .filter( ".ui-filter-hidequeue" )
93 .toggleClass( "ui-screen-hidden", true )
94 .toggleClass( "ui-filter-hidequeue", false );
98 //filtervalue is empty => show all
99 listItems.toggleClass( "ui-screen-hidden", false );
101 listview._refreshCorners();
106 if ( listview.options.inset ) {
107 wrapper.addClass( "ui-listview-filter-inset" );
110 wrapper.bind( "submit", function() {
113 .insertBefore( list );
117 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
119 //>>excludeEnd("jqmBuildExclude");