Make SETCHOICEFILTER more useful
J
Jeremy Palmer
At present, '... filtering ... uses case-insensitive "contains" comparison.' However, every time I have wanted to use this function it has been to filter
out
(i.e. hide) a specific choice. So I either write some code to pick the seven values (out of eight) which I don't want to filter out (which requires maintenance if I ever amend the choice list) or I just give up. Can we have an extra argument, please, to make it keep all the choice except
the matching ones? And it would be even better if it could use a regexp rather than a list of strings.Shawn Pickett
Jeremy Palmer - This is a bit of a work around, but you could use a helper function to wrap around the SETCHOICEFILTER that extends its functionality a bit. The code below will do what you are looking to do and keep the list of current values dynamic using the form (Please excuse the lack of formatting when it renders, I promise I did indent :) ):
const form = this;
function setChoiceListFilter (choiceListName, newValues, method = 'include') {
const choiceList = form.elementsByDataName[choiceListName];
/* Example of choice list object using this:
{ allow_other: true,
choice_list_id: null,
choices: [
{ label: 'A', value: 'A' },
{ label: 'B', value: 'B' },
{ label: 'C', value: 'C' },
{ label: 'D', value: 'D' } ],
data_name: 'feature_type',
default_previous_value: false,
description: null,
disabled: false,
hidden: false,
key: 'a559',
label: 'Feature Type',
multiple: false,
parent: undefined,
required: false,
required_conditions: [],
required_conditions_type: null,
type: 'ChoiceField',
use_choice_list: false,
visible_conditions: [],
visible_conditions_behavior: 'clear',
visible_conditions_type: null }
*/
// Grab the current values set in the form
const currentValues = choiceList.choices.map( function (choice) {
return choice.value;
});
let filteredValues = currentValues;
if (method === 'include' && newValues) {
// Do nothing; accept the new values as-is
filteredValues = currentValues.filter( function (value) {
return newValues.includes(value);
});
} else if (method ==='exclude' && newValues) {
filteredValues = currentValues.filter( function (value) {
return !newValues.includes(value);
});
}
SETCHOICEFILTER(choiceListName, filteredValues);
}
setChoiceListFilter('feature_type', ['A', 'B', 'C'], 'exclude');