Skip to content

Commit 7e3a1a6

Browse files
committed
Show 'No objects found' placeholder in filtering-select as well
Closes #3332
1 parent 7580f33 commit 7e3a1a6

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

app/assets/javascripts/rails_admin/ra.filtering-select.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
this.input = this._inputField();
5151
this.button = this._buttonField();
5252
}
53+
this.clearOption = $('<span style="color: #888"></span>').append(
54+
'<i class="icon-remove"></i> ' + $('<span></span>').text(RailsAdmin.I18n.t("clear")).html()
55+
);
56+
this.noObjectsPlaceholder = $('<option disabled="disabled" />').text(RailsAdmin.I18n.t("no_objects"));
5357

5458
this._setOptionsSource();
5559
this._initAutocomplete();
@@ -85,34 +89,30 @@
8589
}
8690
};
8791

88-
if (request.term.length > 0 || this.input.attr('required')) {
89-
var additionalOptions = [];
92+
var matches = $.map(
93+
data,
94+
function(el) {
95+
var id = el.id || el.value;
96+
var value = el.label || el.id;
97+
// match regexp only for local requests, remote ones are already
98+
// filtered, and label may not contain filtered term.
99+
if (id && (xhr || matcher.test(el.label))) {
100+
return {
101+
html: highlighter(value, request.term),
102+
value: value,
103+
id: id
104+
};
105+
}
106+
}
107+
);
108+
109+
if (request.term.length === 0 && !this.input.attr('required')) {
110+
return [{html: this.clearOption, value: null, id: null}].concat(matches);
111+
} else if (matches.length === 0) {
112+
return [{html: this.noObjectsPlaceholder, value: null, id: null}];
90113
} else {
91-
var additionalOptions = [{
92-
html: $('<span style="color: #888"></span>').append(
93-
'<i class="icon-remove"></i> ' +
94-
$('<span></span>').text(RailsAdmin.I18n.t("clear")).html()
95-
),
96-
value: null,
97-
id: null
98-
}];
114+
return matches;
99115
}
100-
101-
return additionalOptions.concat($.map(
102-
data,
103-
function(el) {
104-
var id = el.id || el.value;
105-
var value = el.label || el.id;
106-
// match regexp only for local requests, remote ones are already
107-
// filtered, and label may not contain filtered term.
108-
if (id && (xhr || matcher.test(el.label))) {
109-
return {
110-
html: highlighter(value, request.term),
111-
value: value,
112-
id: id
113-
};
114-
}
115-
}));
116116
},
117117

118118
_getSourceFunction: function(source) {

spec/integration/widgets/filtering_select_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
expect(all(:css, 'ul.ui-autocomplete li.ui-menu-item a').map(&:text)).to eq ['Los Angeles Dodgers']
3131
find('input.ra-filtering-select-input').set('Mets')
3232
page.execute_script("$('input.ra-filtering-select-input').trigger('focus').trigger('keydown')")
33-
is_expected.not_to have_selector('ul.ui-autocomplete')
33+
is_expected.to have_selector('ul.ui-autocomplete li.ui-menu-item a')
34+
expect(all(:css, 'ul.ui-autocomplete li.ui-menu-item a').map(&:text)).to match_array ['No objects found']
3435
end
3536

3637
it 'sets id of the selected item' do

0 commit comments

Comments
 (0)