<template><el-table ref="multipleTableRef" :data="tableData" style="width: 100%"><el-table-column type="selection" width="55" /><el-table-column label="时间" width="120"><template #default="scope">{{ scope.row.date }}</template></el-table-column><el-table-column property="name" label="姓名" width="120" /><el-table-column property="address" label="地址" show-overflow-tooltip /></el-table>
</template><script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ElTable } from 'element-plus'const multipleTableRef = ref<any>()
const tableData = [{id: '1',date: '2016-05-03',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},{id: '2',date: '2016-05-02',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},{id: '3',date: '2016-05-04',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},{id: '4',date: '2016-05-01',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},]//部分显示,默认回显 id 为 1 的勾选框
const toggleSelection = (rows: any) => {console.log( multipleTableRef.value,'值必须不为空');console.log(rows, 'table全部数据');const ids = ['1', '2', '4']if (rows) {rows.forEach((row: any) => {if(row.id === '1'){ //单个情况回显// if (ids.includes(row.id)) { // 判断当前行是否为默认回显行 数组多个数据回显情况multipleTableRef.value!.toggleRowSelection(row, true) // 默认勾选} else {multipleTableRef.value!.toggleRowSelection(row, false) // 不选中}})} else {multipleTableRef.value!.clearSelection()}
}
onMounted(() => {// getTableData()toggleSelection(tableData)
})
</script>