自定义标签输入和选择框 jQuery插件 selectize.js

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

简介

selectize.js是一个有用且轻量级的插件,用于创建坚实且用户友好的标签输入字段和选择列表,并具有干净强大的API和代码。

还与最新的Bootstrap 5/4/3框架兼容。

请查看演示页面以查看它的实际操作。

对于IE 8的支持,你应该在你的网页上包括es5垫片。

参见:

  • jQuery标签管理器与Twitter引导-标签管理器
  • 用于jQuery的选择框替换插件-Select2
  • 标记输入和自动完成插件-TextExt
  • 使用jQuery进行高级标记过滤-过滤
  • 10个最佳多选插件

基本用法:

1.在head部分包含jQuery库和selectize.js

  1. <script src="/path/to/jquery.min.js"></script>
  2. <script src="/path/to/selectize.js"></script>

2.包含jQuery selectize CSS来设计插件的样式

  1. <link rel="stylesheet" href="selectize.css">

3.为标签输入创建一个容器

  1. <input type="text" id="input-tags" class="demo-default" value="awesome,neat">

4.调用输入字段上的函数来初始化插件。

  1. $('#input-tags').selectize({
  2.  
  3. // An array of the initial options available to select; array of objects.
  4. // By default this is populated from the original input element.
  5. // If your element is a <select> with <option>s specified this property gets populated automatically.
  6. // Setting this property is convenient if you have your data as an array and want to automatically generate the <option>s.
  7. options: [],
  8.  
  9. // Initial selected values.
  10. items: []
  11.  
  12. // Option groups that options will be bucketed into.
  13. // If your element is a <select> with <optgroup>s this property gets populated automatically.
  14. // Make sure each object in the array has a property named whatever optgroupValueField is set to.
  15. optgroups: [],
  16.  
  17. // Custom delimiter character to separate items
  18. delimiter: ',',
  19.  
  20. // All plugins live in their own folders in "src/plugins".
  21. plugins: [],
  22.  
  23. // regexp or string for splitting up values from a paste command
  24. splitOn: null,
  25.  
  26. // If false, items created by the user will not show up as available options once they are unselected.
  27. persist: true,
  28.  
  29. // Enable or disable international character support.
  30. diacritics: true,
  31.  
  32. // Allows the user to create new items that aren't in the initial list of options.
  33. // This setting can be any of the following: true, false (disabled), or a function to process input.
  34. // The function can take one of two forms: synchronous (with signature function(input){} or asynchronous (with signature function(input, callback).
  35. // In the synchronous case, the function should return an object for the options (eg, with defaults: return { 'value': value, 'text': text };).
  36. // The asynchronous version should invoke the callback with the result in the same format as the object above (eg, callback( { 'value': value, 'text': text});)
  37. create: false,
  38.  
  39. // If true, when user exits the field (clicks outside of input), a new option is created and selected (if create setting is enabled).
  40. createOnBlur: false,
  41.  
  42. // Specifies a RegExp or a string containing a regular expression that the current search filter must match to be allowed to be created.
  43. // May also be a predicate function that takes the filter text and returns whether it is allowed.
  44. createFilter: null,
  45.  
  46. // Toggles match highlighting within the dropdown menu.
  47. highlight: true,
  48.  
  49. // Show the dropdown immediately when the control receives focus.
  50. openOnFocus: true,
  51.  
  52. // The max number of items to render at once in the dropdown list of options.
  53. maxOptions: 1000,
  54.  
  55. // The max number of items the user can select. Null allows an unlimited number of items
  56. maxItems: 1,
  57.  
  58. // If true, the items that are currently selected will not be shown in the dropdown list of available options.
  59. hideSelected: false,
  60.  
  61. // If true, the "Add..." option is the default selection in the dropdown.
  62. addPrecedence: false,
  63.  
  64. // If true, the tab key will choose the currently selected item.
  65. selectOnTab: false,
  66.  
  67. // If true, the load function will be called upon control initialization (with an empty search). Alternatively it can be set to 'focus' to call the load function when control receives focus.
  68. preload: false,
  69.  
  70. // Allows empty options.
  71. allowEmptyOption: false,
  72.  
  73. // Shows empty option in the dropdown
  74. showEmptyOptionInDropdown: false,
  75.  
  76. // Label for empty option
  77. emptyOptionLabel: '--',
  78.  
  79. // Enable setting the first option in the list as active.
  80. setFirstOptionActive: false,
  81.  
  82. // If true, the dropdown will be closed after a selection is made.
  83. closeAfterSelect: false,
  84.  
  85. // in ms
  86. closeDropdownThreshold: 250,
  87.  
  88. // The animation duration (in milliseconds) of the scroll animation triggered when going [up] and [down] in the options dropdown.
  89. scrollDuration: 60,
  90.  
  91. // The number of milliseconds to wait before requesting options from the server or null.
  92. // If null, throttling is disabled. Useful when loading options dynamically while the user types a search / filter expression.
  93. loadThrottle: 300,
  94.  
  95. // or 'top'
  96. deselectBehavior: 'previous',
  97.  
  98. // The class name added to the wrapper element while awaiting the fulfillment of load requests.
  99. loadingClass: 'loading',
  100.  
  101. // The number of milliseconds to wait before requesting options from the server or null.
  102. // If null, throttling is disabled. Useful when loading options dynamically while the user types a search / filter expression.
  103. loadThrottle: 300,
  104.  
  105. // The placeholder of the control (displayed when nothing is selected / typed).
  106. // Defaults to input element's placeholder, unless this one is specified.
  107. placeholder: undefined,
  108.  
  109. // The <option> attribute from which to read JSON data about the option.
  110. dataAttr: 'data-data',
  111.  
  112. // The name of the property to group items by.
  113. optgroupField: 'optgroup',
  114.  
  115. // The name of the property to use as the value when an item is selected.
  116. valueField: 'value',
  117.  
  118. // The name of the property to render as an option / item label (not needed when custom rendering functions are defined).
  119. labelField: 'text',
  120.  
  121. // The name of the property to disabled option and optgroup.
  122. disabledField: 'disabled',
  123.  
  124. // The name of the property to render as an option group label (not needed when custom rendering functions are defined).
  125. optgroupLabelField: 'label',
  126.  
  127. // The name of the option group property that serves as its unique identifier.
  128. optgroupValueField: 'value',
  129.  
  130. // If truthy, Selectize will make all optgroups be in the same order as they were added (by the `$order` property).
  131. // Otherwise, it will order based on the score of the results in each.
  132. lockOptgroupOrder: false,
  133.  
  134. // A single field or an array of fields to sort by.
  135. // Each item in the array should be an object containing at least a field property.
  136. // Optionally, direction can be set to 'asc' or 'desc'.
  137. // The order of the array defines the sort precedence.
  138. sortField: '$order',
  139.  
  140. // An array of property names to analyze when filtering options.
  141. searchField: ['text'],
  142.  
  143. // When searching for multiple terms (separated by space), this is the operator used. Can be 'and' or 'or' .
  144. searchConjunction: 'and',
  145.  
  146. // multi or single
  147. mode: null,
  148.  
  149. // Default classes
  150. wrapperClass: 'selectize-control',
  151. inputClass: 'selectize-input',
  152. dropdownClass: 'selectize-dropdown',
  153. dropdownContentClass: 'selectize-dropdown-content',
  154.  
  155. // The element the dropdown menu is appended to. This should be 'body' or null. If null, the dropdown will be appended as a child of the Selectize control.
  156. dropdownParent: null,
  157.  
  158. // Copy the original input classes to the dropdown element.
  159. copyClassesToDropdown: true,
  160.  
  161. // Custom rendering functions. Each function should accept two arguments: data and escape and return HTML (string or DOM element) with a single root element.
  162. // The escape argument is a function that takes a string and escapes all special HTML characters. This is very important to use to prevent XSS vulnerabilities.
  163. render: {
  164. /*
  165. item: null,
  166. optgroup: null,
  167. optgroup_header: null,
  168. option: null,
  169. option_create: null
  170. */
  171. }
  172.  
  173. });

