jQuery 全功能自动补全库 Typeahead.js

  • 源码大小:249.51KB
  • 所需积分:1积分
  • 源码编号:19JP-3397
  • 浏览次数:724次
  • 最后更新:2023年06月09日
  • 所属栏目:表单
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

只是另一个jQuery typeahead插件,它通过灵活的API在输入字段上提供完全可定制和启用AJAX的自动完成功能。

主要功能:

  • 跨浏览器。
  • 大量的自定义选项和回调功能。
  • 支持跨域JSONP。
  • 正在加载动画。
  • 突出显示搜索字符串。
  • 自定义模板。
  • 键盘交互。
  • 支持下拉筛选器的结果组。
  • “localStorage”或“sessionStorage”缓存支持。
  • 可排序的结果。

通过程序包管理器安装:

  1. # NPM
  2. $ npm install jquery-typeahead
  3.  
  4. # Bower
  5. $ bower install jquery-typeahead

基本用法:

1.首先,您需要在html文档中加载以下JavaScript和CSS文件。

  1. <link rel="stylesheet" href="jquery.typeahead.css">
  2. <script src="jquery.min.js"></script>
  3. <script src="jquery.typeahead.js"></script>

2.然后创建一个输入字段:

  1. <input class="js-typeahead"
  2. name="q"
  3. type="search"
  4. autofocus
  5. autocomplete="off">

3.最后,调用输入字段上的main函数,并指定要获取的数据源。

  1. $('.js-typeahead').typeahead({
  2. source: {
  3. // local data
  4. groupName: {
  5. data: [ {...}, {...} ]
  6. }
  7. }
  8. });
  9.  
  10. // or
  11. $('.js-typeahead').typeahead({
  12. source: {
  13. // remote data
  14. groupName: {
  15. ajax: {
  16. url: "..."
  17. }
  18. }
  19. }
  20. });

4.使用以下选项配置typeahead/autocomplete插件。

  1. // *RECOMMENDED*, jQuery selector to reach Typeahead's input for initialization
  2. input: null,
  3.  
  4. // Accepts 0 to search on focus, minimum character length to perform a search
  5. minLength: 2,
  6.  
  7. // False as "Infinity" will not put character length restriction for searching results
  8. maxLength: false,
  9.  
  10. // Accepts 0 / false as "Infinity" meaning all the results will be displayed
  11. maxItem: 8,
  12.  
  13. // When true, Typeahead will get a new dataset from the source option on every key press
  14. dynamic: false,
  15.  
  16. // delay in ms when dynamic option is set to true
  17. delay: 300,
  18.  
  19. // "asc" or "desc" to sort results
  20. order: null,
  21.  
  22. // Set to true to match items starting from their first character
  23. offset: false,
  24.  
  25. // Added support for excessive "space" characters
  26. hint: false,
  27.  
  28. // Will allow to type accent and give letter equivalent results, also can define a custom replacement object
  29. accent: false,
  30.  
  31. // Added "any" to highlight any word in the template, by default true will only highlight display keys
  32. highlight: true,
  33.  
  34. // Improved feature, Boolean,string,object(key, template (string, function))
  35. group: false,
  36.  
  37. // New feature, order groups "asc", "desc", Array, Function
  38. groupOrder: null,
  39.  
  40. // Maximum number of result per Group
  41. maxItemPerGroup: null,
  42.  
  43. // Take group options string and create a dropdown filter
  44. dropdownFilter: false,
  45.  
  46. // Filter the typeahead results based on dynamic value, Ex: Players based on TeamID
  47. dynamicFilter: null,
  48.  
  49. // Add a backdrop behind Typeahead results
  50. backdrop: false,
  51.  
  52. // Display the backdrop option as the Typeahead input is :focused
  53. backdropOnFocus: false,
  54.  
  55. // Improved option, true OR 'localStorage' OR 'sessionStorage'
  56. cache: false,
  57.  
  58. // Cache time to live in ms
  59. ttl: 3600000,
  60.  
  61. // Requires LZString library
  62. compression: false,
  63.  
  64. // Display search results on input focus
  65. searchOnFocus: false,
  66.  
  67. // Blur Typeahead when Tab key is pressed, if false Tab will go though search results
  68. blurOnTab: true,
  69.  
  70. // List the results inside any container string or jQuery object
  71. resultContainer: null,
  72.  
  73. // Forces the source to be generated on page load even if the input is not focused!
  74. generateOnLoad: null,
  75.  
  76. // The submit function only gets called if an item is selected
  77. mustSelectItem: false,
  78.  
  79. // String or Function to format the url for right-click & open in new tab on link results
  80. href: null,
  81.  
  82. // Allows search in multiple item keys ["display1", "display2"]
  83. display: ["display"],
  84.  
  85. // Display template of each of the result list
  86. template: null,
  87.  
  88. // Set the input value template when an item is clicked
  89. templateValue: null,
  90.  
  91. // Set a custom template for the groups
  92. groupTemplate: null,
  93.  
  94. // Compile display keys, enables multiple key search from the template string
  95. correlativeTemplate: false,
  96.  
  97. // Display an empty template if no result
  98. emptyTemplate: false,
  99.  
  100. // // If text is detected in the input, a cancel button will be available to reset the input (pressing ESC also cancels)
  101. cancelButton: true,
  102.  
  103. // Display a loading animation when typeahead is doing request / searching for results
  104. loadingAnimation: true,
  105.  
  106. // Set to false or function to bypass Typeahead filtering. WARNING: accent, correlativeTemplate, offset & matcher will not be interpreted
  107. filter: true,
  108.  
  109. // Add an extra filtering function after the typeahead functions
  110. matcher: null,
  111.  
  112. // Source of data for Typeahead to filter
  113. source: null,
  114.  
  115. // CSS selectors
  116. selector: {
  117. container: "typeahead__container",
  118. result: "typeahead__result",
  119. list: "typeahead__list",
  120. group: "typeahead__group",
  121. item: "typeahead__item",
  122. empty: "typeahead__empty",
  123. display: "typeahead__display",
  124. query: "typeahead__query",
  125. filter: "typeahead__filter",
  126. filterButton: "typeahead__filter-button",
  127. dropdown: "typeahead__dropdown",
  128. dropdownItem: "typeahead__dropdown-item",
  129. button: "typeahead__button",
  130. backdrop: "typeahead__backdrop",
  131. hint: "typeahead__hint",
  132. cancelButton: "typeahead__cancel-button"
  133. },
  134.  
  135. // Display debug information (RECOMMENDED for dev environment)
  136. debug: false

5.回调函数。

  1. callback: {
  2.  
  3. // When Typeahead is first initialized (happens only once)
  4. onInit: null,
  5.  
  6. // When the Typeahead initial preparation is completed
  7. onReady: null,
  8.  
  9. // Called when the layout is shown
  10. onShowLayout: null,
  11.  
  12. // Called when the layout is hidden
  13. onHideLayout: null,
  14.  
  15. // When data is being fetched & analyzed to give search results
  16. onSearch: null,
  17.  
  18. // When the result container is displayed
  19. onResult: null,
  20.  
  21. // When the result HTML is build, modify it before it get showed
  22. onLayoutBuiltBefore: null,
  23.  
  24. // Modify the dom right after the results gets inserted in the result container
  25. onLayoutBuiltAfter: null,
  26.  
  27. // When a key is pressed to navigate the results, before the navigation happens
  28. onNavigateBefore: null,
  29.  
  30. // When a key is pressed to navigate the results
  31. onNavigateAfter: null,
  32.  
  33. // When the mouse enter an item in the result list
  34. onMouseEnter: null,
  35.  
  36. // When the mouse leaves an item in the result list
  37. onMouseLeave: null,
  38.  
  39. // Possibility to e.preventDefault() to prevent the Typeahead behaviors
  40. onClickBefore: null,
  41.  
  42. // Happens after the default clicked behaviors has been executed
  43. onClickAfter: null,
  44.  
  45. // When the dropdownFilter is changed, trigger this callback
  46. onDropdownFilter: null,
  47.  
  48. // Gets called when the Ajax request(s) are sent
  49. onSendRequest: null,
  50.  
  51. // Gets called when the Ajax request(s) are all received
  52. onReceiveRequest: null,
  53.  
  54. // Perform operation on the source data before it gets in Typeahead data
  55. onPopulateSource: null,
  56.  
  57. // Perform operation on the source data before it gets in Typeahead cache
  58. onCacheSave: null,
  59.  
  60. // When Typeahead form is submitted
  61. onSubmit: null,
  62.  
  63. // Triggered if the typeahead had text inside and is cleared
  64. onCancel: null
  65. }

更新日志:

v2.11.2版本(2022-09-17)

  • CSS已更新

v2.11.1版本(2020-05-19)

  • 已更新

v2.11.0版本(2019-11-01)

  • 多源异步结果

v2.10.7版本(2019-10-20)

  • 修复Chrome上无法放置光标的输入问题
  • 修复显示键值为null时的排序问题
  • 修复在访问找到的元素之前先检查是否找到输入的问题

2018-07-31

  • 版本2.10.6

预览截图