I want to create a SAPUI TreeTable from a JSON request, currently my output looks like this (as you can see, every node contains an empty row -> I do not know where this is
just get rid of the __metadata, set flat[key].__metadata = ""
, i guess TreeTable renderer must take it for another child.
// flatten to object with string keys that can be easily referenced later
var flat = {};
for (var i = 0; i < data.d.results.length; i++) {
var key = 'id' + data.d.results[i].ID;
flat[key] = data.d.results[i];
flat[key].__metadata = "";
}
sap.ui.jsview("test.view", {
getControllerName: function() {
return "test.controller";
},
createContent: function(oController) {
var oTreeTable = new sap.ui.table.TreeTable({
columns: [
new sap.ui.table.Column({
label: "Title",
template: "Title"
}),
new sap.ui.table.Column({
label: "Mon01",
template: "Mon01"
}),
new sap.ui.table.Column({
label: "Mon02",
template: "Mon02"
}),
new sap.ui.table.Column({
label: "Mon03",
template: "Mon03"
}),
new sap.ui.table.Column({
label: "Mon04",
template: "Mon04"
}),
],
selectionMode: sap.ui.table.SelectionMode.None,
enableColumnReordering: false,
expandFirstLevel: false,
toggleOpenState: function(oEvent) {
}
});
var data = {
"d": {
"results": [{
"__metadata": {
"id": "http://url/EntitySet('00000001')",
"uri": "http://url/EntitySet('00000001')",
"type": " NAMESPACE_SRV.Entity"
},
"Mon04": "",
"Mon03": "",
"Mon02": "09/2014",
"Mon01": "08/2014",
"Title": "Parent 1",
"ID": "00000001",
"ParentId": "",
"ChildId": "",
}, {
"__metadata": {
"id": "http://url/EntitySet('00000002')",
"uri": "http://url/EntitySet('00000002')",
"type": "NAMESPACE_SRV.Entity"
},
"Mon04": "",
"Mon03": "",
"Mon02": "1560",
"Mon01": "1550",
"Title": "Parent 2",
"ID": "00000002",
"ParentId": "",
"ChildId": "",
}, {
"__metadata": {
"id": "http://url/EntitySet('00000003')",
"uri": "http://url/EntitySet('00000003')",
"type": "NAMESPACE_SRV.Entity"
},
"Mon04": "",
"Mon03": "",
"Mon02": "1860",
"Mon01": "1750",
"Title": "Child 1",
"ID": "00000003",
"ParentId": "00000002",
"ChildId": "00000001",
}, {
"__metadata": {
"id": "http://url/EntitySet('00000004')",
"uri": "http://url/EntitySet('00000004')",
"type": "NAMESPACE_SRV.Entity"
},
"Mon04": "",
"Mon03": "",
"Mon02": "1860",
"Mon01": "1750",
"Title": "Child 1_1",
"ID": "00000004",
"ParentId": "00000003",
"ChildId": "00000001",
}, {
"__metadata": {
"id": "http://url/EntitySet('00000005')",
"uri": "http://url/EntitySet('00000005')",
"type": "NAMESPACE_SRV.Entity"
},
"Mon04": "",
"Mon03": "",
"Mon02": "2060",
"Mon01": "1950",
"Title": "Child 2",
"ID": "00000005",
"ParentId": "00000002",
"ChildId": "00000001",
}, {
"__metadata": {
"id": "http://url/EntitySet('00000006')",
"uri": "http://url/EntitySet('00000006')",
"type": "NAMESPACE_SRV.Entity"
},
"Mon04": "",
"Mon03": "",
"Mon02": "2060",
"Mon01": "1950",
"Title": "Child 3",
"ID": "00000006",
"ParentId": "00000002",
"ChildId": "00000001",
}]
}
};
var flat = {};
for (var i = 0; i < data.d.results.length; i++) {
var key = 'id' + data.d.results[i].ID;
flat[key] = data.d.results[i];
flat[key].__metadata = "";
}
// add 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]);
}
}
// here it is!
// console.log(root);
// to access the JSON via "/root" in bindRows(), could this be a problem??
var data = {
root: root,
};
console.log(data);
var oTreeModel = new sap.ui.model.json.JSONModel();
oTreeModel.setData(data);
oTreeTable.setModel(oTreeModel);
oTreeTable.bindRows({
path: '/root',
});
return oTreeTable;
},
});
sap.ui.controller("test.controller", {
onInit: function() {
}
});
sap.ui.view({
type: sap.ui.core.mvc.ViewType.JS,
viewName: "test.view"
})
.placeAt("uiArea");
<script id='sap-ui-bootstrap' type='text/javascript' src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js' data-sap-ui-libs="sap.m,sap.ui.commons,sap.ui.table,sap.viz" data-sap-ui-theme="sap_bluecrystal"></script>
<body class="sapUiBody">
<div id="uiArea"></div>
</body>