5.回调函数。

  1. load : null, // function(query, callback) { ... }
  2. score : null, // function(search) { ... }
  3. onInitialize : null, // function() { ... }
  4. onChange : null, // function(value) { ... }
  5. onItemAdd : null, // function(value, $item) { ... }
  6. onItemRemove : null, // function(value) { ... }
  7. onClear : null, // function() { ... }
  8. onOptionAdd : null, // function(value, data) { ... }
  9. onOptionRemove : null, // function(value) { ... }
  10. onOptionClear : null, // function() { ... }
  11. onOptionGroupAdd : null, // function(id, data) { ... }
  12. onOptionGroupRemove : null, // function(id) { ... }
  13. onOptionGroupClear : null, // function() { ... }
  14. onDropdownOpen : null, // function($dropdown) { ... }
  15. onDropdownClose : null, // function($dropdown) { ... }
  16. onType : null, // function(str) { ... }
  17. onDelete : null, // function(values) { ... }

6.API方法。

  1. // initialize the Selectize control
  2. var $select = $('#input-tags').selectize(options);
  3. // fetch the instance
  4. var selectize = $select[0].selectize;
  5.  
  6. // add options, string or array
  7. selectize.addOption(data);
  8.  
  9. // update options
  10. selectize.updateOption(value, data)
  11.  
  12. // remove an option
  13. selectize.removeOption(value);
  14.  
  15. // clear options
  16. selectize.clearOptions(silent);
  17.  
  18. // get options
  19. selectize.getOption(value)
  20.  
  21. // retrieve the jQuery element for the previous or next option, relative to the currently highlighted option
  22. // the direction argument should be 1 for "next" or -1 for "previous"
  23. selectize.getAdjacentOption(value, direction);
  24.  
  25. // refresh options
  26. selectize.refreshOptions(triggerDropdown);
  27.  
  28. // clear all selected items
  29. selectize.clear(silent);
  30.  
  31. // get item
  32. selectize.getItem(value);
  33.  
  34. // add an item
  35. selectize.addItem(value, silent);
  36.  
  37. // remove an item
  38. selectize.removeItem(value, silent);
  39.  
  40. // create a new item
  41. selectize.createItem(value, [triggerDropdown], [callback]);
  42.  
  43. // refresh items
  44. selectize.refreshItems();
  45.  
  46. // add a new optgroup
  47. selectize.addOptionGroup(id, data);
  48.  
  49. // remove a optgroup
  50. selectize.removeOptionGroup(id);
  51.  
  52. // clear all optgroups
  53. selectize.clearOptionGroups();
  54.  
  55. // open the autocomplete dropdown
  56. selectize.open();
  57.  
  58. // close the autocomplete dropdown
  59. selectize.close();
  60.  
  61. // re-position the autocomplete dropdown
  62. selectize.positionDropdown();
  63.  
  64. // destroy the instnace
  65. selectize.destroy();
  66.  
  67. // load options
  68. selectize.load(fn)
  69.  
  70. // focus on the control
  71. selectize.focus();
  72.  
  73. // move the focus out of the control
  74. selectize.blur();
  75.  
  76. // lock/unlock the control
  77. selectize.lock();
  78. selectize.unlock();
  79.  
  80. // disable/enable user input
  81. selectize.disable();
  82. selectize.enable();
  83.  
  84. // get the value of the control
  85. selectize.getValue();
  86.  
  87. // set the selected items
  88. selectize.setValue(value, silent);
  89.  
  90. // set the selected item
  91. selectize.setActiveItem($item, e);
  92.  
  93. // reset the number of max items to the given value
  94. selectize.setMaxItems(value);
  95.  
  96. // move the caret to the specified position
  97. selectize.setCaret(index);
  98.  
  99. // check if is reaching the limit
  100. selectize.isFull();
  101.  
  102. // clear cache
  103. selectize.clearCache(template);
  104.  
  105. // update place holder
  106. selectize.updatePlaceholder();
  107.  
  108. // add an event listener
  109. selectize.on(event, handler);
  110.  
  111. // remove an event listener
  112. selectize.off(event, handler);
  113.  
  114. // remove all event listeners
  115. selectize.off(event);
  116.  
  117. // trigger event listeners
  118. selectize.trigger(event, ...);

