From 693d24b4b64bf247275be2875f90e7cdca0a7855 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 22 Jun 2023 10:29:14 +1000 Subject: [PATCH] Table filters refactor (#5086) - Fix a couple of bugs - Code cleanup --- InvenTree/templates/js/translated/build.js | 6 +----- InvenTree/templates/js/translated/filters.js | 9 ++++----- InvenTree/templates/js/translated/part.js | 18 ++++++------------ InvenTree/templates/js/translated/stock.js | 9 ++------- .../templates/js/translated/table_filters.js | 2 +- InvenTree/templates/js/translated/tables.js | 16 ++++------------ 6 files changed, 18 insertions(+), 42 deletions(-) diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index 422250e7f1..626c233c08 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -1086,11 +1086,7 @@ function loadBuildOutputTable(build_info, options={}) { params.is_building = true; params.build = build_info.pk; - var filters = {}; - - for (var key in params) { - filters[key] = params[key]; - } + var filters = Object.assign({}, params); setupFilterList('builditems', $(table), options.filterTarget || '#filter-list-incompletebuilditems', { labels: { diff --git a/InvenTree/templates/js/translated/filters.js b/InvenTree/templates/js/translated/filters.js index 201591ac5e..51fcc265b6 100644 --- a/InvenTree/templates/js/translated/filters.js +++ b/InvenTree/templates/js/translated/filters.js @@ -74,8 +74,7 @@ function loadTableFilters(tableKey, query={}) { }); // Override configurable filters with hard-coded query - Object.assign(filters, query); - + filters = Object.assign(filters, query); return filters; } @@ -488,9 +487,9 @@ function setupFilterList(tableKey, table, target, options={}) { `); for (var key in filters) { - var value = getFilterOptionValue(tableKey, key, filters[key]); - var title = getFilterTitle(tableKey, key); - var description = getFilterDescription(tableKey, key); + let value = getFilterOptionValue(tableKey, key, filters[key]); + let title = getFilterTitle(tableKey, key); + let description = getFilterDescription(tableKey, key); var filter_tag = `
diff --git a/InvenTree/templates/js/translated/part.js b/InvenTree/templates/js/translated/part.js index 8c7df7a045..ebce845a7c 100644 --- a/InvenTree/templates/js/translated/part.js +++ b/InvenTree/templates/js/translated/part.js @@ -1895,11 +1895,7 @@ function loadRelatedPartsTable(table, part_id, options={}) { options.params.part = part_id; - var filters = {}; - - for (var key in options.params) { - filters[key] = options.params[key]; - } + var filters = Object.assign({}, options.params); setupFilterList('related', $(table), options.filterTarget); @@ -2242,8 +2238,6 @@ function loadPartTable(table, url, options={}) { // Ensure category detail is included options.params['category_detail'] = true; - var params = options.params || {}; - let filters = {}; if (!options.disableFilters) { @@ -2268,6 +2262,9 @@ function loadPartTable(table, url, options={}) { }); } + // Update fields with passed parameters + filters = Object.assign(filters, options.params); + var columns = [ { field: 'pk', @@ -2435,7 +2432,7 @@ function loadPartTable(table, url, options={}) { name: table_name, queryParams: filters, groupBy: false, - original: params, + original: options.params, sidePagination: 'server', pagination: 'true', formatNoMatches: function() { @@ -2781,10 +2778,7 @@ function loadPartTestTemplateTable(table, options) { setupFilterList('parttests', table, filterListElement); - // Override the default values, or add new ones - for (var key in params) { - filters[key] = params[key]; - } + filters = Object.assign(filters, params); table.inventreeTable({ method: 'get', diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index 95cef81533..1299ad81dd 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -1886,10 +1886,7 @@ function loadStockTable(table, options) { }); } - // Override the default values, or add new ones - for (var key in params) { - filters[key] = params[key]; - } + filters = Object.assign(filters, params); var col = null; @@ -2450,9 +2447,7 @@ function loadStockLocationTable(table, options) { plural_name: '{% trans "stock locations" %}', }); - for (var key in params) { - filters[key] = params[key]; - } + filters = Object.assign(filters, params); // Function to request sub-location items function requestSubItems(parent_pk) { diff --git a/InvenTree/templates/js/translated/table_filters.js b/InvenTree/templates/js/translated/table_filters.js index f52cd9dd5c..fba4ff52d1 100644 --- a/InvenTree/templates/js/translated/table_filters.js +++ b/InvenTree/templates/js/translated/table_filters.js @@ -471,7 +471,7 @@ function getBuildTableFilters() { async: false, success: function(response) { for (var key in response) { - var owner = response[key]; + let owner = response[key]; ownersList[owner.pk] = { key: owner.pk, value: `${owner.name} (${owner.label})`, diff --git a/InvenTree/templates/js/translated/tables.js b/InvenTree/templates/js/translated/tables.js index 1d68451f90..54b702690c 100644 --- a/InvenTree/templates/js/translated/tables.js +++ b/InvenTree/templates/js/translated/tables.js @@ -252,17 +252,11 @@ function reloadTableFilters(table, filters, options={}) { options = table.bootstrapTable('getOptions'); // Construct a new list of filters to use for the query - var params = {}; - - for (var k in filters) { - params[k] = filters[k]; - } + let params = Object.assign({}, filters); // Original query params will override - if (options.original != null) { - for (var key in options.original) { - params[key] = options.original[key]; - } + if (options.original) { + params = Object.assign(params, options.original); } // Store the total set of query params @@ -318,9 +312,7 @@ function convertQueryParameters(params, filters) { } - for (var key in filters) { - params[key] = filters[key]; - } + params = Object.assign(params, filters); // Add "order" back in (if it was originally specified by InvenTree) // Annoyingly, "order" shadows some field names in InvenTree...