Bugfix in listview filter

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

  1. /*
  2. * jQuery Mobile Framework : "listview" filter extension
  3. * Copyright (c) jQuery Project
  4. * Dual licensed under the MIT or GPL Version 2 licenses.
  5. * http://jquery.org/license
  6. * @Modified from the original version ( bugfix where filter bar was inserted on every refresh, also binded a submit event on the search input
  7. */
  8. (function($, undefined ) {

  9. $.mobile.listview.prototype.options.filter = false;

  10. $( "[data-role='listview']" ).live( "listviewcreate", function() {
  11. var list = $( this ),
  12. listview = list.data( "listview" );
  13. if ( !listview.options.filter ) {
  14. return;
  15. }

  16. var wrapper = $( "<form>", { "class": "ui-listview-filter ui-bar-c", "role": "search" } ),

  17. search = $( "<input>", {
  18. placeholder: "Filter results...",
  19. "data-type": "search"
  20. })
  21. .bind( "keyup change", function() {
  22. var val = this.value.toLowerCase(),
  23. listItems = list.children();
  24. listItems.show();
  25. if ( val ) {
  26. // This handles hiding regular rows without the text we search for
  27. // and any list dividers without regular rows shown under it
  28. var childItems = false,
  29. item;

  30. for (var i = listItems.length; i >= 0; i--) {
  31. item = $(listItems[i]);
  32. if (item.is("li[data-role=list-divider]")) {
  33. if (!childItems) {
  34. item.hide();
  35. }
  36. // New bucket!
  37. childItems = false;
  38. } else if (item.text().toLowerCase().indexOf( val ) === -1) {
  39. item.hide();
  40. } else {
  41. // There's a shown item in the bucket
  42. childItems = true;
  43. }
  44. }
  45. }
  46. })
  47. .appendTo( wrapper )
  48. .textinput();
  49. // Kill the keyboard on submit
  50. wrapper.submit(function(e){
  51. e.preventDefault();
  52. //kill the keyboard
  53. $( window.document.activeElement ).blur();
  54. return false;
  55. });
  56. // Remove old search input
  57. if (list.prevAll(".ui-listview-filter") && list.prevAll(".ui-listview-filter").remove) {
  58. list.prevAll(".ui-listview-filter").remove(); 
  59. }
  60. wrapper.insertBefore( list );
  61. });

  62. })( jQuery );