More refactoring

This commit is contained in:
Oliver Walters 2020-04-11 14:47:41 +10:00
parent e3102900b6
commit 2f71233d62
3 changed files with 39 additions and 36 deletions

View File

@ -294,7 +294,7 @@ function setupFilterList(tableKey, table, target) {
var filters = addTableFilter(tableKey, tag, val); var filters = addTableFilter(tableKey, tag, val);
reloadStockTable(table, filters); reloadTable(table, filters);
// Run this function again // Run this function again
setupFilterList(tableKey, table, target); setupFilterList(tableKey, table, target);
@ -311,7 +311,7 @@ function setupFilterList(tableKey, table, target) {
element.find(`#${clear}`).click(function() { element.find(`#${clear}`).click(function() {
var filters = clearTableFilters(tableKey); var filters = clearTableFilters(tableKey);
reloadStockTable(table, filters); reloadTable(table, filters);
setupFilterList(tableKey, table, target); setupFilterList(tableKey, table, target);
}); });
@ -324,7 +324,7 @@ function setupFilterList(tableKey, table, target) {
var filters = removeTableFilter(tableKey, filter); var filters = removeTableFilter(tableKey, filter);
reloadStockTable(table, filters); reloadTable(table, filters);
// Run this function again! // Run this function again!
setupFilterList(tableKey, table, target); setupFilterList(tableKey, table, target);

View File

@ -30,39 +30,6 @@ function removeStockRow(e) {
} }
function reloadStockTable(table, filters) {
/* Reload the stock table.
*
* 'original' is the original query params provided to the
* 'loadStockTable' function.
* These override any user-configured filters.
*/
// Override the queryParams for the table
var options = table.bootstrapTable('getOptions');
var params = {};
var filters = loadTableFilters("stock");
for (var key in filters) {
params[key] = filters[key];
}
// Original parameters will override
for (var key in options.original) {
params[key] = options.original[key];
}
options.queryParams = params;
table.bootstrapTable('refreshOptions', options);
table.bootstrapTable('refresh');
}
function loadStockTable(table, options) { function loadStockTable(table, options) {
/* Load data into a stock table with adjustable options. /* Load data into a stock table with adjustable options.
* Fetches data (via AJAX) and loads into a bootstrap table. * Fetches data (via AJAX) and loads into a bootstrap table.

View File

@ -44,6 +44,42 @@ function isNumeric(n) {
} }
/*
* Reload a table which has already been made into a bootstrap table.
* New filters can be optionally provided, to change the query params.
*/
function reloadTable(table, filters) {
// Simply perform a refresh
if (filters == null) {
table.bootstrapTable('refresh');
return;
}
// More complex refresh with new filters supplied
var options = table.bootstrapTable('getOptions');
// Construct a new list of filters to use for the query
var params = {};
for (var key in filters) {
params[key] = filters[key];
}
// Original query params will override
if (options.original != null) {
for (var key in options.original) {
params[key] = options.original[key];
}
}
options.queryParams = params;
table.bootstrapTable('refreshOptions', options);
table.bootstrapTable('refresh');
}
/* Wrapper function for bootstrapTable. /* Wrapper function for bootstrapTable.
* Sets some useful defaults, and manage persistent settings. * Sets some useful defaults, and manage persistent settings.
*/ */