vue实现增、删、改

该文描述了如何在Vue项目中使用ElementUI库实现表格数据的增、删、改功能,不依赖后端接口。通过表格展示数据,自定义操作列进行编辑和删除,使用对话框处理新增和修改操作,同时包含表单验证和数据唯一性检查。
摘要由CSDN通过智能技术生成

vue实现增、删、改

业务需求,前端实现增删改,不走接口
在这里插入图片描述
在这里插入图片描述

主页

    <el-table :data="collectionPointList"
                        :header-cell-style="{ fontSize: '14px', height: '48px', padding: '0px', background: 'white', }"
                        :row-style="{ height: 56 + 'px' }" height="40%" ref="multipleTable" :row-class-name="rowClassName">
                        <template slot="empty">
                            <h-empty v-if="isEmpty(collectionPointList)" type="table" />
                        </template>
                        <el-table-column label="编码" align="left" prop="code" :show-overflow-tooltip="true" />
                        <el-table-column label="名称" align="left" prop="name" :show-overflow-tooltip="true" />
                        <el-table-column label="采集点标识" align="left" prop="point" :show-overflow-tooltip="true" />
                        <el-table-column label="业务标识位" align="left" prop="businessPoint">
                            <template slot-scope="scope">
                                <span>{{ scope.row.businessPoint == true ? "是" : "否" }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" fixed="right" width="100px"
                            class-name="small-padding fixed-width">
                            <template slot-scope="scope">
                                <h-button type="text" icon="hv-edit" @click="handelUpdate(scope.row)">修改
                                </h-button>
                                <h-button type="text" class="delete_button" style="color: #f56c6c"
                                    @click="handleDelete(scope.$index)">删除
                                </h-button>
                            </template>
                        </el-table-column>
                    </el-table>
   <h-dialog :title="title" :visible.sync="open" append-to-body>
                    <el-form ref="form1" :model="form1" :rules="rules" label-width="110px">
                        <el-form-item label="编码:" prop="code">
                            <el-input v-model="form1.code" placeholder="请输入编码" />
                        </el-form-item>
                        <el-form-item label="名称:" prop="name">
                            <el-input v-model="form1.name" placeholder="请输入名称" />
                        </el-form-item>
                        <el-form-item label="采集点标识:" prop="point">
                            <el-input v-model="form1.point" placeholder="请输入采集点标识" />
                        </el-form-item>
                        <el-form-item label="业务标识位:" prop="businessPoint">
                            <el-switch v-model="form1.businessPoint" active-color="#13ce66">
                            </el-switch>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <h-button type="primary" @click="submitForm">确 定</h-button>
                        <h-button @click="cancel">取 消</h-button>
                    </div>
                </h-dialog>

JS部分

新增、修改
 handleAdd() {
            this.form1 = {
                businessPoint: true
            }
            this.title = "新增采集点"
            this.open = true
        },
        handelUpdate(row) {
            this.title = "修改采集点"
            this.open = true
            this.form1 = { ...row }
            this.id = row.id
        },
        //获取当前的行数
        rowClassName({ row, rowIndex }) {
            //把每一行的索引放进row
            row.id = rowIndex + 1;
        },


 submitForm() {
            this.$refs["form1"].validate((valid) => {
                if (valid) {
                    if (this.title === "新增采集点") {
                        let arrayCode
                        if (this.collectionPointList.length == 0) {
                            this.collectionPointList.push(this.form1)
                            this.open = false
                        } else {
                            //获取集合的编码
                            let listCodes = this.collectionPointList.map(item => item.code)
                            //判断表单中编码是否在集合中存在。此处判断,存在为true,不存在为false
                            arrayCode = listCodes.includes(this.form1.code)
                            //若不存在即添加
                            if (!arrayCode) {
                                this.collectionPointList.push(this.form1)
                                this.open = false
                            } else {
                                this.$modal.msgError("编码不可以重复")
                            }
                        }
                    } else {
                        let arrayCode
                        //获取集合的编码
                        let listCodes = this.collectionPointList.map(item => item.code)
                        //判断表单中编码是否在集合中存在。此处判断,存在为true,不存在为false
                        arrayCode = listCodes.includes(this.form1.code)
                        //若不存在即添加
                        if (!arrayCode) {
                            this.collectionPointList.forEach(element => {
                                if (element.id == this.id) {
                                    element.code = this.form1.code
                                    element.name = this.form1.name
                                    element.point = this.form1.point
                                    element.businessPoint = this.form1.businessPoint
                                }
                            })
                            this.open = false
                        } else {
                            this.$modal.msgError("编码不可以重复")
                        }

                    }
                }
            })

        },

删除

//采集点删除
        handleDelete(index) {
            this.$modal.confirm('是否确认删除选中的数据项?').then(function () {
            }).then(() => {
                this.collectionPointList.splice(index, 1);
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值