From 9d7f9a9aa35598bf822dea4c77231c0b35fff361 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 9 Jul 2021 12:31:25 +1000 Subject: [PATCH] Fix for strange table filtering bug - When a table was "refreshed" based on the selectable filters, table ordering was not observed - This was due to the original query parameter conversion not being observed - Refactored out the conversion function so it works correctly now - Also removed some cruft from generated query strings --- InvenTree/templates/js/filters.js | 6 +- InvenTree/templates/js/tables.js | 97 ++++++++++++++++++------------- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/InvenTree/templates/js/filters.js b/InvenTree/templates/js/filters.js index a27e91d5dc..4ee08affdf 100644 --- a/InvenTree/templates/js/filters.js +++ b/InvenTree/templates/js/filters.js @@ -314,7 +314,7 @@ function setupFilterList(tableKey, table, target) { // Only add the new filter if it is not empty! if (tag && tag.length > 0) { var filters = addTableFilter(tableKey, tag, val); - reloadTable(table, filters); + reloadTableFilters(table, filters); // Run this function again setupFilterList(tableKey, table, target); @@ -333,7 +333,7 @@ function setupFilterList(tableKey, table, target) { element.find(`#${clear}`).click(function() { var filters = clearTableFilters(tableKey); - reloadTable(table, filters); + reloadTableFilters(table, filters); setupFilterList(tableKey, table, target); }); @@ -346,7 +346,7 @@ function setupFilterList(tableKey, table, target) { var filters = removeTableFilter(tableKey, filter); - reloadTable(table, filters); + reloadTableFilters(table, filters); // Run this function again! setupFilterList(tableKey, table, target); diff --git a/InvenTree/templates/js/tables.js b/InvenTree/templates/js/tables.js index 96eb3f8123..8fedeb8f9e 100644 --- a/InvenTree/templates/js/tables.js +++ b/InvenTree/templates/js/tables.js @@ -68,7 +68,7 @@ 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) { +function reloadTableFilters(table, filters) { // Simply perform a refresh if (filters == null) { @@ -94,16 +94,11 @@ function reloadTable(table, filters) { } options.queryParams = function(tableParams) { - - for (key in params) { - tableParams[key] = params[key]; - } - - return tableParams; - } + return convertQueryParameters(tableParams, params); + }; table.bootstrapTable('refreshOptions', options); - table.bootstrapTable('refresh'); + table.bootstrapTable('refresh', filters); } @@ -122,6 +117,55 @@ function visibleColumnString(columns) { } +/* + * Convert bootstrap-table style parameters to "InvenTree" style +*/ +function convertQueryParameters(params, filters) { + + // Override the way that we ask the server to sort results + // It seems bootstrap-table does not offer a "native" way to do this... + if ('sort' in params) { + var order = params['order']; + + var ordering = params['sort'] || null; + + if (ordering) { + + if (order == 'desc') { + ordering = `-${ordering}`; + } + + params['ordering'] = ordering; + } + + delete params['sort']; + delete params['order']; + + } + + for (var key in filters) { + params[key] = filters[key]; + } + + // Add "order" back in (if it was originally specified by InvenTree) + // Annoyingly, "order" shadows some field names in InvenTree... + if ('order' in filters) { + params['order'] = filters['order']; + } + + // Remove searchable[] array (generated by bootstrap-table) + if ('searchable' in params) { + delete params['searchable']; + } + + if ('sortable' in params) { + delete params['sortable']; + } + + return params; +} + + /* Wrapper function for bootstrapTable. * Sets some useful defaults, and manage persistent settings. */ @@ -147,39 +191,8 @@ $.fn.inventreeTable = function(options) { var filters = options.queryParams || options.filters || {}; options.queryParams = function(params) { - - // Override the way that we ask the server to sort results - // It seems bootstrap-table does not offer a "native" way to do this... - if ('sort' in params) { - var order = params['order']; - - var ordering = params['sort'] || null; - - if (ordering) { - - if (order == 'desc') { - ordering = `-${ordering}`; - } - - params['ordering'] = ordering; - } - - delete params['sort']; - delete params['order']; - - } - - for (var key in filters) { - params[key] = filters[key]; - } - - // Add "order" back in (if it was originally specified by InvenTree) - // Annoyingly, "order" shadows some field names in InvenTree... - if ('order' in filters) { - params['order'] = filters['order']; - } - return params; - } + return convertQueryParameters(params, filters); + }; options.rememberOrder = true;