Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)

简介: Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)

<template>
    <div style="padding: 20px">
        <el-dialog
                :title="dialogTitle"
                :visible.sync="dialogVisible"
                v-if="dialogVisible"
                width="60%"
        >
            <el-form ref="refForm" :disabled="action === 'scan'" :model="formData"
                     :label-width="(config.labelWidth || '100')+'px'"
                     :size="config.size||'mini'">
                <el-row :gutter="config.gutter">
                    <el-col :key="index" :span="item.span?item.span:12"
                            v-for="(item,index) in config.itemList.filter(item=>!item.hideInForm)">
                        <el-form-item
                                style="height: 36px"
                                :label="item.label+ (config.labelPostfix || ':')"
                                :prop="item.prop"
                                :rules="[
                                            {required: item.require, message: '请输入'+item.label},
                                            {pattern:item.pattern, message: item.formatMsg || '格式不符合规范' }
                                        ]"
                        >
                            <!--单行文本 input-->
                            <el-input
                                    v-if="item.type=='input'|| item.type===undefined"
                                    show-word-limit
                                    :disabled="item.disabled"
                                    :maxlength="item.maxlength"
                                    :placeholder="item.placeholder"
                                    :prefix-icon="item.preIcon"
                                    :style="'width:'+item.width||'100%'"
                                    clearable
                                    type="text"
                                    v-model="formData[item.prop]">
                            </el-input>
                            <!--单选 radio-->
                            <el-radio-group v-if="item.type=='radio'" v-model="formData[item.prop]">
                                <el-radio :key="index" :label="items.value" v-for="(items,index) in item.list||[]">
                                    {{items.label}}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span v-if="action !== 'scan'" slot="footer">
                    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                    <el-button size="small" type="primary" @click="confirm">确 定</el-button>
            </span>
        </el-dialog>
        <el-row style="margin: 16px 0px">
            <el-button size="mini" type="primary" @click="add">新增</el-button>
        </el-row>
        <el-table ref="refTable" :data="tableData" border>
            <el-table-column
                    v-for="(item,index) in config.itemList.filter(item=>!item.hideInTable)"
                    :align="item.align?item.align:'center'"
                    :label=item.label
                    :prop=item.prop
                    :min-width="item.minWidth?item.minWidth:'120'"
                    :formatter="item.formatter"
                    :key="index"
                    show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span v-if="item.list && item.list.length>0">{{getLabel(item.list,scope.row[item.prop])}}</span>
                    <span v-else>{{scope.row[item.prop]}}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    align="center"
                    width="160">
                <template slot-scope="scope">
                    <el-button @click="scan(scope.row)" type="text" size="small">查看</el-button>
                    <el-button @click="edit(scope.$index)" type="text" size="small">编辑</el-button>
                    <el-button @click="del(scope.$index,scope.row.id)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                index: null,
                action: '',
                dialogTitle: '',
                dialogVisible: false,
                config: {
                    itemList: [
                        {
                            label: '姓名',
                            prop: 'name',
                            type: 'input',
                            require: true
                        },
                        {
                            label: '性别',
                            prop: 'gender',
                            type: 'radio',
                            defaultValue: '1',
                            list: [
                                {
                                    label: '男',
                                    value: '1'
                                },
                                {
                                    label: '女',
                                    value: '2'
                                },
                            ]
                        },
                        {
                            label: '手机号码',
                            prop: 'phone',
                            type: 'input',
                            maxlength: 11,
                            pattern: this.GLOBAL.phoneRegExp,
                            hideInTable: true
                        },
                        {
                            label: '创建日期',
                            prop: 'date',
                            hideInForm: true
                        },
                    ]
                },
                formData: {},
                tableData: [],
            }
        },
        methods: {
            // 表单校验
            ifvalid() {
                let result = true
                this.$refs.refForm.validate(valid => {
                    if (!valid) {
                        result = false
                    }
                })
                return result
            },
            // 保存/修改确认
            confirm() {
                if (this.ifvalid()) {
                    if (this.action === 'add') {
                        this.$set(this.formData,'date',new Date().format("yyyy-MM-dd hh:mm:ss"))
                        this.tableData.push(this.formData)
                    }
                    if (this.action === 'edit') {
                        this.$set(this.tableData, this.index, this.formData)
                    }
                    this.dialogVisible = false
                }
            },
            add() {
                this.action = 'add'
                this.dialogTitle = '新增'
                this.formData = {}
                this.config.itemList.forEach(
                    item => {
                        if (item.defaultValue !== undefined) {
                            this.$set(this.formData, item.prop, item.defaultValue)
                        }
                    }
                )
                this.dialogVisible = true
            },
            edit(index) {
                this.index = index
                this.action = 'edit'
                this.dialogTitle = '修改'
                this.formData = JSON.parse(JSON.stringify(this.tableData[index]))
                this.dialogVisible = true
            },
            del(index, id) {
                this.$confirm('确定删除吗?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.tableData.splice(index, 1);
                })
            },
            scan(row) {
                this.formData = row
                this.action = 'scan'
                this.dialogTitle = '详情'
                this.dialogVisible = true
            },
            // 表格中,显示表单中下拉数据的中文
            getLabel(list, value) {
                for (let item of list) {
                    if (item.value === value) {
                        return item.label
                        break
                    }
                }
            },
        }
    }
 
 
    // 为Date原型添加时间格式化方法
    Date.prototype.format = function(fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            S: this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                (this.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1
                        ? o[k]
                        : ("00" + o[k]).substr(("" + o[k]).length)
                );
            }
        }
        return fmt;
    };
</script>
<style scoped>
</style>
目录
相关文章
|
2天前
Element UI 表格数据格式化
Element UI 表格数据格式化
4 0
|
2天前
|
存储 JavaScript 容器
Element UI表格拖拽(vue中) —— 行拖拽、列拖拽
Element UI表格拖拽(vue中) —— 行拖拽、列拖拽
6 0
|
2天前
Element UI 表格翻页全选
Element UI 表格翻页全选
6 0
|
2月前
|
Web App开发 缓存 前端开发
前端性能优化的整理笔记(一)
前端性能优化的整理笔记(一)
114 0
|
6天前
|
前端开发 JavaScript 数据库
如何实现前后端分离-----前端笔记
如何实现前后端分离-----前端笔记
|
6天前
|
前端开发 安全 NoSQL
技术笔记:Security前端页面配置
技术笔记:Security前端页面配置
|
1月前
|
JSON 前端开发 JavaScript
前端Ajax、Axios和Fetch的用法和区别笔记
前端Ajax、Axios和Fetch的用法和区别笔记
29 2
|
2月前
|
存储 前端开发 JavaScript
前端笔记_OAuth规则机制下实现个人站点接入qq三方登录
前端笔记_OAuth规则机制下实现个人站点接入qq三方登录
52 1
|
2月前
|
移动开发 前端开发 JavaScript
10款精美的web前端源码的特效,2024年最新面试题+笔记+项目实战
10款精美的web前端源码的特效,2024年最新面试题+笔记+项目实战
|
2月前
|
前端开发 容器
CSS3属性详解(一)文本 盒模型中的 box-ssize 属性 处理兼容性问题:私有前缀 边框 背景属性 渐变 前端开发入门笔记(七)
CSS3属性详解(一)文本 盒模型中的 box-ssize 属性 处理兼容性问题:私有前缀 边框 背景属性 渐变 前端开发入门笔记(七)
42 2