|
50 | 50 | this.input = this._inputField(); |
51 | 51 | this.button = this._buttonField(); |
52 | 52 | } |
| 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")); |
53 | 57 |
|
54 | 58 | this._setOptionsSource(); |
55 | 59 | this._initAutocomplete(); |
|
85 | 89 | } |
86 | 90 | }; |
87 | 91 |
|
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}]; |
90 | 113 | } 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; |
99 | 115 | } |
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 | | - })); |
116 | 116 | }, |
117 | 117 |
|
118 | 118 | _getSourceFunction: function(source) { |
|
0 commit comments