From 72899530cf153b8b08b079057fea67d3d64ca46e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 26 Aug 2020 23:43:19 +1000 Subject: [PATCH] Add helper function to simplify reloading of field options --- .../static/script/inventree/modals.js | 47 +++++++++++++++++++ InvenTree/stock/templates/stock/location.html | 15 +++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/static/script/inventree/modals.js b/InvenTree/InvenTree/static/script/inventree/modals.js index a17e148416..e0a24b4e4f 100644 --- a/InvenTree/InvenTree/static/script/inventree/modals.js +++ b/InvenTree/InvenTree/static/script/inventree/modals.js @@ -87,6 +87,53 @@ function setFieldOptions(fieldName, optionList, options={}) { } +function reloadFieldOptions(fieldName, options) { + /* Reload the options for a given field, + * using an AJAX request. + * + * Args: + * - fieldName: The name of the field + * - options: + * -- url: Query url + * -- params: Query params + * -- value: A function which takes a returned option and returns the 'value' (if not specified, the `pk` field is used) + * -- text: A function which takes a returned option and returns the 'text' + * -- title: A function which takes a returned option and returns the 'title' (optional!) + */ + + inventreeGet(options.url, options.params, { + success: function(response) { + var opts = makeOptionsList(response, + function(item) { + return options.text(item); + }, + function(item) { + if (options.value) { + return options.value(item); + } else { + // Fallback is to use the 'pk' field + return item.pk; + } + }, + function(item) { + if (options.title) { + return options.title(item); + } else { + return null; + } + } + ); + + // Update the target field with the new options + setFieldOptions(fieldName, opts); + }, + error: function(response) { + console.log("Error GETting field options"); + } + }); +} + + function partialMatcher(params, data) { /* Replacement function for the 'matcher' parameter for a select2 dropdown. diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index d55c8bd397..9ef74f238b 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -216,7 +216,20 @@ { field: 'part', action: function(value) { - console.log('value: ' + value); + + reloadFieldOptions( + 'supplier_part', + { + url: "{% url 'api-supplier-part-list' %}", + params: { + part: value, + pretty: true, + }, + text: function(item) { + return item.pretty_name; + } + } + ) } }, ],