+
${buttons}
`);
@@ -322,6 +327,13 @@ function setupFilterList(tableKey, table, target) {
$(table).bootstrapTable('refresh');
});
+ // Add a callback for downloading table data
+ if (options.download) {
+ element.find(`#download-${tableKey}`).click(function() {
+ downloadTableData($(table));
+ });
+ }
+
// Add a callback for adding a new filter
element.find(`#${add}`).click(function clicked() {
@@ -358,14 +370,14 @@ function setupFilterList(tableKey, table, target) {
reloadTableFilters(table, filters);
// Run this function again
- setupFilterList(tableKey, table, target);
+ setupFilterList(tableKey, table, target, options);
}
});
} else {
addClicked = false;
- setupFilterList(tableKey, table, target);
+ setupFilterList(tableKey, table, target, options);
}
});
@@ -376,7 +388,7 @@ function setupFilterList(tableKey, table, target) {
reloadTableFilters(table, filters);
- setupFilterList(tableKey, table, target);
+ setupFilterList(tableKey, table, target, options);
});
// Add callback for deleting each filter
@@ -390,7 +402,7 @@ function setupFilterList(tableKey, table, target) {
reloadTableFilters(table, filters);
// Run this function again!
- setupFilterList(tableKey, table, target);
+ setupFilterList(tableKey, table, target, options);
});
}
diff --git a/InvenTree/templates/js/translated/part.js b/InvenTree/templates/js/translated/part.js
index d42755a0f0..a0887f9473 100644
--- a/InvenTree/templates/js/translated/part.js
+++ b/InvenTree/templates/js/translated/part.js
@@ -1218,7 +1218,7 @@ function loadPartTable(table, url, options={}) {
filters[key] = params[key];
}
- setupFilterList('parts', $(table), options.filterTarget || null);
+ setupFilterList('parts', $(table), options.filterTarget, {download: true});
var columns = [
{
diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js
index 1ca89368dc..dcca969a28 100644
--- a/InvenTree/templates/js/translated/stock.js
+++ b/InvenTree/templates/js/translated/stock.js
@@ -43,7 +43,6 @@
duplicateStockItem,
editStockItem,
editStockLocation,
- exportStock,
findStockItemBySerialNumber,
installStockItem,
loadInstalledInTable,
@@ -506,49 +505,6 @@ function stockStatusCodes() {
}
-/*
- * Export stock table
- */
-function exportStock(params={}) {
-
- constructFormBody({}, {
- title: '{% trans "Export Stock" %}',
- fields: {
- format: {
- label: '{% trans "Format" %}',
- help_text: '{% trans "Select file format" %}',
- required: true,
- type: 'choice',
- value: 'csv',
- choices: exportFormatOptions(),
- },
- sublocations: {
- label: '{% trans "Include Sublocations" %}',
- help_text: '{% trans "Include stock items in sublocations" %}',
- type: 'boolean',
- value: 'true',
- }
- },
- onSubmit: function(fields, form_options) {
-
- var format = getFormFieldValue('format', fields['format'], form_options);
- var cascade = getFormFieldValue('sublocations', fields['sublocations'], form_options);
-
- // Hide the modal
- $(form_options.modal).modal('hide');
-
- var url = `{% url "stock-export" %}?format=${format}&cascade=${cascade}`;
-
- for (var key in params) {
- url += `&${key}=${params[key]}`;
- }
-
- location.href = url;
- }
- });
-}
-
-
/**
* Assign multiple stock items to a customer
*/
@@ -1615,7 +1571,7 @@ function loadStockTable(table, options) {
original[k] = params[k];
}
- setupFilterList(filterKey, table, filterTarget);
+ setupFilterList(filterKey, table, filterTarget, {download: true});
// Override the default values, or add new ones
for (var key in params) {
diff --git a/InvenTree/templates/js/translated/tables.js b/InvenTree/templates/js/translated/tables.js
index c2418dbe78..8a6674299c 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,62 @@ 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, opts={}) {
+
+ // Extract table configuration options
+ var table_options = table.bootstrapTable('getOptions');
+
+ var url = table_options.url;
+
+ if (!url) {
+ console.log('Error: downloadTableData could not find "url" parameter.');
+ }
+
+ var query_params = table_options.query_params || {};
+
+ url += '?';
+
+ constructFormBody({}, {
+ title: opts.title || '{% trans "Export Table Data" %}',
+ fields: {
+ format: {
+ label: '{% trans "Format" %}',
+ help_text: '{% trans "Select File Format" %}',
+ required: true,
+ type: 'choice',
+ value: 'csv',
+ choices: exportFormatOptions(),
+ }
+ },
+ onSubmit: function(fields, form_options) {
+ var format = getFormFieldValue('format', fields['format'], form_options);
+
+ // Hide the modal
+ $(form_options.modal).modal('hide');
+
+ for (const [key, value] of Object.entries(query_params)) {
+ url += `${key}=${value}&`;
+ }
+
+ url += `export=${format}`;
+
+ location.href = url;
+ }
+ });
+}
+
+
+
+
/**
* Render a URL for display
* @param {String} text
@@ -114,6 +171,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 +282,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);
};
diff --git a/InvenTree/templates/stock_table.html b/InvenTree/templates/stock_table.html
index a8a4ec6691..d609e78253 100644
--- a/InvenTree/templates/stock_table.html
+++ b/InvenTree/templates/stock_table.html
@@ -11,9 +11,6 @@