7.事件。

  1. selectize.on('initialize', function(){
  2. // do something
  3. });
  4.  
  5. selectize.on('focus', function(){
  6. // do something
  7. });
  8.  
  9. selectize.on('blur', function(){
  10. // do something
  11. });
  12.  
  13. selectize.on('clear', function(){
  14. // do something
  15. });
  16.  
  17. selectize.on('option_clear', function(){
  18. // do something
  19. });
  20.  
  21. selectize.on('optgroup_clear', function(){
  22. // do something
  23. });
  24.  
  25. selectize.on('destroy', function(){
  26. // do something
  27. });
  28.  
  29. selectize.on('change', function(value){
  30. // do something
  31. });
  32.  
  33. selectize.on('item_add', function(value, $item){
  34. // do something
  35. });
  36.  
  37. selectize.on('item_remove', function(value, $item){
  38. // do something
  39. });
  40.  
  41. selectize.on('option_add', function(value, data){
  42. // do something
  43. });
  44.  
  45. selectize.on('option_remove', function(value){
  46. // do something
  47. });
  48.  
  49. selectize.on('optgroup_add', function(id, data){
  50. // do something
  51. });
  52.  
  53. selectize.on('optgroup_remove', function(id){
  54. // do something
  55. });
  56.  
  57. selectize.on('dropdown_open', function($dropdown){
  58. // do something
  59. });
  60.  
  61. selectize.on('dropdown_close', function($dropdown){
  62. // do something
  63. });
  64.  
  65. selectize.on('type', function(str){
  66. // do something
  67. });
  68.  
  69. selectize.on('load', function(data){
  70. // do something
  71. });

