Advertisement
NewboO

jQuery Mobile, extended search filter

Oct 17th, 2012
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $.mobile.listview.prototype.options.xFilter = false;
  2. $.mobile.listview.prototype.options.xFilterId = false;
  3.  
  4. $( document ).delegate( ":jqmData(role='listview')", "listviewcreate", function() {
  5.  
  6.     var listview = $(this).data( "listview" );
  7.  
  8.     if ( !listview.options.xFilter || !$(this).attr('id') ) {
  9.         return;
  10.     }
  11.    
  12.     var list = $(this).add('ul:jqmData(x-filter-id='+$(this).attr('id')+')');
  13.  
  14.     if ( listview.options.filterReveal ) {
  15.         list.children().addClass( "ui-screen-hidden" );
  16.     }
  17.  
  18.     var wrapper = $( "<form>", {
  19.             "class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
  20.             "role": "search"
  21.         }),
  22.         search = $( "<input>", {
  23.             placeholder: listview.options.filterPlaceholder
  24.         })
  25.         .attr( "data-" + $.mobile.ns + "type", "search" )
  26.         .jqmData( "lastval", "" )
  27.         .bind( "keyup change", function() {
  28.  
  29.             var $this = $( this ),
  30.                 val = this.value.toLowerCase(),
  31.                 listItems = null,
  32.                 lastval = $this.jqmData( "lastval" ) + "",
  33.                 childItems = false,
  34.                 itemtext = "",
  35.                 item,
  36.                 // Check if a custom filter callback applies
  37.                 isCustomFilterCallback = listview.options.filterCallback !== $.mobile.listview.prototype.options.filterCallback;
  38.  
  39.             listview._trigger( "beforefilter", "beforefilter", { input: this } );
  40.  
  41.             // Change val as lastval for next execution
  42.             $this.jqmData( "lastval" , val );
  43.             if ( isCustomFilterCallback || val.length < lastval.length || val.indexOf( lastval ) !== 0 ) {
  44.  
  45.                 // Custom filter callback applies or removed chars or pasted something totally different, check all items
  46.                 listItems = list.children();
  47.             } else {
  48.  
  49.                 // Only chars added, not removed, only use visible subset
  50.                 listItems = list.children( ":not(.ui-screen-hidden)" );
  51.  
  52.                 if ( !listItems.length && listview.options.filterReveal ) {
  53.                     listItems = list.children( ".ui-screen-hidden" );
  54.                 }
  55.             }
  56.  
  57.             if ( val ) {
  58.  
  59.                 // This handles hiding regular rows without the text we search for
  60.                 // and any list dividers without regular rows shown under it
  61.  
  62.                 for ( var i = listItems.length - 1; i >= 0; i-- ) {
  63.                     item = $( listItems[ i ] );
  64.                     itemtext = item.jqmData( "filtertext" ) || item.text();
  65.  
  66.                     if ( item.is( "li:jqmData(role=list-divider)" ) ) {
  67.  
  68.                         item.toggleClass( "ui-filter-hidequeue" , !childItems );
  69.  
  70.                         // New bucket!
  71.                         childItems = false;
  72.  
  73.                     } else if ( listview.options.filterCallback( itemtext, val, item ) ) {
  74.  
  75.                         //mark to be hidden
  76.                         item.toggleClass( "ui-filter-hidequeue" , true );
  77.                     } else {
  78.  
  79.                         // There's a shown item in the bucket
  80.                         childItems = true;
  81.                     }
  82.                 }
  83.  
  84.                 // Show items, not marked to be hidden
  85.                 listItems
  86.                     .filter( ":not(.ui-filter-hidequeue)" )
  87.                     .toggleClass( "ui-screen-hidden", false );
  88.  
  89.                 // Hide items, marked to be hidden
  90.                 listItems
  91.                     .filter( ".ui-filter-hidequeue" )
  92.                     .toggleClass( "ui-screen-hidden", true )
  93.                     .toggleClass( "ui-filter-hidequeue", false );
  94.  
  95.             } else {
  96.  
  97.                 //filtervalue is empty => show all
  98.                 listItems.toggleClass( "ui-screen-hidden", !!listview.options.filterReveal );
  99.             }
  100.             listview._refreshCorners();
  101.         })
  102.         .appendTo( wrapper )
  103.         .textinput();
  104.  
  105.     if ( listview.options.inset ) {
  106.         wrapper.addClass( "ui-listview-filter-inset" );
  107.     }
  108.  
  109.     wrapper.bind( "submit", function() {
  110.         return false;
  111.     })
  112.     .insertBefore( $(this) );
  113. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement