From a0932b362e0aa457f8625b9e2d52bafee5a2e038 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 17 May 2023 15:51:49 +0200 Subject: [PATCH] Bug 33568: Preserve item selection across pagination and filtering MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit BTW Do we really need StaffDetailItemSelection? Signed-off-by: Owen Leonard Signed-off-by: Laurence Rault Signed-off-by: Emily Lamancusa Signed-off-by: Tomás Cohen Arazi Signed-off-by: Katrin Fischer --- .../prog/en/modules/catalogue/detail.tt | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index bc7c74d6b6..6c5350f7a4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -1332,11 +1332,10 @@ } [% IF StaffDetailItemSelection %] - function itemSelectionBuildDeleteLink(div) { - var itemnumbers = new Array(); - $("input[name='itemnumber'][type='checkbox']:checked", div).each(function() { - itemnumbers.push($(this).val()); - }); + + let items_selection = {}; + function itemSelectionBuildDeleteLink(tab_id) { + var itemnumbers = items_selection[tab_id]; if (itemnumbers.length > 0) { var url = '/cgi-bin/koha/tools/batchMod.pl?op=show&del=1'; url += '&itemnumber=' + itemnumbers.join('&itemnumber='); @@ -1349,11 +1348,8 @@ return true } - function itemSelectionBuildModifyLink(div) { - var itemnumbers = new Array(); - $("input[name='itemnumber'][type='checkbox']:checked", div).each(function() { - itemnumbers.push($(this).val()); - }); + function itemSelectionBuildModifyLink(tab_id) { + var itemnumbers = items_selection[tab_id]; if (itemnumbers.length > 0) { var url = '/cgi-bin/koha/tools/batchMod.pl?op=show'; url += '&itemnumber=' + itemnumbers.join('&itemnumber='); @@ -1366,10 +1362,10 @@ return true; } - function itemSelectionBuildActionLinks(tab) { - var div = $("#" + tab); - var delete_link_ok = itemSelectionBuildDeleteLink(div); - var modify_link_ok = itemSelectionBuildModifyLink(div); + function itemSelectionBuildActionLinks(tab_id) { + var delete_link_ok = itemSelectionBuildDeleteLink(tab_id); + var modify_link_ok = itemSelectionBuildModifyLink(tab_id); + var div = $("#" + tab_id); if (modify_link_ok || delete_link_ok) { $('.itemselection_actions', div).show(); } else { @@ -1378,10 +1374,6 @@ } $(document).ready(function() { - $('table.items_table').each(function() { - var div = $(this).parent().attr("id"); - itemSelectionBuildActionLinks(div); - }); $(".SelectAll").on("click",function(e){ e.preventDefault(); @@ -2220,6 +2212,9 @@ [% IF hidden_count %] default_filters.lost_status = "0"; [% END %] + if ( !items_selection.hasOwnProperty(tab_id) ){ + items_selection[tab_id] = []; + } var items_table = $("#" + tab_id + '_table').kohaTable({ ajax: { url: item_table_url }, order: [[ 0, "asc" ]], @@ -2234,7 +2229,11 @@ orderable: false, render: function (data, type, row, meta) { if ( can_edit_items_from[row.holding_library_id] ){ - return ''.format(row.item_id); + if ( items_selection[tab_id].includes(row.item_id) ) { + return ''.format(row.item_id); + } else { + return ''.format(row.item_id); + } } else { return '' } @@ -2686,11 +2685,28 @@ }); itemSelectionBuildActionLinks(tab_id); }, + [% IF StaffDetailItemSelection %] drawCallback: function(settings){ - $(this).find("td:first input[name='itemnumber'][type='checkbox']").on("change", function(){ - itemSelectionBuildActionLinks(tab_id); - }); + var api = new $.fn.dataTable.Api(settings) + $.each( + $(this).find("tbody tr td:first-child"), + function (index, e) { + let tr = $(this).parent() + let row = api.row(tr).data() + if (!row) return // Happen if the table is empty + $(this).find("input[name='itemnumber'][type='checkbox']").on("change", function(){ + let itemnumber = parseInt($(this).val()); + if( $(this).prop("checked") ){ + items_selection[tab_id].push(itemnumber); + } else { + items_selection[tab_id] = items_selection[tab_id].filter( id => id != itemnumber ); + } + itemSelectionBuildActionLinks(tab_id); + }); + } + ); }, + [% END %] ...dt_options, }, items_table_settings[tab_id], -- 2.39.5