更多示例:

  • 基本示例
  • 如何以编程方式与控件交互的示例
  • 联系人列表示例
  • 自定义外观示例
  • 动态示例
  • 集成异步加载的第三方数据的示例。
  • 执行示例

更新日志:

版本0.15.2(2023-01-07)

  • 已从每个讨论的缩小资产中删除源映射
  • 更新libs
  • 小调整
  • 正在更新Autofill_Disable插件以修复回归

版本0.15.1(2022-11-18)

  • 新功能:动态添加选项组

版本0.15.0(2022-11-15)

  • refreshItems():添加“静默”选项
  • 选择类已丢失/监视模式
  • 更高性能的更新OriginalInput
  • 如果openOnFocus为false,则单击鼠标时打开下拉列表,并且单击选项时
  • 确保插件以可预测的顺序加载
  • 添加了screener的尊重词边界选项。初始默认值为true
  • 几个和增强功能
  • 错误修复程序

版本0.14.0(2022-10-03)

  • 在mousedown上添加了一个节流阀,防止单击关闭后重新打开下拉列表。
  • 避免引用可能已删除的此对象的events属性(_E)。
  • 允许筛选器选项用于筛选。
  • 功能清除按钮。
  • 功能:为自定义下拉高度添加“大小”选项。
  • 修复了错误。
  • 更新的程序包。

版本0.13.7(2022-09-23)

  • 修复了dropdown_header插件缺少的样式
  • 固定单击选定项目时向左滚动,页面向右滚动
  • 添加了选项以启用将列表中的第一个选项设置为活动选项。

版本0.13.6(2022-07-10)

  • 使用navigator.userAgentData进行平台检测(如果可用)
  • 对JQuery peerDependency使用正确的semver语法
  • 修复bootstrap3选择宽度项目边框不兼容的值
  • 修复打字错误
  • 使用application/xhtml+xml会导致“无效或非法字符串”

版本0.13.4(2022-02-06)

  • 修复$select宽度项目边框上缺少像素单位的问题。

版本0.13.3(2022-01-28)

  • 更新deps。添加bootstrap 5支持
  • 错误修复

v0.13.3 (2021-06-05)

  • 更新依赖项

版本0.13.2(2020-12-01)

  • 当getSelection找不到输入控件时添加了警告
  • 在移除项目之前添加了事件“item_before_remove”

版本0.13.0(2020-12-01)

  • 添加setMaxItems API

版本0.13.0(2020-11-19)

  • 更新sass实现。构建时压缩css文件

版本0.13.0(2020-11-04)

  • 支持Bootstrap v4.x
  • 添加SASS样式后,LESS样式仍可用于bootstrap 2.x
  • 修复了占位符文本剪辑的错误
  • 修复了删除按钮插件关闭引导模式的错误
  • 向clearOptions函数添加静默选项

