This is a migrated thread and some comments may be shown as answers.

Auto-generate IDs for local data

1 Answer 201 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
dandv
Top achievements
Rank 2
dandv asked on 04 Jan 2015, 12:23 PM
I have a tree where I know the first level of nodes, and want to load dynamically children of those nodes.

How can I create a DataSource based on local data, and not have to define IDs manually? It seems that IDs are mandatory for loading further remote data.

I'm coming from Webix, where you can simply write:

webix.ui({
    view: 'tree',
    data: [
      { value: 'Programs',
      }, {
        value: 'Governance',
        data: [
          {value: 'Regulations'},
          {value: 'Policies'},
          {value: 'Standards'}
        ]
      }
    ],
    on: {
      onItemClick: function (nodeId, el) {
           // nodeId is defined (automatically generated)
        
var node = this.getItem(nodeId);
        // will output the text of the clicked node
        webix.message(node.value);
           // load children of node.value via AJAX here...
      }
    }
});

With Kendo UI, if I don't define IDs, it seems there's no way to distinguish in the transport.read function between returning the first-level nodes from the local data, and loading child nodes of one of the first-level nodes:

var myHDS = new kendo.data.HierarchicalDataSource({
    transport: {
        read: function (options) {
            if  (!options.data.id) {
                options.success([
                    { ... hard-coded local data for the first level of nodes
                             ... must hard-code IDs?
                    }
                ]);
                return;
            }
                // must have an id here
            var node = myHDS.get(options.data.id);
            // load children from the sever via AJAX call

1 Answer, 1 is accepted

Sort by
0
Alex Gyoshev
Telerik team
answered on 07 Jan 2015, 08:49 AM

Hello Dan,

You can define the value field as the id field for the first level, and use a different one for sub-levels

schema: { model: {
    id: "value",
    children: { schema: { model: { id: "anotherField" } } }
} }

Regards,
Alex Gyoshev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
dandv
Top achievements
Rank 2
Answers by
Alex Gyoshev
Telerik team
Share this question
or