From fe3d57ce9e81260195a3c67f82e72eb2b9670d93 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 21 Apr 2022 19:23:14 +0200 Subject: [PATCH] Bug 30584: Fix ES mapping creation We need to tell DT that we are adding/removing row, not only deal manually with the DOM. This patch also fixes the following bug: if you add or remove a row from the "biblio" tab, then go to another tab and back to "biblio", all changes were gone (bug existed prior to bug 29893). Test plan: Add, edit and remove mappings, switch from tabs and save. Use the filters, save (29893 regression test) Signed-off-by: David Nind Signed-off-by: Nick Clemens Signed-off-by: Fridolin Somers --- .../prog/js/elasticsearch-mappings.js | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js b/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js index ad10a7ffb5..b1f9109b45 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js +++ b/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js @@ -53,7 +53,9 @@ $(document).ready(function () { alert( __("This field is mandatory and must have at least one mapping") ); return; } else { - $(this).parents('tr').remove(); + var table = $(this).closest('table'); + let dt = $(table).DataTable(); + dt.row( $(this).closest('tr') ).remove().draw(); } }); @@ -63,15 +65,22 @@ $(document).ready(function () { $('.add').click(function () { var table = $(this).closest('table'); + let table_id = table.attr('id'); var index_name = $(table).attr('data-index_name'); var line = $(this).closest("tr"); var marc_field = $(line).find('input[data-id="mapping_marc_field"]').val(); if (marc_field.length > 0) { var new_line = clone_line(line); new_line.appendTo($('table[data-index_name="' + index_name + '"]>tbody')); - $('.delete').click(function () { - $(this).parents('tr').remove(); - }); + let dt = $('#' + table_id).DataTable(); + dt.row.add(new_line).draw(); + + $(table).on( 'click', '.delete', function () { + var table = $(this).closest('table'); + let dt = $(table).DataTable(); + dt.row( $(this).closest('tr') ).remove().draw(); + } ); + clean_line(line); $(table).tableDnD({ @@ -84,9 +93,20 @@ $(document).ready(function () { }); $("#es_mappings").on("submit", function(e){ - $("#search_fields_table").DataTable({ paging: false }).search('').draw(); - $("#mapping_biblios_table").DataTable({ paging: false }).search('').draw(); - $("#mapping_authorities_table").DataTable({ paging: false }).search('').draw(); + let table_ids = ['search_fields_table', 'mapping_biblios_table', 'mapping_authorities_table']; + $(table_ids).each(function(){ + let table; + // Remove warning "Cannot reinitialise DataTable" + if ( $.fn.dataTable.isDataTable( '#' + this ) ) { + table = $('#' + this).DataTable(); + } + else { + table = $('#' + this).DataTable( { + paging: false + } ); + } + table.search('').draw(); + }); return true; }); }); -- 2.39.5