文章目錄
- 1、小記
- 2、頁面
- 3、typescript事件
- 4、測試數據
- 5、樣式
1、小記
element-plus中el-table 的 expand,箭頭控制子項顯示,有點丑。
想實現類似bootstrap ,用按鈕 展開/隱藏子項的功能
2、頁面
<!-- 表內容 --><el-table:data="tableData":border="true":preserve-expanded-content="true"style="width: 100%":expand-row-keys="expands":row-key="getRowKeys"><!-- 子項 --><el-table-column type="expand" width="0"><!-- width隱藏列 --><template #default="props"><div m="4" class="expandcontent"><el-table :data="props.row.family" :border="true" :color="'red'"><el-table-column label="Name" prop="name" /><el-table-column label="State" prop="state" /><el-table-column label="City" prop="city" /><el-table-column label="Address" prop="address" /><el-table-column label="Zip" prop="zip" /></el-table></div></template></el-table-column><!-- 其他數據列 --><el-table-column label="Date" prop="date" /><el-table-column label="Name" prop="name" /><!-- 操作 --><el-table-column label="Operations"><template #default="props"><el-button type="primary" @click="toggleExpand(props.row)">{{ isExpanded(props.row) ? '收起' : '展開' }}</el-button></template></el-table-column></el-table>
3、typescript事件
<script setup lang="tsx">
const getRowKeys = (row) => {return row.id
}
//展開自定義
const expands = ref<string[]>([])
const toggleExpand = (row) => {const key = getRowKeys(row)const index = expands.value.indexOf(key)if (index > -1) {expands.value.splice(index, 1) // 收起} else {expands.value.push(key) // 展開}
}
const isExpanded = (row) => {return expands.value.includes(getRowKeys(row))
}
</script>
4、測試數據
const tableData = [{id: 1,date: '2016-05-03',name: 'Tom',state: 'California',city: 'San Francisco',address: '3650 21st St, San Francisco',zip: 'CA 94114',expand: false,family: [{name: 'Jerry',state: 'California',city: 'San Francisco',address: '3650 21st',zip: 'CA 94114'},{name: 'Spike',state: 'California',city: 'San Francisco',address: '3650 21st ',zip: 'CA 94114'},{name: 'Tyke',state: 'California',city: 'San Francisco',address: '3650 21st ',zip: 'CA 94114'}]},{id: 2,date: '2016-05-02',name: 'Tom',state: 'California',city: 'San Francisco',address: '3650 21st St, San Francisco',zip: 'CA 94114',expand: false,family: [{name: 'Jerry',state: 'California',city: 'San Francisco',address: '3650 21st St, San Francisco',zip: 'CA 94114'}]}
]
5、樣式
<!-- 樣式 -->
<style scoped lang="scss">
// 子項背景色
:deep(.el-table__expanded-cell) {background-color: #cbdde2 !important;
}
// 子項居中
.expandcontent {width: 95%;margin: 0 auto;
}
</style>