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