I am working on a Plumsail form where I have a multi-select choice column (entitytype) in a SharePoint list. Based upon the values contained in the column (e.g., ['Normal', 'ExtendedA', 'ExtendedB']), I wish to hide or show other columns (e.g., if value contains Normal, hide columns; if the value contains ExtendedA or ExtendedB, show columns).
After reviewing the 'Work with SharePoint form fields in JavaScript' post I could do so with a single-select choice column, but not a multi-select choice column. I am certain it is because what I am doing is searching an array to validate if it contains a string, not looking for equivalency.
I found 'Hiding a field based on contents of SP checkboxes' post and tried to adapt that. In addition, I tried working with .includes. Neither of those two options worked Would someone please assist as I am sure it is something in my syntax as I am new to JavaScript?
Code that works for single-select choice column:
window.fd = fd;
window.$ = $;
// Hides excol-fields based on the value of the formtype field (a single-select choice) upon change
function showHideFields(){
fd.field('excoltext').hidden = true;
fd.field('excolchoice').hidden = true;
fd.field('excolnumber').hidden = true;
if(fd.field('formtype').value === 'Normal'){
fd.field('excoltext').hidden = false;
fd.field('excolchoice').hidden = false;
}
else if(fd.field('formtype').value === 'Extended'){
fd.field('excolnumber').hidden = false;
}
else{
}
}
fd.spRendered(() => {
// Clears excol-fields based on the value of the formtype field upon change
function clearFields() {
if (fd.field('formtype').value == 'Normal') {
// Clear fields of any value
fd.field('excolnumber').clear();
}
else if (fd.field('formtype').value == 'Extended') {
fd.field('excoltext').clear();
fd.field('excolchoice').clear();
}
else{
}
}
// Calling clearFields when formtype value changes
fd.field('formtype').$on('change', clearFields);
// Calling showHideFields when formtype value changes
fd.field('formtype').$on('change', value => {
showHideFields();
});
showHideFields();
})
My attempt at adapted code using a multi-select choice column that does not work:
window.fd = fd;
window.$ = $;
fd.spRendered(function() {
function hideOrShowFields() {
fd.field('excoltext').hidden = true;
fd.field('excolchoice').hidden = true;
fd.field('excolnumber').hidden = true;
if (fd.field('entitytype').value.indexOf("Normal") >= 0) {
// Hide the excolnumber field and show the others
$(fd.field('excoltext').$parent.$el).show();
$(fd.field('excolchoice').$parent.$el).show();
$(fd.field('excolnumber').$parent.$el).hide();
} else {
// Show the excolnumber field and hide the others
$(fd.field('excoltext').$parent.$el).hide();
$(fd.field('excolchoice').$parent.$el).hide();
$(fd.field('excolnumber').$parent.$el).show();
} else if {
(fd.field('entitytype').value.indexOf("ExtendedA") >= 0) {
// Show the excolnumber field and hide the others
$(fd.field('excoltext').$parent.$el).hide();
$(fd.field('excolchoice').$parent.$el).hide();
$(fd.field('excolnumber').$parent.$el).show();
} else {
// Hide the excolnumber field and show the others
$(fd.field('excoltext').$parent.$el).show();
$(fd.field('excolchoice').$parent.$el).show();
$(fd.field('excolnumber').$parent.$el).hide();
}
} else if {
(fd.field('entitytype').value.indexOf("ExtendedB") >= 0) {
// Show the excolnumber field and hide the others
$(fd.field('excoltext').$parent.$el).hide();
$(fd.field('excolchoice').$parent.$el).hide();
$(fd.field('excolnumber').$parent.$el).show();
}
} else {
// Hide the excolnumber field and show the others
$(fd.field('excoltext').$parent.$el).show();
$(fd.field('excolchoice').$parent.$el).show();
$(fd.field('excolnumber').$parent.$el).hide();
}
}
// Calling hideOrShowFields when the user changes the entitytype
fd.field('entitytype').$on('change',hideOrShowFields);
// Calling hideOrShowFields on form loading
hideOrShowFields();
});
Also tried with .includes as a parameter for the multi-select choice column's value but did not work
window.fd = fd;
window.$ = $;
// Hides excol-fields based on the value of the 'entitytype' field upon change
function showHideFields(){
fd.field('excoltext').hidden = true;
fd.field('excolchoice').hidden = true;
fd.field('excolnumber').hidden = true;
// Check for multi-select choice column 'entitytype'
if(fd.field('entitytype').value.includes('Normal')){
fd.field('excoltext').hidden = false;
fd.field('excolchoice').hidden = false;
}
if(fd.field('entitytype').value.includes('Extended')){
fd.field('excolnumber').hidden = false;
}
}
fd.spRendered(() => {
fd.field('entitytype').$on('change', value => {
showHideFields();
});
showHideFields();
})