From 6b7a0fde1bc4afca1f8484d29a9c00ffdae6fe4a Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 3 Mar 2022 17:17:27 +1100 Subject: [PATCH] Store table query parameters when performing a bootstrap-table query - For now it only supports .csv format --- InvenTree/stock/templates/stock/location.html | 7 +-- InvenTree/templates/js/translated/tables.js | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index 4c98db529b..575a798fb2 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -240,12 +240,7 @@ {% endif %} $("#stock-export").click(function() { - - exportStock({ - {% if location %} - location: {{ location.pk }} - {% endif %} - }); + downloadTableData($('#stock-table')); }); $('#location-create').click(function () { diff --git a/InvenTree/templates/js/translated/tables.js b/InvenTree/templates/js/translated/tables.js index c2418dbe78..b295c3f89a 100644 --- a/InvenTree/templates/js/translated/tables.js +++ b/InvenTree/templates/js/translated/tables.js @@ -7,6 +7,7 @@ /* exported customGroupSorter, + downloadTableData, reloadtable, renderLink, reloadTableFilters, @@ -21,6 +22,42 @@ function reloadtable(table) { } +/** + * Download data from a table, via the API. + * This requires a number of conditions to be met: + * + * - The API endpoint supports data download (on the server side) + * - The table is "flat" (does not support multi-level loading, etc) + * - The table has been loaded using the inventreeTable() function, not bootstrapTable() + * (Refer to the "reloadTableFilters" function to see why!) + */ +function downloadTableData(table) { + + // Extract table configuration options + var options = table.bootstrapTable('getOptions'); + + var url = options.url; + + if (!url) { + console.log("Error: downloadTableData could not find 'url' parameter"); + } + + var query_params = options.query_params || {}; + + url += '?'; + + for (const [key, value] of Object.entries(query_params)) { + url += `${key}=${value}&`; + } + + var format = 'csv'; + + url += `export=${format}`; + + location.href = url; +} + + /** * Render a URL for display * @param {String} text @@ -114,6 +151,10 @@ function reloadTableFilters(table, filters) { } } + // Store the total set of query params + // This is necessary for the "downloadTableData" function to work + options.query_params = params; + options.queryParams = function(tableParams) { return convertQueryParameters(tableParams, params); }; @@ -221,7 +262,11 @@ $.fn.inventreeTable = function(options) { // Extract query params var filters = options.queryParams || options.filters || {}; + // Store the total set of query params + options.query_params = filters; + options.queryParams = function(params) { + // Update the query parameters callback with the *new* filters return convertQueryParameters(params, filters); };