• vue2+axios实现修改和删除element-ui表格数据


    共用Dialog组件

    为了使添加按钮和编辑按钮能够共用Dialog组件,把Dialog.vue中的formData定义移动到父组件FundList.vue中,再将其传递到Dialog.vue中。

    <template>
    	<div>
    	<!--此处省略-->
          <Dialog :dialogue="dialogue" :formData="formData" @update="getProfile"></Dialog>
        </div>
    </template>
    
    <script>
        import Dialog from '../components/Dialog';
        export default {
            name: "fundList",
            components: {
                Dialog
            },
            data() {
                return {
                    tableData: [],
                    formData: {
                        type: "",
                        description: "",
                        income: "",
                        expense: "",
                        cash: "",
                        remark: "",
                        id: ""
                    },
                    dialogue: {
                        show: false
                    }
                };
            },
            created() {
                this.getProfile();
            },
            methods: {
                //...
            }
        }
    
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    Dialog.vue中通过props获取从FundList.vue传递过来的数据:

    <script>
        export default {
            name: "dialogue",
            data() {
                return {
                 
                    format_type_list: [
                        "提现","手续费","管理费","充值","转账","优惠劵"
                    ],
                    form_rules: {
    				    //...
                    }
                }
            },
            props: {
                dialogue: Object,
                formData: Object
            },
            methods: {
                //...
            }
        };
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    根据按钮显示对话框

    编辑FundList.vue,修改点击添加和编辑按钮时调用的方法,分别展示不同的对话框信息。

    <script>
        import Dialog from '../components/Dialog';
        export default {
            name: "fundList",
            components: {
                Dialog
            },
            data() {
                return {
                    tableData: [],
                    formData: {
                        type: "",
                        description: "",
                        income: "",
                        expense: "",
                        cash: "",
                        remark: "",
                        id: ""
                    },
                    dialogue: {
                        show: false,
                        title: '',
                        option: 'edit'
                    }
                };
            },
            created() {
                this.getProfile();
            },
            methods: {
                getProfile() {
                    this.$axios.get('/api/profiles')
                        .then(res => {
                            this.tableData = res.data;
                        })
                        .catch(err => console.log(err));
                },
                handleEdit(index, row) {
                    //console.log("HANDLE EDIT");
                    this.dialogue = {
                        show: true,
                        title: "修改资金信息",
                        option: "edit"
                    };
                    this.formData = {
                        type: row.type,
                        description: row.description,
                        income: row.income,
                        expense: row.expense,
                        cash: row.cash,
                        remark: row.remark,
                        id: row._id
                    };
                },
                handleDelete(index, row) {
                    console.log("HANDLE DELETE");
                },
                handleAdd() {
                    //console.log("HANDLE ADD");
                    this.dialogue = {
                        show: true,
                        title: "添加资金信息",
                        option: "add"
                    };
                    this.formData = {
                        type: '',
                        description: '',
                        income: '',
                        expense: '',
                        cash: '',
                        remark: '',
                        id: ''
                    };
                }
            }
        }
    
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    根据按钮提交表格

    编辑Dialog.vue,修改onSubmit提交方法,根据按钮类型区分为添加数据和编辑数据,分别调用不同的后端接口。

    <template>
        <div class="dialogue">
            <el-dialog :title="dialogue.title"
                       :visible.sync="dialogue.show"
                       :close-on-click-modal="false"
                       :modal-append-to-body="false">
                <!--此处省略-->
            </el-dialog>
        </div>
    </template>
    
    <script>
        export default {
            name: "dialogue",
            data() {
                return {
                 
                    format_type_list: [
                        "提现","手续费","管理费","充值","转账","优惠劵"
                    ],
                    form_rules: {
                        //...
                    }
                }
            },
            props: {
                dialogue: Object,
                formData: Object
            },
            methods: {
                onSubmit(form) {
                    this.$refs[form].validate(valid => {
                        if (valid) {
                            //console.log(this.formData);
                            const msg = this.dialogue.option == 'add' ? "添加成功" : "修改成功";
                            const url = this.dialogue.option == 'add' ? 'add' : `edit/${this.formData.id}`;
                            this.$axios.post(`/api/profiles/${url}`, this.formData)
                                .then(res => {
                                    this.$message({
                                        message: msg,
                                        type: "successs"
                                    });
                                    // 关闭对话框
                                    this.dialogue.show = false;
                                   // 刷新表格信息
                                    this.$emit('update');
                                });
                        }
                    })
                }
            }
        };
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    删除表格记录

    编辑FundList.vue,定义点击删除按钮时调用的方法:

    handleDelete(index, row) {
        //console.log("HANDLE DELETE");
        this.$axios.delete(`/api/profiles/delete/${row._id}`)
            .then(res => {
                this.$message("删除成功");
                this.getProfile();
            })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    SoapUI实践:自动化测试、压力测试、持续集成
    数据结构之单链表
    【笔记篇】12订单履约系统——之《实战供应链》
    【代码审计-PHP】审计方法、敏感函数、功能点
    kibana 7安装
    35了,我该何去何从
    2、HTML——标题分组、居中、引用标签、水平线标签下划线标签、删除标签、<font>标签、图像标签
    【虚拟语气练习题】对过去的虚拟
    面向接口编程实践之aspnetcoreapi的抽象
    Vue如何实现单选、全选、反选
  • 原文地址:https://blog.csdn.net/Sebastien23/article/details/125610337