在进行删除某一行表格的数据,往往需要进行提示是否删除,选中确认之后才进行删除该项。效果如下:
导入element-plus,安装方式有如下几种:
# 选择一个你喜欢的包管理器
# NPM
$ npm install element-plus --save
# Yarn
$ yarn add element-plus
# pnpm
$ pnpm install element-plus
在main.js引入和使用:
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import '../src/assets/static/css/normalize.css'
import App from './App.vue'
import router from '../routers/router'
// 引入element-plus和样式
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(router)
.use(ElementPlus)
.use(createPinia())
.mount('#app')
我的实际运用代码:
<el-table
:data="tableData"
border
style="width: 100%"
max-height="400"
>
<el-table-column fixed prop="id" label="项目id" width="80">
</el-table-column>
<el-table-column prop="name" label="项目名称" width="120">
</el-table-column>
<el-table-column prop="manage" label="项目经理" width="100">
</el-table-column>
<el-table-column prop="supplier" label="供应商" width="120">
</el-table-column>
<el-table-column prop="type" label="项目类型" width="100">
</el-table-column>
<el-table-column prop="address" label="地理位置" width="150">
</el-table-column>
<el-table-column fixed="right" label="操作" width="80">
<template #default="scope">
<el-popconfirm
confirm-button-text="Yes"
cancel-button-text="No"
:icon="InfoFilled"
icon-color="#626AEF"
title="Are you sure to delete this?"
@confirm="confirmEvent(scope.$index)"
@cancel="cancelEvent"
>
<template #reference>
<el-button
size="small"
type="danger"
round
@click.prevent="deleteRow(scope.$index)"
>
删除
</el-button>
</template>
</el-popconfirm>
</template>
</el-table-column>
</el-table>
js代码:
<script setup>
import { useRouter, useRoute } from "vue-router";
import {
InfoFilled
} from "@element-plus/icons-vue";
import Commonhead from '../src/components/Commonhead.vue';
const options = ref([
{
value: "整改通知",
label: "整改通知",
},
{
value: "临检通知",
label: "临检通知",
},
]);
var tableData = ref([
{
id: "2211",
name: "名称示例",
manage: "郭河镇",
supplier: "供应商名称",
type: "公交总站",
address: "广州市越秀区环市路动物园总站",
},
{
id: "2209",
name: "名称示例",
manage: "刘明",
supplier: "供应商名称",
type: "公交总站",
address: "广州市天河区东站客运站",
},
]);
const deleteRow = (index) => {
console.log(index);
};
const confirmEvent = (index) => {
console.log("confirm!");
tableData.value.splice(index, 1);
};
const cancelEvent = () => {
console.log("cancel!");
};
</script>
所运用在该项目页面完整代码实例:
<template>
<div class="container">
<Commonhead :title="title"></Commonhead>
<div class="form">
<div class="form-item">
<div class="form-item-title">通知类型:</div>
<el-select
v-model="value"
clearable
placeholder="选择通知类型"
style="width: 400px"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</div>
<div class="form-item">
<div class="form-item-title">通知内容:</div>
<el-input
style="width: 400px"
v-model="textarea"
:autosize="{ minRows: 3, maxRows: 10 }"
type="textarea"
placeholder=""
/>
</div>
<div class="form-item">
<div class="form-item-title">关联项目:</div>
<div class="table">
<div class="add-btn" @click="gotoAddAssociatedItems">添加关联项目</div>
<el-table
:data="tableData"
border
style="width: 100%"
max-height="400"
>
<el-table-column fixed prop="id" label="项目id" width="80">
</el-table-column>
<el-table-column prop="name" label="项目名称" width="120">
</el-table-column>
<el-table-column prop="manage" label="项目经理" width="100">
</el-table-column>
<el-table-column prop="supplier" label="供应商" width="120">
</el-table-column>
<el-table-column prop="type" label="项目类型" width="100">
</el-table-column>
<el-table-column prop="address" label="地理位置" width="150">
</el-table-column>
<el-table-column fixed="right" label="操作" width="80">
<template #default="scope">
<el-popconfirm
confirm-button-text="Yes"
cancel-button-text="No"
:icon="InfoFilled"
icon-color="#626AEF"
title="Are you sure to delete this?"
@confirm="confirmEvent(scope.$index)"
@cancel="cancelEvent"
>
<template #reference>
<el-button
size="small"
type="danger"
round
@click.prevent="deleteRow(scope.$index)"
>
删除
</el-button>
</template>
</el-popconfirm>
</template>
</el-table-column>
</el-table>
</div>
</div>
<div class="send-btn">发送</div>
</div>
</div>
</template>
<script setup>
import { useRouter, useRoute } from "vue-router";
import {
InfoFilled
} from "@element-plus/icons-vue";
import Commonhead from '../src/components/Commonhead.vue';
const value = ref("");
const textarea = ref("");
const title = ref("整改通知 > 发送通知");
const options = ref([
{
value: "整改通知",
label: "整改通知",
},
{
value: "临检通知",
label: "临检通知",
},
]);
var tableData = ref([
{
id: "2211",
name: "名称示例",
manage: "郭河镇",
supplier: "供应商名称",
type: "公交总站",
address: "广州市越秀区环市路动物园总站",
},
{
id: "2209",
name: "名称示例",
manage: "刘明",
supplier: "供应商名称",
type: "公交总站",
address: "广州市天河区东站客运站",
},
]);
const deleteRow = (index) => {
console.log(index);
};
const confirmEvent = (index) => {
console.log("confirm!");
tableData.value.splice(index, 1);
};
const cancelEvent = () => {
console.log("cancel!");
};
const router = useRouter();
const route = useRoute();
const gotoAddAssociatedItems = () => {
router.push({
path: "/notice/sendNotifications/addAssociatedItems",
});
};
</script>
<style scoped>
.container {
padding: 20px;
}
.form {
width: 1000px;
margin: 0 auto;
}
.form-item {
display: flex;
margin: 20px;
}
.form-item-title {
margin-right: 15px;
font-size: 14px;
/* width: 100px; */
}
.form-item-title::before {
content: "*";
left: 0;
top: 0;
}
.add-btn {
width: 110px;
font-size: 14px;
height: 30px;
text-align: center;
color: #fff;
background-color: #f5b90f;
line-height: 30px;
border-radius: 5px;
margin-bottom: 15px;
cursor: pointer;
}
.send-btn {
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
background-color: #f5b90f;
color: #fff;
font-size: 14px;
margin: 50px auto;
border-radius: 5px;
cursor: pointer;
}
</style>