Skip to content

Commit dd269d4

Browse files
committed
Merge pull request harvesthq#2362 from panmari/max_shown_results
Added option max_shown_results
2 parents 9fbcd10 + 60bca15 commit dd269d4

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

coffee/lib/abstract-chosen.coffee

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AbstractChosen
3232
@display_selected_options = if @options.display_selected_options? then @options.display_selected_options else true
3333
@display_disabled_options = if @options.display_disabled_options? then @options.display_disabled_options else true
3434
@include_group_label_in_selected = @options.include_group_label_in_selected || false
35+
@max_shown_results = @options.max_shown_results || Number.POSITIVE_INFINITY
3536

3637
set_default_text: ->
3738
if @form_field.getAttribute("data-placeholder")
@@ -65,11 +66,16 @@ class AbstractChosen
6566

6667
results_option_build: (options) ->
6768
content = ''
69+
shown_results = 0
6870
for data in @results_data
71+
data_content = ''
6972
if data.group
70-
content += this.result_add_group data
73+
data_content = this.result_add_group data
7174
else
72-
content += this.result_add_option data
75+
data_content = this.result_add_option data
76+
if data_content != ''
77+
shown_results++
78+
content += data_content
7379

7480
# this select logic pins on an awkward flag
7581
# we can make it better
@@ -79,6 +85,9 @@ class AbstractChosen
7985
else if data.selected and not @is_multiple
8086
this.single_set_selected_text(this.choice_label(data))
8187

88+
if shown_results >= @max_shown_results
89+
break
90+
8291
content
8392

8493
result_add_option: (option) ->

public/options.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ <h3>Example:</h3>
116116
<p>By default, Chosen only shows the text of a selected option. Setting this option to <code class="language-javascript">true</code> will show the text and group (if any) of the selected option.</p>
117117
</td>
118118
</tr>
119+
<tr>
120+
<td>max_shown_results</td>
121+
<td>Infinity</td>
122+
<td>
123+
<p>Only show the first (n) matching options in the results. This can be used to increase performance for selects with very many options.</p>
124+
</td>
125+
</tr>
119126
</table>
120127

121128
<h2><a name="attributes" class="anchor" href="#attributes">Attributes</a></h2>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
describe "search", ->
2+
it "should display only matching items when entering a search term", ->
3+
tmpl = "
4+
<select data-placeholder='Choose a Country...'>
5+
<option value=''></option>
6+
<option value='United States'>United States</option>
7+
<option value='United Kingdom'>United Kingdom</option>
8+
<option value='Afghanistan'>Afghanistan</option>
9+
</select>
10+
"
11+
div = $("<div>").html(tmpl)
12+
select = div.find("select")
13+
select.chosen()
14+
15+
container = div.find(".chosen-container")
16+
container.trigger("mousedown") # open the drop
17+
# Expect all results to be shown
18+
results = div.find(".active-result")
19+
expect(results.size()).toBe(3)
20+
21+
# Enter some text in the search field.
22+
search_field = div.find(".chosen-search input").first()
23+
search_field.val("Afgh")
24+
search_field.trigger('keyup')
25+
26+
# Expect to only have one result: 'Afghanistan'.
27+
results = div.find(".active-result")
28+
expect(results.size()).toBe(1)
29+
expect(results.first().text()).toBe "Afghanistan"
30+
31+
it "should only show max_shown_results items in results", ->
32+
tmpl = "
33+
<select data-placeholder='Choose a Country...'>
34+
<option value=''></option>
35+
<option value='United States'>United States</option>
36+
<option value='United Kingdom'>United Kingdom</option>
37+
<option value='Afghanistan'>Afghanistan</option>
38+
</select>
39+
"
40+
div = $("<div>").html(tmpl)
41+
select = div.find("select")
42+
select.chosen({max_shown_results: 1 })
43+
44+
container = div.find(".chosen-container")
45+
container.trigger("mousedown") # open the drop
46+
results = div.find(".active-result")
47+
expect(results.size()).toBe(1)
48+
49+
# Enter some text in the search field.
50+
search_field = div.find(".chosen-search input").first()
51+
search_field.val("United")
52+
search_field.trigger("keyup")
53+
54+
# Showing only one result: the one that occurs first.
55+
results = div.find(".active-result")
56+
expect(results.size()).toBe(1)
57+
expect(results.first().text()).toBe "United States"
58+
59+
# Showing still only one result, but not the first one.
60+
search_field.val("United Ki")
61+
search_field.trigger("keyup")
62+
results = div.find(".active-result")
63+
expect(results.size()).toBe(1)
64+
expect(results.first().text()).toBe "United Kingdom"

0 commit comments

Comments
 (0)