0

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!

2
  • What did you try? Please show us some of your code Commented Jun 9, 2015 at 8:06
  • I guess you'd need to use a recursive function Commented Jun 9, 2015 at 8:10

1 Answer 1

1

You can do something like this very easily with recursion.

Here's a rough example.

DEMO

function toggleChildren (e) {
  e.stopPropagation();
  
  var el = $(this);
  el.children('ul').toggle();
}

function buildNests (obj, parent) {
  var item = $('<li />', {
    text: obj.namn,
    on: {
      click: toggleChildren
    }
  }),
  list = $('<ul />', {
    html: [item]
  });
  
  parent.append(list);
  
  if (obj.items.length > 0) {
    obj.items.forEach(function (e) {
      buildNests(e, item);
    });
  }
}

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: []
        }]
    }]
};

buildNests(data, $('#con'));
<div id="con"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.