diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index 09d3bd1..83ce56a 100644 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -52,20 +52,22 @@ class Admin::UniversalTablesController < OrbitAdminController table = UTable.where(:uid => params["uid"]).first rescue nil data = [] if !table.nil? - enteries = search_data(table, 50) - ma = ModuleApp.find_by_key("universal_table") - enteries.each do |entry| - if params["links"].present? - data << { - "id" => entry.id.to_s, - "text" => entry.column_entries.first.text, - "link" => OrbitHelper.cal_url_to_show(ma,entry) - } - else - data << { - "id" => entry.id.to_s, - "text" => entry.column_entries.first.text - } + if params[:q].present? + enteries = search_data(table, 50) + ma = ModuleApp.find_by_key("universal_table") + enteries.each do |entry| + if params["links"].present? + data << { + "id" => entry.id.to_s, + "text" => entry.column_entries.first.text, + "link" => OrbitHelper.cal_url_to_show(ma,entry) + } + else + data << { + "id" => entry.id.to_s, + "text" => entry.column_entries.first.text + } + end end end end diff --git a/app/views/admin/universal_tables/_entry_form.html.erb b/app/views/admin/universal_tables/_entry_form.html.erb index 8bac7d6..e4adeba 100644 --- a/app/views/admin/universal_tables/_entry_form.html.erb +++ b/app/views/admin/universal_tables/_entry_form.html.erb @@ -69,34 +69,38 @@ $("#universal_table_tags").select2({ return { id: term, text: "#" + term.trim().toLowerCase() }; } }); - $("#universal_table_tags").val(<%= raw(@entry.table_tags.collect { |tag| tag.id.to_s }) %>).trigger("change"); - - var select2Options = { + $("#universal_table_tags").val(<%= raw(@entry.table_tags. collect { |tag| tag.id.to_s }) %>).trigger("change"); + + const preselectedData = $("#table_entry_related_entries").data('value'); + preselectedData.forEach(function(item) { + var option = new Option(item.text, item.id, true, true); + $('#table_entry_related_entries').append(option); + }); + var select2Options = { maximumSelectionSize: 10, minimumResultsForSearch: Infinity, multiple: true, minimumInputLength: 2, + width: '100%', placeholder: "<%= t("universal_table.search_entries") %>", ajax: { type: 'get', url: "/admin/universal_tables/get_entries?uid=<%= @entry.u_table.uid %>", - allowClear: true, + allowClear: false, dataType: 'json', delay: 250, data: function (term) { - var query = { - q: term - } - return query; + return { q: term }; }, results: function(data, page) { return { results: data }; }, - cache: true + cache: false }, formatResult: function(i) { + console.log(i) return '
' + i.text + '
'; }, // Formats results in drop down formatSelection: function(i) { @@ -107,16 +111,5 @@ $("#universal_table_tags").select2({ } // we do not want to escape markup since we are displaying html in results } - $("#table_entry_related_entries").select2(select2Options); - $('.select2').each(function () { - const data = $(this).data('value'); - if (data) { - console.log($(this)) - $(this).select2({ - data: data, - multiple: true, - width: '100%' - }).val(data.map(i => i.id)).trigger('change'); - } - }); + $("#table_entry_related_entries").select2(select2Options).trigger("change"); \ No newline at end of file