新增和编辑共用弹窗模板

新增和编辑弹窗模板

html 复制代码
<!-- 添加或编辑用例合集 -->
<template>
    <el-dialog
        :title="`${type == 'add' ? '新建' : type == 'edit' ? '编辑' : ''}`"
        :visible.sync="dialogVisible"
        width="560px"
        :close-on-click-modal="false"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="88px" class="demo-ruleForm">
            <el-form-item label="名称" prop="name"> 
                <el-input v-model="ruleForm.name" placeholder="请输入" :maxlength="30"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="desc">
                <el-input type="textarea" placeholder="请输入" :rows="4" :maxlength="200" v-model="ruleForm.desc"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="handleClose">取 消</el-button>
            <el-button size="small" type="primary" :loading="loading" @click="submitForm('ruleForm')">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "AddOrEditDialog",
    props: {
        rowData: {
            type: Object
        },
        // 弹窗类型
        type: {
            type: String,
        }
    },
    data() {
        return {
            dialogVisible: true,
            ruleForm: {
                name: '',
                desc: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入合集名称', trigger: 'blur' },
                    { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
                ],
                desc: [
                    { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
                ],
            },
        }
    },
    computed: {
    },
    created() {
        if(this.type == 'edit') {
            this.initForm()
        }
    },
    methods: {
        // 提交表单
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        handleClose() {
            this.$emit('closeDialog', false)
        },
        handleUpdate() {
            this.$emit('update')
        },
        // 初始化表单
        initForm() {
            
        },
    }
}
</script>

<style lang="less" scoped>

</style>

在组件中使用

html 复制代码
<AddOrEditDialog
:type="dialogType"
:rowData="rowData"
@closeDialog="handleClose"
@update="handleUpdate"
></AddOrEditDialog>
相关推荐
Zoey的笔记本10 小时前
敏捷与稳定并行:Scrum看板+BPM工具选型指南
大数据·前端·数据库·python·低代码
文心快码BaiduComate10 小时前
0代码手写!体验百度Comate的“魔法”:我造了个会理解情绪的中介层
前端·程序员·前端框架
zhengxianyi51510 小时前
ruoyi-vue-pro优化——让菜单支持多个参数,一键直达【经营分析】、【生产报表】、【销售报表】
vue.js·前后端分离·数据大屏·ruoyi-vue-pro优化
38242782710 小时前
表单提交验证:onsubmit与return详解
前端·javascript·html
前端小蜗10 小时前
普通前端程序员的 2025:没什么大胜利,但也没被生活击倒
前端
Coder_Boy_10 小时前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范实现细节
大数据·开发语言·人工智能·spring boot
lly20240610 小时前
SQL SELECT 语句详解
开发语言
bug总结10 小时前
身份证号脱敏的正确实现
前端·javascript·vue.js
superman超哥10 小时前
Rust 异步时间管理核心:Tokio 定时器实现机制深度剖析
开发语言·rust·编程语言·rust异步时间管理核心·tokio定时器实现机制·tokio定时器
朔北之忘 Clancy10 小时前
2025 年 9 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·数学·青少年编程·题解