Skip to content

Instantly share code, notes, and snippets.

@birdayz
Last active January 11, 2019 20:27
Show Gist options
  • Save birdayz/56e791513a2860af80594d46c0a6d195 to your computer and use it in GitHub Desktop.
Save birdayz/56e791513a2860af80594d46c0a6d195 to your computer and use it in GitHub Desktop.
Infinimesh recursive object transformation for vuetify tree
<div id="app">
<v-app id="inspire">
<v-treeview :items="items"></v-treeview>
</v-app>
</div>
function transformObject(input) {
var res = {};
res.id = input.uid;
res.name = input.name;
res.children = [];
if (input.devices) {
for (var device of input.devices) {
res.children.push(transformObject(device));
}
}
if (input.objects) {
for (var object of input.objects) {
res.children.push(transformObject(object));
}
}
return res;
}
function transform(input) {
var res = [];
for (var value of input.objects) {
var el = transformObject(value);
res.push(el)
}
for (var value of input.devices) {
var el = transformObject(value);
res.push(el)
}
return res;
}
var data = {
"objects": [{
"uid": "0x1119d",
"name": "Johannes' Home",
"objects": [{
"uid": "0x1119e",
"name": "First Floor",
"objects": [{
"uid": "0x1119f",
"name": "Living Room",
"devices": [{
"uid": "0x111a0",
"name": "PC"
}]
}]
},
{
"uid": "0x111a3",
"name": "Second Floor"
}
],
"devices": [{
"uid": "0x111a4",
"name": "le lamp"
}]
},
{
"uid": "0x111a5",
"name": "Enclosing Room",
"devices": [{
"uid": "0x111a6",
"name": "Enclosing-room-device"
}]
}
],
"devices": [{
"uid": "0x111a2",
"name": "some device"
}]
};
new Vue({
el: '#app',
data: () => ({
items: transform(data)
})
})
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.4.1/dist/vuetify.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/vuetify@1.4.1/dist/vuetify.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment