Search code examples
jqueryjqtree

jqtree not showing custom attributes


In the jqtree docs, it says you can add custom attributes to the data:

You can also include other data in the objects. You can later access this data. For example, to add an id:

{
    label: 'node1',
    id: 1
}

I am sending it JSON that looks like this:

[
  {
    "label": "foo",
    "id": "12115",
    "children": [
      {
        "label": "bar",
        "id": "15273",
        "children": []
      },
      {
        "label": "bin",
        "id": "15288",
        "children": []
      },
      {
        "label": "baz",
        "id": "33263",
        "children": []
      }
    ]
  }
]

I'm implementing it like this:

$(this).tree({
    dataUrl: 'http://my/server/url/',
    autoOpen: true,
    dragAndDrop: false
});

The trees show up fine, but it doesn't pass the 'id' fields through in any way that I can determine. Am I doing something wrong, or is that functionality no longer supported?


Solution

  • Jqtree does not add an id attribute to the html. It does store the id attribute internally.

    You can use the getNodeById function to access the internal node.

    var node = $(this).tree('getNodeById', '15273');
    console.log(node.name);
    console.log(node.id);
    

    If you want to add the id attribute to the html, you could use the onCreateLi option. Also see this example.