- {
- "catalog": {
- "clothing": {
- "categories": [
- {"name": "Women", "categories": [
- {"name":"Clothing", "categories": [
- {"name": "Dresses", "categories": [
- {"name": "Casual Red Dress", "amount": 16.99, "currency": "EUR", "size": "S"},
- {"name": "Short Black Dress", "amount": 47.99, "currency": "EUR", "size": "M"},
- {"name": "Long Blue Dinner Dress", "amount": 103.99, "currency": "USD", "size": "L"}
- ]},
- {"name": "Tops", "categories": [
- {"name": "Printed Shirt", "amount": 24.99, "currency": "USD", "size": "M"},
- {"name": "Tank Top", "amount": 14.99, "currency": "USD", "size": "S"}
- ]},
transformTreeData: function(nodesIn) {
var nodes = []; //'deep' object structure
var nodeMap = {}; //'map', each node is an attribute
if (nodesIn) {
var nodeOut;
var parentId;
for (var i = 0; i < nodesIn.length; i++) {
var nodeIn = nodesIn[i]
nodeOut = { id: nodeIn.ID,
text: nodeIn.Text,
type: nodeIn.Type,
children: [] };
parentId = nodeIn.ParentID;
if (parentId && parentId.length > 0) {
//we have a parent, add the node there
//NB because object references are used, changing the node
//in the nodeMap changes it in the nodes array too
//(we rely on parents always appearing before their children)
var parent = nodeMap[nodeIn.ParentID];
if (parent) {
parent.children.push(nodeOut);
}
} else {
//there is no parent, must be top level
nodes.push(nodeOut);
}
//add the node to the node map, which is a simple 1-level list of all nodes
nodeMap[nodeOut.id] = nodeOut;
}
}
return nodes;
}
var flatData = this.readFile();
var deepData = this.transformTreeData(flatData);
this.setModelData(deepData);
sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/Device",
"com/scn/demo/treetable/model/models"
], function(UIComponent, Device, models) {
"use strict";
return UIComponent.extend("com.scn.demo.treetable.Component", {
metadata: {
manifest: "json"
},
/**
* The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
* @public
* @override
*/
init: function() {
// call the base component's init function
UIComponent.prototype.init.apply(this, arguments);
// set the device model
this.setModel(models.createDeviceModel(), "device");
var flatData = this.readFile();
var deepData = this.transformTreeData(flatData);
this.setModelData(deepData);
},
readFile: function() {
var flatData = null;
//load the data from the JSON file
//NB same format as gateway service could be
var inModel = new sap.ui.model.json.JSONModel();
inModel.loadData("/webapp/model/FlatData.json", "", false);
var data = inModel.getData();
if (data) {
flatData = data.nodes;
}
return flatData;
},
transformTreeData: function(nodesIn) {
var nodes = []; //'deep' object structure
var nodeMap = {}; //'map', each node is an attribute
if (nodesIn) {
var nodeOut;
var parentId;
for (var i = 0; i < nodesIn.length; i++) {
var nodeIn = nodesIn[i];
nodeOut = { id: nodeIn.ID,
text: nodeIn.Text,
type: nodeIn.Type,
children: [] };
parentId = nodeIn.ParentID;
if (parentId && parentId.length > 0) {
//we have a parent, add the node there
//NB because object references are used, changing the node
//in the nodeMap changes it in the nodes array too
//(we rely on parents always appearing before their children)
var parent = nodeMap[nodeIn.ParentID];
if (parent) {
parent.children.push(nodeOut);
}
} else {
//there is no parent, must be top level
nodes.push(nodeOut);
}
//add the node to the node map, which is a simple 1-level list of all nodes
nodeMap[nodeOut.id] = nodeOut;
}
}
return nodes;
},
setModelData: function (nodes) {
//store the nodes in the JSON model, so the view can access them
var nodesModel = new sap.ui.model.json.JSONModel();
nodesModel.setData({nodeRoot: { children: nodes }});
this.setModel(nodesModel, "nodeModel");
}
});
});
<mvc:View xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="sap.m" xmlns:table="sap.ui.table" xmlns:mvc="sap.ui.core.mvc"
controllerName="com.scn.demo.treetable.controller.View1">
<App>
<pages>
<Page title="{i18n>title}" class="sapUiSizeCompact">
<content>
<table:TreeTable id="TreeTable"
rows="{path:'nodeModel>/nodeRoot', parameters: {arrayNames:['children']}}"
enableSelectAll="false"
expandFirstLevel="true">
<table:columns>
<table:Column width="13rem">
<Label text="{i18n>treetitle}"/>
<table:template>
<Text text="{nodeModel>text}" />
</table:template>
</table:Column>
</table:columns>
</table:TreeTable>
</content>
</Page>
</pages>
</App>
</mvc:View>
{ "nodes": [
{ "ID": "O100", "Text": "Software Development", "ParentID": "", "Type": "O" },
{ "ID": "O110", "Text": "Team A", "ParentID": "O100", "Type": "O" },
{ "ID": "S111", "Text": "Product Owner", "ParentID": "O110", "Type": "S" },
{ "ID": "S112", "Text": "Scrum Master", "ParentID": "O110", "Type": "S" },
{ "ID": "S113", "Text": "Team Member", "ParentID": "O110", "Type": "S" },
{ "ID": "S114", "Text": "Team Member", "ParentID": "O110", "Type": "S" },
{ "ID": "S115", "Text": "Team Member", "ParentID": "O110", "Type": "S" },
{ "ID": "O120", "Text": "Team B", "ParentID": "O100", "Type": "O" },
{ "ID": "S121", "Text": "Product Owner", "ParentID": "O120", "Type": "S" },
{ "ID": "S122", "Text": "Scrum Master", "ParentID": "O120", "Type": "S" },
{ "ID": "S123", "Text": "Team Member", "ParentID": "O120", "Type": "S" },
{ "ID": "S124", "Text": "Team Member", "ParentID": "O120", "Type": "S" },
{ "ID": "S125", "Text": "Team Member", "ParentID": "O120", "Type": "S" },
{ "ID": "S126", "Text": "Team Member", "ParentID": "O120", "Type": "S" },
{ "ID": "S127", "Text": "Team Member", "ParentID": "O120", "Type": "S" },
{ "ID": "O130", "Text": "Support", "ParentID": "O100", "Type": "O" },
{ "ID": "S131", "Text": "Team Lead", "ParentID": "O130", "Type": "S" },
{ "ID": "S132", "Text": "Support Analyst", "ParentID": "O130", "Type": "S" },
{ "ID": "S133", "Text": "Support Analyst", "ParentID": "O130", "Type": "S" },
{ "ID": "S134", "Text": "Support Analyst", "ParentID": "O130", "Type": "S" },
{ "ID": "S135", "Text": "Support Analyst", "ParentID": "O130", "Type": "S" }
]
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |