1. <input class="basic-search" type="text" data-filter-list=".fruit-list-1"/>
  2. <div id="list" style="overflow:auto;height:150px;border: 2px solid grey;">
  3. <ul class="list fruit-list-1">
  4. <li class="list" style="padding-left:10px"><a href="http://example.com" target="_blank">List Irem 1</a></li>
  5. </ul>
  6. <ul class="list fruit-list-1">
  7. <li class="list" style="padding-left:10px"><a href="http://example.com" target="_blank">List Item 2</a></li>
  8. </ul>
  9. </div>
  10. <script src="/js/jquery.filter-list.js"></script>
  11. <script>
  12. /**
  13. * jquery.filter-list.js
  14. * ------------------------------------------------------
  15. * Authors: Jeroen Ransijn
  16. * Company: Aan Zee
  17. * version: 1.0
  18. * Usage: $('[data-filter-list]').filterList();
  19. */
  20. ;(function (root, $, undefined) {
  21. "use strict";
  22. var pluginName = "filterList";
  23. var defaults = {
  24. isToggledWithClassNames: false,
  25. isFilteredOnKeyup: true,
  26. filterListAttribute: 'data-filter-list',
  27. selectorItem: 'li',
  28. classNameHidden: 'is-hidden',
  29. classNameShown: 'is-shown'
  30. };
  31. // Case-insensitive "contains"
  32. $.expr[':'].Contains = function(a,i,m){
  33. return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  34. };
  35. // The actual plugin constructor
  36. function Plugin( element, options ) {
  37. this.element = element;
  38. this.$el = $(element);
  39. this.options = $.extend( {}, defaults, options) ;
  40. this._defaults = defaults;
  41. this._name = pluginName;
  42. this.init();
  43. }
  44. Plugin.prototype = {
  45. init: function () {
  46. var listSelector = this.$el.attr( this.options['filterListAttribute'] );
  47. this.$list = $(listSelector);
  48. this.$items = this.$list.find( this.options['selectorItem'] );
  49. if (this.$list.length > 0) {
  50. this.$el
  51. .on('keyup', this._onKeyup.bind(this))
  52. .on('search', this.search.bind(this));
  53. }
  54. },
  55. reset: function () {
  56. if ( this.options['isToggledWithClassNames'] ) {
  57. this.$list.find( this.options['selectorItem'] )
  58. .removeClass(this.options['classNameHidden'] + ' ' + this.options['classNameShown']);
  59. } else {
  60. this.$list.find( this.options['selectorItem'] ).show();
  61. }
  62. this.$el.trigger('reset');
  63. },
  64. _onKeyup: function (e) {
  65. if ((e && e.which == 13 || this.options['isFilteredOnKeyup']) ) {
  66. this.search();
  67. }
  68. },
  69. search: function () {
  70. var searchValue = this.$el.val();
  71. if (searchValue.trim() === "") {
  72. return this.reset();
  73. }
  74. var matches = this.$items.filter(':Contains(' + searchValue + ')');
  75. var nonMatches = this.$items.not( matches );
  76. // Hide non matches
  77. if ( this.options['isToggledWithClassNames'] ) {
  78. nonMatches
  79. .addClass( this.options['classNameHidden'] )
  80. .removeClass(this.options['classNameShown']);
  81. } else {
  82. nonMatches.hide();
  83. }
  84. // Show matches
  85. if ( this.options['isToggledWithClassNames'] ) {
  86. matches
  87. .removeClass( this.options['classNameHidden'] )
  88. .addClass( this.options['classNameShown'] );
  89. } else {
  90. matches.show();
  91. }
  92. this.$el.trigger('filter', [searchValue || "", matches, nonMatches]);
  93. },
  94. getList: function () {
  95. return this.$list;
  96. }
  97. };
  98. // A really lightweight plugin wrapper around the constructor,
  99. // preventing against multiple instantiations
  100. $.fn[pluginName] = function ( options ) {
  101. return this.each(function () {
  102. if ( ! $.data(this, "plugin-" + pluginName)) {
  103. $.data(this, "plugin-" + pluginName,
  104. new Plugin( this, options ));
  105. }
  106. });
  107. };
  108. })(this, jQuery);
  109. $(function () {
  110. // Document ready
  111. $('.basic-search').filterList();
  112. $('.basic-search-on-enter').filterList({
  113. isFilteredOnKeyup: false
  114. });
  115. $('.trigger-enter').on('click', function (e) {
  116. e.preventDefault();
  117. $(this).parent().find('input').trigger('search');
  118. });
  119. $('.use-class-names').filterList({
  120. isToggledWithClassNames: true
  121. });
  122. });
  123. </script>