vue中table多選/單選行,獲取其數(shù)據(jù)方式
vue table多選/單選行,獲取其數(shù)據(jù)
多選
使用官方文檔提供的方式:手動(dòng)添加一個(gè)el-table-column,設(shè)type屬性為selection即可。
<template>
? <el-table
? ? ref="multipleTable"
? ? :data="tableData"
? ? tooltip-effect="dark"
? ? style="width: 100%"
? ? @selection-change="handleSelectionChange">
? ? <el-table-column
? ? ? type="selection"
? ? ? width="55">
? ? </el-table-column>
? ? <el-table-column
? ? ? label="日期"
? ? ? width="120">
? ? ? <template slot-scope="scope">{{ scope.row.date }}</template>
? ? </el-table-column>
? ? <el-table-column
? ? ? prop="name"
? ? ? label="姓名"
? ? ? width="120">
? ? </el-table-column>
? ? <el-table-column
? ? ? prop="address"
? ? ? label="地址"
? ? ? show-overflow-tooltip>
? ? </el-table-column>
? </el-table>
? <div style="margin-top: 20px">
? ? <el-button @click="toggleSelection([tableData[1], tableData[2]])">切換第二、第三行的選中狀態(tài)</el-button>
? ? <el-button @click="toggleSelection()">取消選擇</el-button>
? </div>
</template><script>
? export default {
? ? data() {
? ? ? return {
? ? ? ? tableData: [{
? ? ? ? ? date: '2016-05-03',
? ? ? ? ? name: '王小虎',
? ? ? ? ? address: '上海市普陀區(qū)金沙江路 1518 弄'
? ? ? ? }, {
? ? ? ? ? date: '2016-05-02',
? ? ? ? ? name: '王小虎',
? ? ? ? ? address: '上海市普陀區(qū)金沙江路 1518 弄'
? ? ? ? }],
? ? ? ? multipleSelection: []
? ? ? }
? ? },
? ? methods: {
? ? ? toggleSelection(rows) {
? ? ? ? if (rows) {
? ? ? ? ? rows.forEach(row => {
? ? ? ? ? ? this.$refs.multipleTable.toggleRowSelection(row);
? ? ? ? ? });
? ? ? ? } else {
? ? ? ? ? this.$refs.multipleTable.clearSelection();
? ? ? ? }
? ? ? },
? ? ? handleSelectionChange(val) {
? ? ? ? this.multipleSelection = val;
? ? ? }
? ? }
? }
</script>單選
在多選的基礎(chǔ)上修改,樣式不變,將handleSelectionChange方法修改為:
handleSelectionChange(val) {
? ? ? this.multipleSelection = val
? ? ? if (val.length > 1) {
? ? ? ? this.$refs.multipleTableFrag.clearSelection()
? ? ? ? this.$refs.multipleTableFrag.toggleRowSelection(val.pop())
? ? ? }
? ? ? console.log(val[0]['name'])
? ? },用數(shù)組存儲(chǔ)所選行,每次數(shù)組長度大于1時(shí),會(huì)pop之前的行,保證數(shù)組中最多存在一個(gè)行的數(shù)據(jù)。獲取該行某個(gè)屬性的數(shù)據(jù),用val[0]['屬性名']。
注意:
this.$refs.multipleTableFrag.clearSelection()為通過table的屬性ref值,找到相應(yīng)的table,執(zhí)行后面的clearSelection()方法。
若多個(gè)表,記得ref要寫的不一樣。
el-table 多選 單選功能
常見的el-table多選刪除功能
<el-table :data="tableData" @selection-change="handleSelectionChange">
js: 將選擇的id保存在一個(gè)數(shù)組里面,可以使用forEach或者map(map可以直接返回一個(gè)數(shù)組,較為方便)
?// 表格復(fù)選框 數(shù)據(jù)
? ? ? ? handleSelectionChange(val) {
? ? ? ? ? ? this.changArr = []
? ? ? ? ? ? val.forEach((item) => {
? ? ? ? ? ? ? ? this.changArr.push(item.idBq)
? ? ? ? ? ? })
? ? ? ? },然后就是刪除操作:
?// 批量刪除
? ? ? ? deleteDatafile() {
? ? ? ? ? ? if (this.changArr.length > 0) {
? ? ? ? ? ? ? ? this.$confirm('此操作將永久批量刪除該文件, 是否繼續(xù)?', '提示', {
? ? ? ? ? ? ? ? ? ? confirmButtonText: '確定',
? ? ? ? ? ? ? ? ? ? cancelButtonText: '取消',
? ? ? ? ? ? ? ? ? ? type: 'warning',
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? .then(() => {
? ? ? ? ? ? ? ? ? ? ? ? removeTag({
? ? ? ? ? ? ? ? ? ? ? ? ? ? idbqs: this.changArr,
? ? ? ? ? ? ? ? ? ? ? ? }).then((res) => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (res && res.data.data) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.$message.success('刪除成功')
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this.queryData()
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? .catch(() => {
? ? ? ? ? ? ? ? ? ? ? ? this.$message({
? ? ? ? ? ? ? ? ? ? ? ? ? ? type: 'info',
? ? ? ? ? ? ? ? ? ? ? ? ? ? message: '已取消刪除',
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? },單選刪除
直接使用 @select="select",在這個(gè)方法里面將數(shù)據(jù)前面的復(fù)選框回顯上
?<el-table :data="tableData" @select="select">
js部分:
// 單選
? ? ? ? select(selection, row) {
? ? ? ? ? ? if (selection.length > 1) {
? ? ? ? ? ? ? ? let del_row = selection.shift()
? ? ? ? ? ? ? ? this.$refs.table.toggleRowSelection(del_row, false)
? ? ? ? ? ? }
? ? ? ? ? ? this.xmdmArr = []
? ? ? ? ? ? this.chooseList = []
? ? ? ? ? ? this.xmdmArr.push(row.xmdm)
? ? ? ? ? ? this.chooseList = row.bqszdx.map((item) => {
? ? ? ? ? ? ? ? return item.idBq
? ? ? ? ? ? })
? ? ? ? },總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue自定義組件實(shí)現(xiàn)v-model雙向綁定數(shù)據(jù)的實(shí)例代碼
vue中父子組件通信,都是單項(xiàng)的,直接在子組件中修改prop傳的值vue也會(huì)給出一個(gè)警告,接下來就用一個(gè)小列子一步一步實(shí)現(xiàn)了vue自定義的組件實(shí)現(xiàn)v-model雙向綁定,需要的朋友可以參考下2021-10-10
基于vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖
一般做移動(dòng)端輪播圖的時(shí)候,最常用的就是Swiper插件了,而vue.js也有一個(gè)輪播組件vue-awesome-swiper,用法跟swiper相似。接下來通過本文給大家詳解講解vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖實(shí)例代碼,需要的朋友參考下2017-03-03
Vue項(xiàng)目打包問題詳解(生產(chǎn)環(huán)境樣式失效)
在Vue開發(fā)過程中,項(xiàng)目的打包是一個(gè)非常重要的步驟,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包問題(生產(chǎn)環(huán)境樣式失效)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Vue實(shí)現(xiàn)大屏獲取當(dāng)前所處城市及當(dāng)?shù)靥鞖獾木唧w方案
文章介紹了如何在大屏項(xiàng)目中展示城市名稱和實(shí)時(shí)天氣信息,通過瀏覽器的GeolocationAPI獲取經(jīng)緯度,然后使用逆地理編碼服務(wù)獲取城市名稱,文章還提到了使用Open-Meteo獲取天氣信息,該服務(wù)免費(fèi)且支持全球經(jīng)緯度的實(shí)時(shí)天氣和未來7天預(yù)報(bào),需要的朋友可以參考下2026-02-02

