It is a complex case, but I found the solution:
I assume you can have structure like this:
[{
name: "Group 1",
options: [
{caption: "Option 1", value: "1"},
{caption: "Option 2", value: "2"}
]
}, {
name: "Group 2",
options: [
{caption: "Option 3", value: "3"},
{caption: "Option 4", value: "4"}
]
}
]
But it's not ready for select component, because we cant bind ng-click on or tags and also we need proper order. Updated options array:
[
{
"caption":"Group 1",
"type":"group",
"isActive":true,
"parentGroupSelected":false
},
{
"caption":"Option 1",
"value":"1",
"type":"option",
"parentGroupSelected":false,
"parentGroup":"Group 1"
},
{
"caption":"Option 2",
"value":"2",
"type":"option",
"parentGroupSelected":false,
"parentGroup":"Group 1"},
{
"caption":"Group 2",
"type":"group",
"isActive":true,
"parentGroupSelected":false
},
{
"caption":"Option 3",
"value":"3",
"type":"option",
"parentGroupSelected":true,
"parentGroup":"Group 2"
},
{
"caption":"Option 4",
"value":"4",
"type":"option",
"parentGroupSelected":true,
"parentGroup":"Group 2"
}]
Template (HMTL)
<div data-ng-controller="AppController as vm">
<select data-ng-model="vm.selectModel" data-ng-change="vm.handler(vm.selectModel)">
<option ng-value="item" data-ng-if="vm.checkVisibility(item)" data-ng-repeat="item in vm.optionsInline track by $index" >
<span>{{item.caption}}</span>
</option>
</select>
</div>
We need two functions:
- Handle click event - actually we have to put it in onchange listener
- Check visibility of actual options
JS
Visibility check function:
vm.checkVisibility = function(option) {
if(option.type == 'group') {
return true;
}
if(option.type == 'option') {
if(option.parentGroupSelected == true) {
return true;
}
}
return false;
}
Onclick (Change) handler:
vm.handler = function(option) {
if(option.type == 'group') {
option.isActive = !option.isActive;
vm.optionsInline.forEach(function(optionItem){
optionItem.parentGroupSelected = false; // accordion mode
if(optionItem.type == 'option') {
if(optionItem.parentGroup == option.caption) {
optionItem.parentGroupSelected = option.isActive;
}
}
})
}
}
If you still have questions you can refer to my jsfiddle example below
JSFiddle example