Store table query parameters when performing a bootstrap-table query

- For now it only supports .csv format
This commit is contained in:
Oliver 2022-03-03 17:17:27 +11:00
parent 549f16b7aa
commit 6b7a0fde1b
2 changed files with 46 additions and 6 deletions

View File

@ -240,12 +240,7 @@
{% endif %}
$("#stock-export").click(function() {
exportStock({
{% if location %}
location: {{ location.pk }}
{% endif %}
});
downloadTableData($('#stock-table'));
});
$('#location-create').click(function () {

View File

@ -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);
};