用vue写一个table表格展开行内嵌一个具有完整勾选功能的表格
时间: 2023-04-02 20:01:39 浏览: 69
可以使用vue-table-with-tree-grid插件来实现这个功能。首先在项目中安装该插件,然后在组件中引入该插件并进行配置。在表格中添加一个展开行的按钮,并在展开行中嵌入一个子表格,子表格中添加勾选框即可实现完整勾选功能。具体实现细节可以参考该插件的文档和示例代码。
相关问题
用vue写一个table表格展开行内嵌一个具有完整勾选功能表格的具体代码
以下是用 Vue 写一个具有展开行和内嵌勾选功能的表格的代码示例:
<template>
<div>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in tableData" :key="index">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.gender }}</td>
<td>
<button @click="toggleSubTable(index)">
{{ showSubTableIndex === index ? '收起' : '展开' }}
</button>
</td>
</tr>
<tr v-if="showSubTableIndex !== null">
<td colspan="4">
<table>
<thead>
<tr>
<th>选择</th>
<th>课程</th>
<th>分数</th>
</tr>
</thead>
<tbody>
<tr v-for="(subItem, subIndex) in tableData[showSubTableIndex].subTable" :key="subIndex">
<td>
<input type="checkbox" v-model="subItem.checked">
</td>
<td>{{ subItem.course }}</td>
<td>{{ subItem.score }}</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{
name: '张三',
age: 20,
gender: '男',
subTable: [
{ course: '语文', score: 80, checked: false },
{ course: '数学', score: 90, checked: false },
{ course: '英语', score: 85, checked: false }
]
},
{
name: '李四',
age: 22,
gender: '女',
subTable: [
{ course: '语文', score: 75, checked: false },
{ course: '数学', score: 95, checked: false },
{ course: '英语', score: 90, checked: false }
]
},
{
name: '王五',
age: 18,
gender: '男',
subTable: [
{ course: '语文', score: 70, checked: false },
{ course: '数学', score: 80, checked: false },
{ course: '英语', score: 75, checked: false }
]
}
],
showSubTableIndex: null
}
},
methods: {
toggleSubTable(index) {
if (this.showSubTableIndex === index) {
this.showSubTableIndex = null
} else {
this.showSubTableIndex = index
}
}
}
}
</script>
vue2 封装一个el-table 可以实现展开行嵌套表格
可以使用`el-table`的`expand-row-keys`属性来实现展开行,并在展开行中再嵌套一个`el-table`来实现嵌套表格的效果。具体实现步骤如下:
1. 在父组件中引入`el-table`和需要展示的子组件。
```
<template>
<el-table :data="tableData" :expand-row-keys="expandRowKeys">
<!-- 父表格的列 -->
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column type="expand">
<!-- 展开行中嵌套的子表格 -->
<child-table></child-table>
</el-table-column>
</el-table>
</template>
<script>
import ChildTable from './ChildTable.vue'
export default {
components: { ChildTable },
data() {
return {
tableData: [{ name: '张三', age: 18 }, { name: '李四', age: 20 }],
expandRowKeys: []
}
}
}
</script>
```
2. 在子组件`ChildTable`中也使用`el-table`来展示数据。
```
<template>
<el-table :data="tableData">
<el-table-column prop="subject" label="科目"></el-table-column>
<el-table-column prop="score" label="分数"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [{ subject: '语文', score: 90 }, { subject: '数学', score: 85 }]
}
}
}
</script>
```
3. 在父组件中监听展开行的事件,并记录展开的行的`key`值,用于判断哪些行需要展开子表格。
```
<template>
<el-table :data="tableData" :expand-row-keys="expandRowKeys" @expand-change="handleExpandChange">
<!-- 父表格的列 -->
</el-table>
</template>
<script>
import ChildTable from './ChildTable.vue'
export default {
components: { ChildTable },
data() {
return {
tableData: [{ name: '张三', age: 18 }, { name: '李四', age: 20 }],
expandRowKeys: []
}
},
methods: {
handleExpandChange(row, expandedRows) {
// 获取展开行的key值
const keys = expandedRows.map(row => row.key)
// 更新展开行的key值
this.expandRowKeys = keys
}
}
}
</script>
```
这样就可以实现在`el-table`中展开行,并在展开行中嵌套另一个`el-table`来展示子数据的效果了。