Bugfix in listview filter
It seems that the search input is added on every refresh.
Here is a small bugfix for this.
Also, I added a bind on the submit button so that the keyboard disappear on submit.
See changes in green in the below code.
Cheers
- /*
- * jQuery Mobile Framework : "listview" filter extension
- * Copyright (c) jQuery Project
- * Dual licensed under the MIT or GPL Version 2 licenses.
- * http://jquery.org/license
- *
- * @Modified from the original version ( bugfix where filter bar was inserted on every refresh, also binded a submit event on the search input
- */
- (function($, undefined ) {
- $.mobile.listview.prototype.options.filter = false;
- $( "[data-role='listview']" ).live( "listviewcreate", function() {
- var list = $( this ),
- listview = list.data( "listview" );
- if ( !listview.options.filter ) {
- return;
- }
- var wrapper = $( "<form>", { "class": "ui-listview-filter ui-bar-c", "role": "search" } ),
- search = $( "<input>", {
- placeholder: "Filter results...",
- "data-type": "search"
- })
- .bind( "keyup change", function() {
- var val = this.value.toLowerCase(),
- listItems = list.children();
- listItems.show();
- if ( val ) {
- // This handles hiding regular rows without the text we search for
- // and any list dividers without regular rows shown under it
- var childItems = false,
- item;
- for (var i = listItems.length; i >= 0; i--) {
- item = $(listItems[i]);
- if (item.is("li[data-role=list-divider]")) {
- if (!childItems) {
- item.hide();
- }
- // New bucket!
- childItems = false;
- } else if (item.text().toLowerCase().indexOf( val ) === -1) {
- item.hide();
- } else {
- // There's a shown item in the bucket
- childItems = true;
- }
- }
- }
- })
- .appendTo( wrapper )
- .textinput();
-
- // Kill the keyboard on submit
- wrapper.submit(function(e){
- e.preventDefault();
- //kill the keyboard
- $( window.document.activeElement ).blur();
- return false;
- });
-
- // Remove old search input
- if (list.prevAll(".ui-listview-filter") && list.prevAll(".ui-listview-filter").remove) {
- list.prevAll(".ui-listview-filter").remove();
- }
- wrapper.insertBefore( list );
- });
- })( jQuery );