cancel
Showing results for 
Search instead for 
Did you mean: 

sap.ui.table.TreeTable - Not displaying hierarchy when loaded from flat json structure

former_member193947
Participant

Hello,

I am trying to implement a tree structure with the help of sap.ui.table.TreeTable as shown in Sample. But instead of displaying hierarchy as parent/child nodes, the tree table is displaying every row as parent entry.

We have a flat structure data from a table in json format and without converting them to hierarchical format json, is it possible to display them as parent/child nodes ? Have anybody implemented it ? If yes, can you please share the logic/code information ?

Thanks.

SergioG_TX
Active Contributor

like Jun Wu and Chandeep said - this is not possible with a flat structure, instead, you need to format your json structure as a hierarchy.

every level (parent) needs to contain a child in order to reflect the levels in the tree, for example:

aTree = [ { propA: 'parent1', children: [ { propA: 'child of parent 1', children:[] } ] }, 
{ propA: 'parent2 no children', children: [] } ] then you can assign this to the model and bind it to the tree table

Accepted Solutions (0)

Answers (7)

Answers (7)

0 Kudos

May be this can help you

https://embed.plnkr.co/plunk/moTGOT

0 Kudos

Hello,

You can try like below code.

Because I was having same flat structure to form a treetable.

in your Success method write like

success: function (oData) {

var data = oData.QuestionTree.results;

var flat = {};

for (var i = 0; i < data.length; i++) {

var key = 'id' + data[i].Id;

flat[key] = data[i];

flat[key].__metadata = "";

}

// child container array to each node

for (var i in flat) {

flat[i].children = []; // add children container

}

// populate the child container arrays

for (var i in flat) {

var parentkey = 'id' + flat[i].ParentId;

if (flat[parentkey]) {

flat[parentkey].children.push(flat[i]);

}

}

// find the root nodes (no parent found) and create the hierarchy tree from them

var root = [];

for (var i in flat) {

var parentkey = 'id' + flat[i].ParentId;

if (!flat[parentkey]) {

root.push(flat[i]);

}

}

var oJsonModel = new sap.ui.model.json.JSONModel();

oJsonModel.setData(root);

treeTable.setModel(oJsonModel);

treeTable.bindRows({

path: "/",

parameters: {

arrayNames: ['children'],

numberOfExpandedLevels: length

}

});

}

Thanks,

Nigel_James
Active Contributor
0 Kudos

It is the

"HierarchyLevel": 1,
and the
"ParentNodeID": 2,

fields that you need to take care of to ensure that they are set correctly.

HTH,

Nigel

former_member203031
Contributor
0 Kudos

Hi,

If you want to display the data in tree format then the you are fetching alsoshould be in the same way.

Please find the example in the below link

https://embed.plnkr.co/pn3BRS4EvjWTMtBfDJia/

Let me know if you have any doubt on this.

Thanks,

Deepak Raj.

former_member193947
Participant
0 Kudos

Thanks for your inputs.

0 Kudos

you can't do like this!!!

junwu
Active Contributor
0 Kudos

no way