项目作者: Josuerc026

项目描述 :
List Search/Filter Class with data-attribute driven API
高级语言: JavaScript
项目地址: git://github.com/Josuerc026/qSearch.git
创建时间: 2019-04-16T03:32:56Z
项目社区:https://github.com/Josuerc026/qSearch

开源协议:

下载


qSearch

List Search/Filter Class with data-attribute driven API

Settings

Setting Description
(Required) attrs Attributes/Column values the data list should gather.
(Optional ) afterAll(data, count) Gets called after every data event (searching, filtering, sorting) right before render.
  1. // A new list instance
  2. new QSearch('list_name_goes_here', {
  3. //
  4. attrs: ['example_one','example_two'],
  5. settings: {
  6. afterAll: function(data, count){
  7. // Happens after all events (search, filterOut, sorting) occur
  8. }
  9. }
  10. });

Methods available

Searching

Method Summary
.search( searchValue: string, afterSearch: cb) Triggers a search that returns items matching the provided value. afterSearch() is a callback that gets called after search, but before render
afterSearch(filteredElements, filteredCount) : callback fn() A callback that gets called after search, but before render
.filterOut( filterValue: string, keepState: boolean) Filters out items that match the provided value. Note: .search() returns items that match, while .filter() excludes them. KeepState stores the filtered list and saves it to use across all actions.
.restoreAll() Restores filtered list to its original state. Primarily to reset filtered lists that have keepState enabled.

Sorting

Method Summary
.sort(attribute: string, sortBy: string) Sort a given attribute by either DESC or ASC, passed in as the second parameter.

How to create a new searchable list instance

Instaniate a new QSearch list by provided the name of the list as the first argument and pass in the attributes in the second one.

  1. // A new list instance
  2. new QSearch('list_name_goes_here', {
  3. //
  4. attrs: ['example_one','example_two']
  5. });

A set is the overarching container that wraps around the searchable list. Set a name for the set using the data-qs-set attribute.

Specify a searchable list by setting the data-qs-list. A list doesn’t have to be an ordered/unordered HTML list. It can simply be a div that’s directly wrapping around the items that are going to be searched.

An attribute can be considered as a column in a table. It’s the data that’ll be searched for items, which can be considered rows of data. Attach data-qs-attr to attributes that’ll be searched.

  1. // HTML
  2. <div data-qs-set="list_one">
  3. <ul data-qs-list>
  4. <li>
  5. <span data-qs-attr="name">Example 1</span>
  6. <br/>
  7. <span data-qs-attr="date">2019</span>
  8. </li>
  9. <li>
  10. <span data-qs-attr="name">Example 2</span>
  11. <br/>
  12. <span data-qs-attr="date">2017</span>
  13. </li>
  14. <li>
  15. <span data-qs-attr="name">Example 3</span>
  16. <br/>
  17. <span data-qs-attr="date">2008</span>
  18. </li>
  19. </ul>
  20. </div>

Basic Example

  1. // A new list instance
  2. let list = new QSearch('list_one', {
  3. attrs: ['name','date']
  4. });
  5. let input = document.querySelector('.search');
  6. input.addEventListener('keyup', function(){
  7. // Search takes in a value to compare
  8. // Callback is executed right before the filtered list is rendered on the page
  9. list.search(this.value, (elms, count) => {
  10. console.log(`Search occurred! There are ${elms.length} items found`);
  11. if(elms.length === count){
  12. elms.forEach(item => item.classList.remove('new-item'));
  13. }else{
  14. elms.forEach(item => item.classList.add('new-item'));
  15. }
  16. });
  17. });
  1. // HTML
  2. // Search bar
  3. <input class="search">
  4. //List of random name and dates
  5. <div data-qs-set="list_one">
  6. <ul data-qs-list>
  7. <li>
  8. <span data-qs-attr="name">Avengers: Endgame</span>
  9. <br/>
  10. <span data-qs-attr="date">2019</span>
  11. </li>
  12. <li>
  13. <span data-qs-attr="name">Spiderman: Homecoming</span>
  14. <br/>
  15. <span data-qs-attr="date">2017</span>
  16. </li>
  17. <li>
  18. <span data-qs-attr="name">Iron-man</span>
  19. <br/>
  20. <span data-qs-attr="date">2008</span>
  21. </li>
  22. <li>
  23. <span data-qs-attr="name">Titanic</span>
  24. <br/>
  25. <span data-qs-attr="date">1999</span>
  26. </li>
  27. <li>
  28. <span data-qs-attr="name">My Birthday</span>
  29. <br/>
  30. <span data-qs-attr="date">1995</span>
  31. </li>
  32. <li>
  33. <span data-qs-attr="name">JavaScript Created</span>
  34. <br/>
  35. <span data-qs-attr="date">1995</span>
  36. </li>
  37. </ul>
  38. </div>