<template>
	<el-table ref="myTable" :data="bikeList" height="260px" @selection-change="handleSelectionChange" :row-key="getRowKeys" :reserve-selection="true">
		<el-table-column type="selection" width="55" align="center" />
		<el-table-column label="ID" align="center" prop="id" />
	</el-table>
</template>
<script setup name="bikeList" lang="ts">

import { listBike} from '@/api/bike';
import { BikeVO } from '@/api/bike/types';
const myTable= ref(ElTable);
const bikeList = ref<BikeVO[]>([]);
const ids = ref<Array<string | number>>([]);

/** 多选框选中数据 */
const handleSelectionChange = (selection: BikeVO[]) => {
  ids.value = selection.map((item) => item.id);
};

const getRowKeys=(row:any)=> {
  return row.id
}

const initSelectionRow = async () => {
  nextTick(() => {
    myTable.value.clearSelection();
    bikeList.value.forEach((row) => {
      if(ids.value.includes(row.id)){
          //this.$refs.myTable.toggleRowSelection(row, true); //Vue2写法
          myTable.value.toggleRowSelection(row, true);  //Vue3写法
      }
    });
  });
}

const getList = async () => {
  const res = await listBike({});
  bikeList.value = res.rows;
  initSelectionRow();
}
onMounted(() => {
  getList()
})
</script>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