版本0.12.6(2018-07-13)

  • 使现代化

版本0.12.5(2018-06-28)

  • 如果下拉列表在关闭时未失去焦点,则允许其在单击时重新打开AfterSelect:true
  • 修正了clearOptions函数的错误。现在它不会删除已经选择的选项。
  • 新功能:允许禁用单个选项或完整的选项组

版本0.12.4(2016-12-07)

  • 添加了一个新功能,允许禁用单个选项或完整的选项组

版本0.12.4(2016年10月13日)

  • JS更新

版本0.12.3(2016年8月25日)

  • 应用Selectize后使标签工作
  • 当Microplguin丢失时,输出友好的错误消息。
  • 添加本地服务器命令grunt server。
  • 粘贴文本时自动停止创建项目,仅当粘贴的文本包含分隔符时才创建项目。
  • 修复Chrome中的回归“必填字段无法聚焦”
  • 修复了有效性API的检测,我们以前有假阴性。
  • 修复iOS下关闭选择后打开键盘的错误
  • 修复突出显示多个字符的问题

版本0.12.2(2016年6月24日)

  • 修复了由于uglifyjs中的错误而导致无法生成的问题(“无法分配给只读属性'subarray'”)。
  • 修复IE11上的选项卡问题。
  • 修复jQuery的jQuery初始化>=1.9
  • 使remove_button适用于单选项
  • 修复了导致allowEmptyOption:true无用的错误
  • 选项render中的函数现在可以返回除文本之外的DOM节点

2016-01-03

  • 发布0.12.1。
  • 选项render中的函数现在除了返回文本之外,还可以返回DOM节点。

2015-01-30

  • 更新+错误修复

版本0.11.2(2014-09-25)

  • 使现代化

版本0.11.0(2014-08-02)

  • 使现代化

版本0.10.1(2014-06-03)

  • 使现代化

v0.9.1 (2014-05-01)

  • 使现代化

v0.9.0 (2014-03-21)

  • 使现代化

v0.8.5 (2013-11-25)

  • 修复了生成脚本。

版本0.8.4(2013-11-15)

  • 添加了“createOnBlur”选项。

v0.8.3(2013年11月13日)

  • 修复了在选择另一个选项(缓存)后,最初选择的项目不会重新出现在下拉列表中的问题。

版本0.8.2(2013-11-03)

  • 修复在值未更改时触发的“更改”事件
  • 单击已选择的选项时,将焦点保持在ESC+关闭(“单一”模式)

版本0.8.1(2013-10-17)

  • 修复了destroy()方法,使其恢复运行Selectize之前最初存在的原始选项、optgroup和tabindex

v0.8.0(2013年10月13日)

  • 增加了从右到左(RTL)环境支持。
  • 添加了多属性排序(每个字段具有可配置的方向)。
  • 添加了“必需”属性处理。感谢@sjhewitt。
  • 增加了对多个选项组中相同选项的支持。
  • 当存在前导/尾随空白时,改进了内部文本框的宽度处理。感谢@jacquelineweller。
  • 聚焦时单击控件现在可以在单一模式下切换下拉菜单。
  • 突出显示焦点上下拉列表中的选定项目。
  • 添加了“searchConjunction”设置,允许“and”或“or”运算符连接多个搜索词。
  • 添加了“添加优先级”设置,允许在创建模式下默认选择“添加…”项。
  • 修复了导致“remove_button”和“drag_drop”插件在锁定/禁用控件时仍然可用的错误。
  • 已禁用控件上的本机浏览器自动完成功能。

版本0.7.7(2013-09-17)

  • 固定addOptionGroup方法

v0.7.5 (2013-09-09)

  • 固定打字错误

v0.7.3(2013年8月31日)

  • 将jQuery添加到deps。

版本0.7.2(2013-08-28)

  • 修复.form控件样式冲突
  • 只触发一次“更改”DOM事件

v0.7.0(2013年8月25日)

  • 简化了addOption(),不再需要显式声明“value”。

版本0.6.14(2013-08-06)

  • 错误修复

预览截图