From 66a69152139805495c92d8f2f786998e75d00db4 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 1 Jun 2022 17:23:36 +1000 Subject: [PATCH] Adds 'multi delete' for attachment tables (#3111) * Adds 'multi delete' for attachment tables - Should roll out seamlessly to any attachment table * JS linting --- InvenTree/build/templates/build/detail.html | 2 +- InvenTree/templates/attachment_table.html | 12 +++ .../templates/js/translated/attachment.js | 91 +++++++++++++++++-- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/InvenTree/build/templates/build/detail.html b/InvenTree/build/templates/build/detail.html index f99a7efb95..248fcec580 100644 --- a/InvenTree/build/templates/build/detail.html +++ b/InvenTree/build/templates/build/detail.html @@ -375,7 +375,7 @@ onPanelLoad('attachments', function() { }, label: 'attachment', success: function(data, status, xhr) { - location.reload(); + $('#attachment-table').bootstrapTable('refresh'); } } ); diff --git a/InvenTree/templates/attachment_table.html b/InvenTree/templates/attachment_table.html index d1562cf5f5..2db4c6345f 100644 --- a/InvenTree/templates/attachment_table.html +++ b/InvenTree/templates/attachment_table.html @@ -2,6 +2,18 @@
+ {% include "filter_list.html" with id="attachments" %}
diff --git a/InvenTree/templates/js/translated/attachment.js b/InvenTree/templates/js/translated/attachment.js index dd1fe31c66..6367037005 100644 --- a/InvenTree/templates/js/translated/attachment.js +++ b/InvenTree/templates/js/translated/attachment.js @@ -57,6 +57,75 @@ function addAttachmentButtonCallbacks(url, fields={}) { } +/* + * Construct a form to delete attachment files + */ +function deleteAttachments(attachments, url, options={}) { + + if (attachments.length == 0) { + console.warn('deleteAttachments function called with zero attachments provided'); + return; + } + + function renderAttachment(attachment, opts={}) { + + var icon = ''; + + if (attachment.filename) { + icon = ``; + } else if (attachment.link) { + icon = ``; + } + + return ` + + ${icon} + ${attachment.filename || attachment.link} + ${attachment.comment} + `; + } + + var rows = ''; + + attachments.forEach(function(att) { + rows += renderAttachment(att); + }); + + var html = ` +
+ {% trans "All selected attachments will be deleted" %} +
+ + + + + + + ${rows} +
{% trans "Attachment" %}{% trans "Comment" %}
+ `; + + constructFormBody({}, { + method: 'DELETE', + title: '{% trans "Delete Attachments" %}', + preFormContent: html, + onSubmit: function(fields, opts) { + inventreeMultiDelete( + url, + attachments, + { + modal: opts.modal, + success: function() { + // Refresh the table once all attachments are deleted + $('#attachment-table').bootstrapTable('refresh'); + } + } + ); + } + }); +} + + function reloadAttachmentTable() { $('#attachment-table').bootstrapTable('refresh'); @@ -71,6 +140,15 @@ function loadAttachmentTable(url, options) { addAttachmentButtonCallbacks(url, options.fields || {}); + // Add callback for the 'multi delete' button + $('#multi-attachment-delete').click(function() { + var attachments = getTableData(table); + + if (attachments.length > 0) { + deleteAttachments(attachments, url); + } + }); + $(table).inventreeTable({ url: url, name: options.name || 'attachments', @@ -80,7 +158,9 @@ function loadAttachmentTable(url, options) { sortable: true, search: true, queryParams: options.filters || {}, + uniqueId: 'pk', onPostBody: function() { + // Add callback for 'edit' button $(table).find('.button-attachment-edit').click(function() { var pk = $(this).attr('pk'); @@ -105,15 +185,14 @@ function loadAttachmentTable(url, options) { $(table).find('.button-attachment-delete').click(function() { var pk = $(this).attr('pk'); - constructForm(`${url}${pk}/`, { - method: 'DELETE', - confirmMessage: '{% trans "Confirm Delete" %}', - title: '{% trans "Delete Attachment" %}', - onSuccess: reloadAttachmentTable, - }); + var attachment = $(table).bootstrapTable('getRowByUniqueId', pk); + deleteAttachments([attachment], url); }); }, columns: [ + { + checkbox: true, + }, { field: 'attachment', title: '{% trans "Attachment" %}',