From bcb3ddad7ec5835282536775d32b17c5dfb31e6e Mon Sep 17 00:00:00 2001 From: rulingcom Date: Tue, 15 Jul 2025 22:16:34 +0800 Subject: [PATCH] added old entries and confirm --- app/views/admin/curations/_form.html.erb | 68 +++++++++++++++---- .../curations/_form_section_post.html.erb | 10 +-- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/app/views/admin/curations/_form.html.erb b/app/views/admin/curations/_form.html.erb index 8c1faf5..e2467ca 100644 --- a/app/views/admin/curations/_form.html.erb +++ b/app/views/admin/curations/_form.html.erb @@ -804,28 +804,66 @@ return m; } // we do not want to escape markup since we are displaying html in results } + $(document).on('focus', '.section_type_ut select', function () { + $(this).data('previous', $(this).val()); + }); $(document).on('change', '.section_type_ut select', function(){ + var $select = $(this); var parent = $(this).parents(".curation_section"); - if($(this).val()){ - selectedUTable = $(this).val(); - parent.find(".section_type_ut_entries").show(); - select2Options.ajax.url = "/admin/universal_tables/get_entries?uid=" + selectedUTable; - parent.find('.section_type_ut_entries input.select2').select2(select2Options); + var previousValue = $select.data('previous'); + if(!confirm("Are you sure? All the entries will be deleted.")){ + $select.val(previousValue).trigger('change.select2'); }else{ - parent.find(".section_type_ut_entries").hide(); - parent.find('.section_type_ut_entries input.select2').select2('destroy'); + $select.data('previous', $select.val()); + if($(this).val()){ + selectedUTable = $(this).val(); + parent.find(".section_type_ut_entries").show(); + parent.find(".section_type_ut_entries").find("ul li").remove(); + parent.find(".section_type_ut_entries").find("input.select2").val(""); + select2Options.ajax.url = "/admin/universal_tables/get_entries?uid=" + selectedUTable; + parent.find('.section_type_ut_entries input.select2').select2(select2Options); + }else{ + parent.find(".section_type_ut_entries").hide(); + parent.find('.section_type_ut_entries input.select2').select2('destroy'); + } } }) $('.select2').each(function () { - const data = $(this).data('value'); - if (data) { - $(this).select2({ - data: data, - multiple: true, - width: '100%' - }).val(data.map(i => i.id)).trigger('change'); - } + let tableid = $(this).parents(".curation_section").find(".universal_table").val() + $(this).select2({ + multiple: true, + ajax: { + type: 'get', + url: "/admin/universal_tables/get_entries?uid=" + tableid, + allowClear: true, + dataType: 'json', + delay: 250, + data: function (term) { + var query = { + q: term + } + return query; + }, + results: function(data, page) { + return { + results: data + }; + }, + cache: true + }, + }) }); + $(document).on("click", ".cleanup", function (){ + let ids = []; + let ul = $(this).parents("ul"); + let input = $(this).parents(".section_type_ut_entries").find("input.select2"); + $(this).parent().remove(); + ul.find("li").each(function(){ + ids.push($(this).data("entry-id")); + }) + input.val(ids.join(",")); + return false; + }) // mind map $(document).on('change', '.section_type_mm select.mind_map_select_ut', function(){ var tableid = $(this).val(); diff --git a/app/views/admin/curations/_form_section_post.html.erb b/app/views/admin/curations/_form_section_post.html.erb index 9a896bd..d78dbde 100755 --- a/app/views/admin/curations/_form_section_post.html.erb +++ b/app/views/admin/curations/_form_section_post.html.erb @@ -63,12 +63,14 @@ class: 'form-control universal_table' %> <% if !form_section_post.new_record? && form_section_post.section_type == "universal_table" %> - <% - tbData = form_section_post.get_table_entries.map{|tb| {id: tb.id.to_s, text: tb.column_entries.first.text}} - %> + <%= t("curation.table_entries") %> - <%= f.text_field :table_entries, :class => "select2", data: { value: tbData } %> + <%= f.text_field :table_entries, :class => "select2" %> <%#= f.select :universal_table, [], {}, class: 'form-control select2', multiple: true, data: { value: tbData } %> <% else %>