Skip to content

Commit 53ff249

Browse files
authored
Merge pull request #2886 from shekibobo/polymorphic-filter-select
Polymorphic filter select
2 parents ce6e160 + 46f5680 commit 53ff249

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@
3131
button: null,
3232
input: null,
3333
select: null,
34+
filtering_select: null,
3435

3536
_create: function() {
36-
var filtering_select = this.element.siblings(
37+
this.filtering_select = this.element.siblings(
3738
'[data-input-for="' + this.element.attr('id') + '"]'
3839
);
3940

4041
// When using the browser back and forward buttons, it is possible that
4142
// the autocomplete field will be cached which causes duplicate fields
4243
// to be generated.
43-
if (filtering_select.length > 0) {
44-
this.input = filtering_select.children('input');
45-
this.button = filtering_select.children('.input-group-btn');
44+
if (this.filtering_select.length > 0) {
45+
this.input = this.filtering_select.children('input');
46+
this.button = this.filtering_select.children('.input-group-btn');
4647
} else {
4748
this.element.hide();
48-
filtering_select = this._inputGroup(this.element.attr('id'));
49+
this.filtering_select = this._inputGroup(this.element.attr('id'));
4950
this.input = this._inputField();
5051
this.button = this._buttonField();
5152
}
@@ -56,7 +57,7 @@
5657
this._overloadRenderItem();
5758
this._autocompleteDropdownEvent(this.button);
5859

59-
return filtering_select.append(this.input)
60+
return this.filtering_select.append(this.input)
6061
.append(this.button)
6162
.insertAfter(this.element);
6263
},
@@ -201,7 +202,7 @@
201202
return $('<div>')
202203
.addClass('input-group filtering-select col-sm-2')
203204
.attr('data-input-for', inputFor)
204-
.css('float', 'left');
205+
.css('float', this.element.css("float"));
205206
},
206207

207208
_initAutocomplete: function() {
@@ -286,6 +287,7 @@
286287
this.input.remove();
287288
this.button.remove();
288289
this.element.show();
290+
this.filtering_select.remove();
289291
$.Widget.prototype.destroy.call(this);
290292
}
291293
});

app/assets/javascripts/rails_admin/ra.widgets.coffee

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,13 @@ $(document).on 'rails_admin.dom_ready', (e, content) ->
168168
field = type_select.parents('.control-group').first()
169169
object_select = field.find('select').last()
170170
urls = type_select.data('urls')
171+
171172
type_select.on 'change', (e) ->
172-
if $(this).val() is ''
173-
object_select.html('<option value=""></option>')
174-
else
175-
$.ajax
176-
url: urls[type_select.val()]
177-
data:
178-
compact: true
179-
all: true
180-
beforeSend: (xhr) ->
181-
xhr.setRequestHeader("Accept", "application/json")
182-
success: (data, status, xhr) ->
183-
html = $('<option></option>')
184-
$(data).each (i, el) ->
185-
option = $('<option></option>')
186-
option.attr('value', el.id)
187-
option.text(el.label)
188-
html = html.add(option)
189-
object_select.html(html)
173+
selected_type = type_select.val().toLowerCase()
174+
selected_data = $("##{selected_type}-js-options").data('options')
175+
object_select.data('options', selected_data)
176+
object_select.filteringSelect("destroy")
177+
object_select.filteringSelect selected_data
190178

191179

192180
# simplemde

app/views/rails_admin/main/_form_polymorphic_association.html.haml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22
type_collection = field.polymorphic_type_collection
33
type_column = field.association.foreign_type.to_s
44
selected_type = field.bindings[:object].send(type_column)
5-
collection = field.associated_collection(selected_type)
65
selected = field.bindings[:object].send(field.association.name)
6+
collection = selected ? [[field.formatted_value, selected.id]] : [[]]
77
column_type_dom_id = form.dom_id(field).sub(field.method_name.to_s, type_column)
8+
current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update'
9+
10+
default_options = { float_left: false }
11+
12+
js_data = type_collection.inject({}) do |options, model|
13+
model_name = model.second.underscore.downcase
14+
source_abstract_model = RailsAdmin.config(form.object.class).abstract_model
15+
options.merge(model_name.gsub("_", "") => {
16+
xhr: true,
17+
remote_source: index_path(model_name, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, current_action: current_action, compact: true),
18+
float_left: false
19+
})
20+
end
821

922
.form-inline
10-
= form.select type_column, type_collection, {include_blank: true, selected: selected_type}, class: "form-control", id: column_type_dom_id, data: { polymorphic: true, urls: field.polymorphic_type_urls.to_json }
11-
= form.select field.method_name, collection, {include_blank: true, selected: selected.try(:id)}, class: "form-control"
23+
- js_data.each do |model, value|
24+
%div{id: "#{model}-js-options", data: { options: value.to_json } }
25+
= form.select type_column, type_collection, {include_blank: true, selected: selected_type}, class: "form-control", id: column_type_dom_id, data: { polymorphic: true, urls: field.polymorphic_type_urls.to_json }, style: "float: left; margin-right: 10px;"
26+
= form.select field.method_name, collection, {include_blank: true, selected: selected.try(:id)}, class: "form-control", data: { filteringselect: true, options: js_data[selected_type.try(:downcase)] || default_options }, placeholder: 'Search'

0 commit comments

Comments
 (0)