I'm trying to dynamically build a cascading dropdown from a nested JSON object which can be of any depth but gotten stuck on the implementation.
The JSON from the server looks like this:
var data = {
typ: 'Gren',
namn: 'Bana',
items: [{
typ: 'Disciplin',
namn: 'Eliminiering',
items: [{
typ: 'Klass',
namn: 'Damer Elit',
items: []
}, {
typ: 'Klass',
namn: 'Damer Junior',
items: []
}]
}, {
typ: 'Disciplin',
namn: 'Poänglopp',
items: [{
typ: 'Klass',
namn: 'Damer Senior',
items: []
}, {
typ: 'Klass',
namn: 'Flickor 13-14',
items: []
}]
}]
};
So the first dropdown should contain two items: Eliminering and Poänglopp. The second dropdown should contain either: Damer Elit and Damer Junior if Elimiering in the first dropdown is selected. If Poänglopp is selected, the second dropdown should contain: Damer Senior and Flickor 13-14.
And so on.
If the items array is empty, there are no sub-categories so no more dropdowns needs to be created. Again: The arrays can be of any depth.
Any help/ideas how to implement this is greatly appreciated!