Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Created November 26, 2012 03:29
Show Gist options
  • Save elijahmanor/4146463 to your computer and use it in GitHub Desktop.
Save elijahmanor/4146463 to your computer and use it in GitHub Desktop.
jQuery Mobile Smart Filtering
$.mobile.listview.prototype.options.filterCallback = function( test, searchValue, item ) {
var list = item.closest( "[data-role='listview']" ),
selector, matches, compare, caseSensitive;
selector = list.data( "selector" );
if ( selector ) {
text = item.find( selector ).text();
} else {
text = item.jqmData( "filtertext" ) || item.text();
}
text = $.trim( text );
caseSensitive = list.data( "caseSensitive" ) || false;
if ( !caseSensitive ) {
searchValue = searchValue.toLowerCase();
text = text.toLowerCase();
}
compare = list.data( "compare" ) || "*=";
if ( compare === "*=" ) {
matches = text.indexOf( searchValue ) >= 0;
} else if ( compare === "^=" ) {
matches = text.indexOf( searchValue ) === 0;
} else if ( compare === "$=" ) {
matches = text.lastIndexOf( searchValue ) === ( text.length - searchValue.length );
}
return !matches;
};
<ul id="members" data-role="listview" data-filter="true" data-selector="h3" data-compare="^=">
<li>
<a href="index.html">
<h3>Rey Bango</h3>
<p><strong>Evangelist</strong></p>
<p>Florida, United States</p>
</a>
</li>
<!-- ... -->
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment