用vue写出一个点击表格某一行弹窗弹窗里也是表格选择某一行数据后确定返回到原表格数据。
<template>
<div>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in tableData" :key="index" @click="showModal(item)">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.gender }}</td>
</tr>
</tbody>
</table>
<pre><code><div v-if="showModalStatus" class="modal">
<div class="modal-content">
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in modalData" :key="index" @click="selectModalData(item)">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.gender }}</td>
</tr>
</tbody>
</table>
<button @click="closeModal">关闭</button>
<button @click="confirmModalData">确定</button>
</div>
</div>
</code></pre>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{
name: '小明',
age: 18,
gender: '男'
},
{
name: '小红',
age: 20,
gender: '女'
},
{
name: '小刚',
age: 22,
gender: '男'
}
],
showModalStatus: false,
modalData: [],
selectedModalData: {}
}
},
methods: {
showModal(item) {
this.modalData = this.tableData.filter(data => data.name !== item.name)
this.showModalStatus = true
},
closeModal() {
this.showModalStatus = false
},
selectModalData(item) {
this.selectedModalData = item
},
confirmModalData() {
if (Object.keys(this.selectedModalData).length === 0) {
alert('请选择一条数据')
} else {
this.tableData.push(this.selectedModalData)
this.closeModal()
}
}
}
}
</script>
<style>
.modal {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
.modal-content {
background-color: white;
padding: 20px;
border-radius: 5px;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid black;
text-align: center;
}
th {
background-color: #eee;
}
tr:hover {
background-color: #f5f5f5;
}
button {
margin: 10px;
}
</style>
原文地址: http://www.cveoy.top/t/topic/nLF 著作权归作者所有。请勿转载和采集!