I have a treeview folder structure with nested children, including file objects. My goal is to display the details of those files based on the selected node using Vue and JavaScript. Below is the mocked data:
data:[{
id: 1,
name: ‘Project A’,
type: ‘folder’,
children: [{
id: 4,
name: 'Project A-1’,
type: ‘folder’,
files: [
{
id: 9,
pid: 4,
name: ‘file 3-A’,
type:’file’,
description: ‘wifi’,
country: ‘USA'
},
{
id: 10,
pid: 4,
name: ‘file 3-B’,
type:’file’,
description: ‘VPN’,
country: ‘USA'
}
]
}
]
},
{
id: 2,
name: 'Services’,
type: 'folder',
children:[],
files: [
{
id: 5,
name: ‘Services-1-A’,
type:’file’,
pid: 2,
description: ‘VPN’,
country: ‘AUS'
},
{
id: 6,
name: ‘Services-1-B’,
type:’file’,
pid: 2,
description: ‘WIFI’,
country: ‘AUS'
}
]
},
{
id: 3,
name: 'Servers',
type: 'folder’,
children:[],
files: [
{
id: 7,
name: ‘Servers-1-A’,
type: ‘file’,
pid: 3,
description: ‘VPN’,
country: ‘CAD'
},
{
id: 8,
name: ‘Servers-1-B',
type: ‘file’,
pid: 3,
description: ‘WIFI’,
country: ‘CAD'
}
]
}]
UI CODE:
<el-row style="background: #f2f2f2">
<el-col :span="6">
<div class="folder-content">
<el-tree
node-key="id"
:data="data"
accordion
@node-click="nodeclicked"
ref="tree"
style="background: #f2f2f2"
highlight-current
>
<span class="custom-tree-node" slot-scope="{ node, data }">
<span class="icon-folder" v-if="data.type === 'folder'">
<i class="el-icon-folder" aria-hidden="true"></i>
<span class="icon-folder_text" @click="showFiles(node.id) >{{ data.name }}</span>
</span>
</span>
</el-tree>
</div>
</el-col>
<el-col :span="12"><div class="entry-content">
<ul>
<li aria-expanded="false" v-for="(file,index) in files" :key="index">
<span class="folder__list"><input type="checkbox" :id= "file" :value="file">
<i class="el-icon-document" aria-hidden="true"></i>
<span class="folder__name">{{file.name}}</span></span>
</li>
</ul>
</div></el-col>
<el-col :span="6"><div class="preview_content"></div></el-col>
</el-row>
method:
showFiles(id) {
let f = this.data.filter(dataObject => {
if (dataObject.children && dataObject.children.id === id) {
return false
} else if (!dataObject.children && dataObject.id === id) {
return false
}
return true
})[1]
this.files = f.files
console.log(this.files)
}
My challenge lies in displaying nested files within children when clicking on a specific folder. I attempted filtering the data but could only display root folder files. How can I accurately iterate through and display nested files inside children?
Instead of retrieving file details, I am getting whole node details with prototype.
Any suggestions on how to iterate and display the desired file details?