Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New "search_in_values" option #2796

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AbstractChosen
@disable_search = @options.disable_search || false
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true
@group_search = if @options.group_search? then @options.group_search else true
@search_in_values = @options.search_in_values || false
@search_contains = @options.search_contains || false
@single_backstroke_delete = if @options.single_backstroke_delete? then @options.single_backstroke_delete else true
@max_selected_options = @options.max_selected_options || Infinity
Expand Down Expand Up @@ -165,6 +166,7 @@ class AbstractChosen
results = 0

searchText = this.get_search_text()
searchMatchFromValue = false
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
regex = this.get_search_regex(escapedSearchText)
highlightRegex = this.get_highlight_regex(escapedSearchText)
Expand All @@ -189,10 +191,15 @@ class AbstractChosen

unless option.group and not @group_search
option.search_match = this.search_string_match(option.search_text, regex)

if not option.search_match and @search_in_values
option.search_match = this.search_string_match(option.value, regex)
searchMatchFromValue = true

results += 1 if option.search_match and not option.group

if option.search_match
if searchText.length
if searchText.length and not searchMatchFromValue
startpos = option.search_text.search highlightRegex
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length)
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos)
Expand Down
5 changes: 5 additions & 0 deletions public/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ <h3>Example:</h3>
<td>false</td>
<td>By default, Chosen’s search matches starting at the beginning of a word. Setting this option to <code class="language-javascript">true</code> allows matches starting from anywhere within a word. This is especially useful for options that include a lot of special characters or phrases in ()s and []s.</td>
</tr>
<tr>
<td>search_in_values</td>
<td>false</td>
<td>By default, Chosen’s search matches in content of HTML&lt;option&gt; element. Setting this option to <code class="language-javascript">true</code> allows matches in <code>value</code> attribute of the &lt;option&gt; element.</td>
</tr>
<tr>
<td>single_backstroke_delete</td>
<td>true</td>
Expand Down