百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Z

zelect

> 编程语言
开源

从 <select> 到更多内容 2012

392 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

从 <select> 到更多内容 2012

$('select').zelect()

It's just yet another <select>.

>>> Example <<<

>>> Download <<<

  • Small, ~ 300 LOC
  • Zero base CSS, roll your own
  • Customizable (well, at least workaroundable)
  • Handles asynchronous paged loading of large option lists (read: AJAX-ready-and-enabled)
  • Initializable in a detached or hidden DOM node
  • Programmatically selectable and changeable
  • Unit-tested

for opts in $.fn.zelect(opts)

optiondefaulttypeusage

throttle300ms.Delay for throttling keyups for filtering/loading option items based on a search term. 0 makes things synchronous.

loaderundefinedfunction(term, page, callback): Array[Item]Function for loading a pageful of option items. See example

renderItemitem.label || item.toString()function(item, term): String, DOM, jQuery, etc.Function to render a single option item. See example

initialundefineditemInitially selected item

placeholderundefinedString, DOM, jQuery, etcPlaceholder text or HTML to show when no initial selection. The first option item is selected by default if this is left undefined.

noResults"No results for '$query'"function(query?): String, DOM, jQuery, etc.Function to render a no-hits text.

regexpMatcher/(^|\s)term/ifunction(term): RegExpFunction to create a RegExp to filter <select>-based options with.

An item is any javascript data structure: String, Object, Array, whatever.

Minimal Setup

If the option list is known on the client, finite and manageable, use HTML:

xml

  First Option
  Another Option
  Third option
javascript
$('#select-backed-zelect').zelect()

Asyncronous Paged Setup

If the option list is server-backed, infinite or close to it, use opts.loader:

xml
javascript
$('#async-backed-zelect').zelect({
  initial: 'Third',
  loader: function(term, page, callback) {
    callback(['First for page '+page, 'Second', 'Third'])
  }
})

Callback expects an array. Elements in the array can be anything that renderItem can handle.

zelect will load the next page of results from opts.loader whenever the option list is scrolled to the bottom. It will stop trying to load more once the callback is called with an empty array.

Subscribing to Changes

These events are triggered on the <select>-element:

eventargstriggered when

ready-zelect is ready: first results have loaded and the initial selection has been made

changeevent, itemSelected item changed. 2nd parameter is the item itself.

In addition:

If the zelect is <select>-backed, $('select').val() will return the value of the currently selected option.

$('select').data('zelect-item') will always return the currently selected item.

Programmatically Changing The Selection

$('select').zelectItem(myNewItemThatIWantToSelectNow, fireChangeEvent) will do that.

Whether a change event fires can be controlled with the fireChangeEvent boolean. The default is true.

Programmatically Refreshing an Externally Updated Item

$('select').refreshZelectItem(myUpdatedItem, function(item) { return item._id }) will replace and rerender the item matching myUpdatedItem._id with the new version.

No change events are fired.

Programmatically Resetting the Zelect

$('select').resetZelect() will reset the query to an empty string, load the first results and select the first item.

A change event will fire.

Styling

zelect comes with no base css. Make your own.

For inspiration, see an example.

Initial Selection

When first rendered, zelect determines the initially selected item in this order:

  1. opts.initial if defined
  2. `` if opts.loader not defined
  3. Render placeholder text from opts.placeholder if defined
  4. Select the first option from the list if there are items
  5. Render noResults text without a term

Custom Option Item Rendering Example

javascript
$('select'.zelect({
  renderItem: function(item, term) {
    return $('').addClass('my-item').text(item.label).highlight(term)
  }
})

Highlights matches of the search term in the option text, by using e.g. jquery.highlight.js.

Ajax Loader Example

javascript
$('select'.zelect({
  loader: function(term, page, callback) {
    $.get('/search', { query: term, page: page }, function(arrayOfItems) {
      callback(arrayOfItems)
    }
  }
})

Uses a GET to retrieve paged results from a server.

Convoluted Semi-Real-World Example

…

Enjoy

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言