I need to display custom string values base on the selected values from an object array. I think I'm close but there is a small issue that I cannot figure out.
Right now If I selected everything from "Gained" and one Item from "Static" the return statement will be "Gained" and if I selected all from "Static" and one item from "Gained" the result will be Static,
HTML Block
<a class="customSegmentSelectorButton" title="{{ ::$parent.Labels.SEGMENTSELECT }}">
<span ng-if="totalSelectedOptions() == 0">No KPIs</span>
<span ng-if="totalSelectedOptions() > 0">Sum of {{selectedOptionsLabel()}} </span>
</a>
Component
customSegmentOptions = [
{
Label: "Gained",
Children: [
{
Label: "New",
Value: "New",
Selected: true
},
{
Label: "Win",
Value: "Win",
Selected: true
},
{
Label: "Add-On",
Value: "AddOn",
Selected: true
}
]
},
{
Label: "Static",
Children: [
{
Label: "Restart",
Value: "Restart",
Selected: true
},
{
Label: "Repeat",
Value: "Repeat",
Selected: true
}
]
}
];
selectedOptionsLabel = function(){
var customSegmentOptions = scope.$parent.customSegmentOptions;
var isGainedSelected = false;
var isStaticSelected = false;
var isChildrenSelected = false;
var selectedChildrenToDisplay = [];
for(var index = 0; index < customSegmentOptions.length; index++){
var item = customSegmentOptions[index];
var children = item.Children.filter(x => x.Selected == true);
if(item.Children.length == children.length){
if(item.Label.toLowerCase() == 'gained'){
isGainedSelected = true;
}else{
isStaticSelected = true;
}
}
selectedChildrenToDisplay = selectedChildrenToDisplay.concat(children.map(x => x.Label));
}
isChildrenSelected = selectedChildrenToDisplay.length < 5 && !isGainedSelected ? true : false;
switch (true) {
case isGainedSelected && !isStaticSelected && !isChildrenSelected:
return "Gained";
case isStaticSelected && !isGainedSelected && !isChildrenSelected:
return "Static";
case isStaticSelected && isGainedSelected:
return "Gained & Static";
case (!isStaticSelected || !isGainedSelected) && isChildrenSelected:
console.log(selectedChildrenToDisplay);
return "children"
}
};
What I am trying to build is when all of one section is selected then I need to return a string such as
- "Sum of Gained" - if only Gained is selected
- "Sum of Static - if only Static is selected
- "Sum of Gained & Static" - if both Gained and Static are selected
When all of one section is NOT selected then I want to list the individual children names separated by commas when applicable and the last comma should be a "&" for example - "Sum of New, Win & Restart")