高级筛选条件搜索数据 结构化筛选

  • 源码大小:202.95KB
  • 所需积分:1积分
  • 源码编号:19JP-3760
  • 浏览次数:835次
  • 最后更新:2023年07月19日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

如果你正在寻找一个过滤器插件,让用户能够使用各种过滤器组合搜索数据中的内容,那么你就找到了合适的地方。

Structured Filter是一个高级的jQuery&jQuery UI过滤插件,可用于使用结构化搜索条件过滤复杂数据,如等于、大于、小于、是/否、包含等。

默认筛选条件:

  • 布尔值:是(1),否(0)
  • 日期:on (eq), not on (ne), after (gt), before (lt), between (bw), not between (nbw), is empty (null), is not empty (nn)
  • 列表:任意(in),等于(eq)
  • 编号:=(当量),!=(ne),>(gt),<(lt),为空(null),不为空(nn)
  • 文本:equals(eq),not equal(ne),以(sw)开头,包含(ct),不包含(nct),结束于(fw),为空(null),不为空(nn)
  • 时间:at (eq), not at (ne), after (gt), before (lt), between (bw), not between (nbw), is empty (null), is not empty (nn)

如何使用它:

1.安装结构化过滤器插件并将其导入到文档中。

  1. # NPM
  2. $ npm i structured-filter --save
  1. <!-- jQuery & jQuery UI -->
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <link href="/path/to/cdn/jquery-ui.min.css" rel="stylesheet" />
  4. <script src="/path/to/cdn/jquery-ui.min.js"></script>
  5. <!-- jQuery Structured Filter -->
  6. <link href="/path/to/css/structured-filter.css" rel="stylesheet" />
  7. <script src="/path/to/js/structured-filter.js"></script>

2.按如下方式准备您的数据:

  1. // sample data for Structured-Filter
  2. var contacts=[
  3. { type:"text", id:"Lastname", label:"Lastname"},
  4. { type:"text", id:"Firstname", label:"Firstname"},
  5. { type:"boolean", id:"active", label:"Is active"},
  6. { type:"number", id:"age", label:"Age"},
  7. {type:"list", id:"category", label:"Category",
  8. list:[
  9. {id:'1', label:"Family"},
  10. {id:'2', label:"Friends"},
  11. {id:'3', label:"Business"},
  12. {id:'4', label:"Acquaintances"},
  13. {id:'5', label:"Other"}
  14. ]
  15. },
  16. {type:"date", id:"bday", label:"Birthday"},
  17. {type:"text", id:"phone", label:"Phone"},
  18. {type:"text", id:"cell", label:"Mobile"},
  19. {type:"text", id:"Address1", label:"Address"},
  20. {type:"text", id:"City", label:"City"},
  21. {type:"list", id:"State", label:"State",
  22. list:[
  23. {id:"AL", label:"Alabama"},
  24. {id:"AK", label:"Alaska"},
  25. {id:"AZ", label:"Arizona"}
  26. ]
  27. },
  28. {type:"text", id:"Zip", label:"Zip"},
  29. {type:"list", id:"Country", label:"Country",
  30. list:[
  31. {label: 'Afghanistan', id: 'AF'},
  32. {label: 'Åland Islands', id: 'AX'},
  33. // ...
  34. ]
  35. }
  36. ]

3.初始化插件以在页面上生成过滤器UI。就是这样。

  1. <div id="myFilter"></div>
  1. $('#myFilter').structFilter({
  2. fields: contacts
  3. })

4.将自定义筛选条件添加到筛选UI中。每个条件必须包含领域,操作人员价值属性如下:

  1. $('#myFilter').structFilter("addCondition", {
  2. "field": {
  3. "label": "Lastname",
  4. "value": "Lastname"
  5. },
  6. "operator": {
  7. "label": "contains",
  8. "value": "ct"
  9. },
  10. "value": {
  11. "label": "\"N\"",
  12. "value": "N"
  13. }
  14. });

5.自定义插件的所有可用选项。

  1. $('#myFilter').structFilter({
  2.  
  3. // data to be filterable
  4. fields: [],
  5.  
  6. // date format
  7. dateFormat: 'mm/dd/yy',
  8.  
  9. // highlights the last added or modified filter
  10. highlight: true,
  11.  
  12. // show/hide button labels
  13. buttonLabels: false,
  14.  
  15. // show/hide submit button
  16. submitButton: false,
  17.  
  18. // provides hidden fields with the conditions' values to be submitted with the form
  19. submitReady: false,
  20.  
  21. // disables operators from conditions
  22. disableOperators: false
  23. })

6.API方法。

  1. // remove all filters
  2. $('#myFilter').structFilter("clear");
  3.  
  4. // get the number of filters
  5. $('#myFilter').structFilter("length");
  6.  
  7. // remove a condition (zero indexed)
  8. $('#myFilter').structFilter("removeCondition", 0);
  9.  
  10. // get or set the filter
  11. $('#myFilter').structFilter("val");
  12. $('#myFilter').structFilter("val", data);
  13. $("#myFilter").structFilter("valText");
  14. $("#myFilter").structFilter("valUrl");

7.事件处理程序。

  1. $('#myFilter').on("change.search", function(event){
  2. // after conditions are changed
  3. });
  4.  
  5. $('#myFilter').on("submit.search", function(event){
  6. // on submit
  7. });

预览截图