Skip to content

Commit dc88363

Browse files
authored
Merge pull request #12 from JJJ/issue/2796
Issue/2796
2 parents e2f4245 + ae6fa13 commit dc88363

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

coffee/lib/abstract-chosen.coffee

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AbstractChosen
2626
@disable_search = @options.disable_search || false
2727
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true
2828
@group_search = if @options.group_search? then @options.group_search else true
29+
@search_in_values = @options.search_in_values || false
2930
@search_contains = @options.search_contains || false
3031
@single_backstroke_delete = if @options.single_backstroke_delete? then @options.single_backstroke_delete else true
3132
@max_selected_options = @options.max_selected_options || Infinity
@@ -168,9 +169,11 @@ class AbstractChosen
168169

169170
results = 0
170171

172+
searchMatchFromValue = false
171173
query = this.get_search_text()
172174
escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
173175
regex = this.get_search_regex(escapedQuery)
176+
highlightRegex = this.get_highlight_regex(escapedQuery)
174177

175178
for option in @results_data
176179

@@ -196,11 +199,15 @@ class AbstractChosen
196199
search_match = this.search_string_match(text, regex)
197200
option.search_match = search_match?
198201

202+
if not option.search_match and @search_in_values
203+
option.search_match = this.search_string_match(option.value, regex)
204+
searchMatchFromValue = true
205+
199206
results += 1 if option.search_match and not option.group
200207

201208
if option.search_match
202-
if query.length
203-
startpos = search_match.index
209+
if query.length and not searchMatchFromValue
210+
startpos = search_match.index highlightRegex
204211
prefix = text.slice(0, startpos)
205212
fix = text.slice(startpos, startpos + query.length)
206213
suffix = text.slice(startpos + query.length)
@@ -228,6 +235,11 @@ class AbstractChosen
228235
regex_flag = if @case_sensitive_search then "" else "i"
229236
new RegExp(regex_string, regex_flag)
230237

238+
get_highlight_regex: (escaped_search_string) ->
239+
regex_anchor = if @search_contains then "" else "\\b"
240+
regex_flag = if @case_sensitive_search then "" else "i"
241+
new RegExp(regex_anchor + escaped_search_string, regex_flag)
242+
231243
search_string_match: (search_string, regex) ->
232244
match = regex.exec(search_string)
233245
match.index += 1 if !@search_contains && match?[1] # make up for lack of lookbehind operator in regex

public/options.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ <h3>Example:</h3>
9191
<td>false</td>
9292
<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>
9393
</tr>
94+
<tr>
95+
<td>search_in_values</td>
96+
<td>false</td>
97+
<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>
98+
</tr>
9499
<tr>
95100
<td>group_search</td>
96101
<td>true</td>

0 commit comments

Comments
 (0)