vue3 element plus 表格单选,默认选中行、选中、清空

<template>
  <el-table :data="tableData" ref="singleTableRef" style="width: 100%" highlight-current-row
    @current-change="handleCurrentChange">
    <el-table-column prop="date" label="Date" width="180" />
    <el-table-column prop="name" label="Name" width="180" />
    <el-table-column prop="address" label="Address" />
  </el-table>
  <div style="margin-top: 20px">
    <el-button @click="setCurrent(2)">选中第三行</el-button>
    <el-button @click="setCurrent()">清空</el-button>
  </div>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue'
const singleTableRef = ref(null)
const tableData = reactive([
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
])
const setCurrent = (index) => {
  singleTableRef.value.setCurrentRow(tableData[index])
}
nextTick(() => {
  if (tableData.length > 1) {
    // 默认选中第二行
    singleTableRef.value?.setCurrentRow(tableData[1])
  }
});  
</script>

vue3 element plus 表格单选,默认选中行、选中、清空_第1张图片

你可能感兴趣的:(vue.js,javascript,前端,前端框架)