一、点击黄色背景文字,然后控制台(F12)
看效果,每个只可以添加一次。
二、全部html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<link
rel="stylesheet"
href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
/>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期"> </el-table-column>
<el-table-column label="配送信息">
<el-table-column prop="name" label="姓名">
<template slot-scope="scope">
<div
style="background-color: yellow"
@click="onRowClick('配送信息-姓名', 'name',scope.row.id)"
>
{{scope.row.name}}
</div>
</template>
</el-table-column>
<el-table-column prop="province" label="省份">
<template slot-scope="scope">
<div
style="background-color: yellow"
@click="onRowClick('配送信息-省份', 'province',scope.row.id)"
>
{{scope.row.province}}
</div>
</template>
</el-table-column>
</el-table-column>
<el-table-column label="地址">
<el-table-column prop="city" label="市区">
<template slot-scope="scope">
<div
style="background-color: yellow"
@click="onRowClick('地址-市区', 'city',scope.row.id)"
>
{{scope.row.city}}
</div>
</template></el-table-column
>
<el-table-column prop="address" label="地址"
><template slot-scope="scope">
<div
style="background-color: yellow"
@click="onRowClick('地址-地址', 'address',scope.row.id)"
>
{{scope.row.address}}
</div>
</template>
</el-table-column>
</el-table-column>
</el-table>
</div>
<script>
const vm = new Vue({
el: "#app",
data() {
return {
tableData: [
{
date: "2016-05-03",
name: "王小虎",
province: "上海",
city: "普陀区",
address: "上海市普陀区金沙江路 1518 弄",
id: 11,
},
{
date: "2016-05-02",
name: "王小虎",
province: "上海",
city: "普陀区",
address: "上海市普陀区金沙江路 1518 弄",
id: 22,
},
{
date: "2016-05-04",
name: "王小虎",
province: "上海",
city: "普陀区",
address: "上海市普陀区金沙江路 1518 弄",
id: 33,
},
{
date: "2016-05-01",
name: "王小虎",
province: "上海",
city: "普陀区",
address: "上海市普陀区金沙江路 1518 弄",
id: 44,
},
{
date: "2016-05-08",
name: "王小虎",
province: "上海",
city: "普陀区",
address: "上海市普陀区金沙江路 1518 弄",
id: 55,
},
],
list: [],
};
},
methods: {
// 数组对象去重
onRowClick(label, value, id) {
/*方法一:*/
for (let i in this.list) {
if (this.list[i].value === value && this.list[i].id === id) {
return false;
}
}
/*方法二:*/
// const some = this.list.some(
// (v) => v.value === value && v.id === id
// );
// if (some === true) return false;
/*方法三:*/
// const filter = this.list.filter(
// (v) => v.value === value && v.id === id
// );
// if (filter.length > 0) return false;
this.list.push({ label, value, id });
console.log(this.list);
},
},
});
</script>
</body>
</html>