我要的效果是表格展示树形数据,就是带children的那种
时间: 2024-02-23 19:59:51 浏览: 12
非常抱歉,我的回答有误。如果需要展示树形数据的话,可以使用 Element UI 自带的 Tree 组件。代码如下:
```html
<template>
<div>
<el-tree :data="treeData" :props="treeProps" @node-click="handleNodeClick">
<el-table :data="tableData" style="width: 100%; margin-top: 10px;">
<el-table-column label="编码" prop="code"></el-table-column>
<el-table-column label="行政区域" prop="name"></el-table-column>
<el-table-column label="状态" prop="status"></el-table-column>
<el-table-column label="操作" width="100">
<template slot-scope="scope">
<el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="handleDisable(scope.row)">停用</el-button>
</template>
</el-table-column>
</el-table>
</el-tree>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [],
treeData: [],
treeProps: {
children: 'children',
label: 'name'
}
}
},
created() {
axios.get('/api/treeData').then(res => {
this.treeData = res.data
this.tableData = res.data[0].children
})
},
methods: {
handleNodeClick(data) {
this.tableData = data.children
},
handleEdit(row) {
console.log(row)
},
handleDisable(row) {
console.log(row)
}
}
}
</script>
```
在这个示例中,我们使用了 `<el-tree>` 组件来展示树形结构数据,然后在每个节点上添加了 `node-click` 事件,当节点被点击时,会触发 `handleNodeClick` 方法,从而切换表格数据。
同时,我们在 `<el-tree>` 组件中嵌套了 `<el-table>` 组件,这样就可以在每个节点下展示表格数据了。在 `created` 钩子函数中,我们获取了树形结构数据,并将第一个节点的子节点作为表格的初始数据。
希望这个回答能够满足您的需求,如果还有问题,请随时提出。