Antd+Vue2实现动态编辑表格

一.新建Vue2项目

vue create demo

二.安装引入Antd

1.安装Antd

yarn add ant-design-vue@1.7.8

2.引入Antd

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//引入svg
import './icons'

Vue.config.productionTip = false

import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';

Vue.use(Antd);

/* eslint-disable no-new */
//runtime

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app")

三.修改组件代码

<template>
    <a-form-model ref="tableform" :model="form">
        <a-table :columns="columns" :data-source="form.tableData" :row-key="(record) => record.id" :pagination="false" :loading="loading">
            <template v-for="col in ['name','age','address']" :slot="col" slot-scope="text, record, index">
                <div :key="col">
                    <a-form-model-item v-if="record.editable" :prop="'tableData.'+index+'.'+col" :rules="rules[col]">
                        <a-input :ref="'input'+col" v-model="record[col]" @blur="e => handleBlur(e.target.value, record.key, col)"/>
                    </a-form-model-item>
                    <template v-else>
                        <span style="display:inline-block;width: 100%;" @click="e => edit(e.target.value, record.key, col)">{{text}}</span>
                    </template>
                </div>
            </template>
        </a-table>
    </a-form-model>
</template>

<script>
    export default {
        data() {
            return {
                columns:[
                    {
                        title: 'id',
                        align: 'center',
                        dataIndex: 'id',
                        key: 'id',
                        slots: { title: 'id' },
                        scopedSlots: { customRender: 'id' },
                    },{
                        title: 'name',
                        align: 'center',
                        dataIndex: 'name',
                        key: 'name',
                        slots: { title: 'name' },
                        scopedSlots: { customRender: 'name' },
                    },{
                        title: 'age',
                        align: 'center',
                        dataIndex: 'age',
                        key: 'age',
                        slots: { title: 'age' },
                        scopedSlots: { customRender: 'age' },
                    },{
                        title: 'address',
                        align: 'center',
                        dataIndex: 'address',
                        key: 'address',
                        slots: { title: 'address' },
                        scopedSlots: { customRender: 'address' },
                    }
                ],
                loading: false,
                editingKey: '',

                form:{
                    tableData:
                        [
                            {id:1,key:1,name:'张三',age:12,address:'河北保定'},
                            {id:2,key:2,name:'李四',age:24,address:'河南驻马店'}
                        ]
                },
                rules: {
                    name: { required: true, message: '姓名不能为空!' },
                    age: { required: true, message: '年龄不能为空!' },
                    address: { required: true, message: '地址不能为空!' },
                }
            }
        },
        methods: {
            edit(value, key, column) {
                console.log(value, key, column)
                const newData = [...this.form.tableData];
                const target = newData.find(item => key === item.key);
                this.editingKey = key;
                if (target) {
                    target.editable = true;
                    this.form.tableData = newData;
                    this.$nextTick(() => {
                        let id = 'input' + column
                        console.log(this.$refs[id])
                        this.$refs[id][0].focus()
                    })
                }
            },
            handleBlur (value, key, column) {
                this.$refs.tableform.validate(async valid => {
                    if (valid) {
                        const newData = [...this.form.tableData];
                        const target = newData.find(item => key === item.key);
                        this.editingKey = key;
                        if (target) {
                            delete target.editable
                            this.form.tableData = newData;
                        }
                        //调取编辑接口
                        this.$message.success('提交成功')
                    }
                })
            },

        }
    }
</script>
<style scoped>
    /deep/.ant-form-item {
        margin: 0 !important;
    }
    /deep/.ant-table-row {
        height: 75px;
    }
</style>

如果文章有帮助的话,欢迎一键三连,感谢支持!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要搭建一个基于Vite、Ant Design和Vue 3的项目,你可以按照以下步骤进行操作: 1. 首先,你需要安装Vite。你可以通过运行以下命令来安装最新版本的Vite: ``` npm init vite@latest my-project cd my-project npm install ``` 这将创建一个新的Vite项目,并安装所有必要的依赖项。 2. 接下来,你需要安装Ant Design Vue。运行以下命令来安装Ant Design Vue库: ``` npm install ant-design-vue@next ``` 这将安装最新版本的Ant Design Vue,并将其添加到你的项目中。 3. 配置路由。你可以通过以下步骤来配置Vue Router: - 首先,运行以下命令来安装Vue Router: ``` npm install vue-router@next ``` - 在你的项目的src目录下创建一个名为"router"的文件夹。 - 在"router"文件夹中创建一个名为"index.js"的文件,并配置你的路由信息。 - 最后,在项目的"main.js"文件中引入并使用Vue Router。 以上是使用Vite、Ant Design VueVue 3搭建项目的基本步骤。你可以根据需要进一步添加其他功能和组件。祝你搭建项目成功!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3+vite+antd 后台管理系统基础模板](https://download.csdn.net/download/yehaocheng520/87420798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vite + vue3 + Antd 搭建后台管理系统](https://blog.csdn.net/m0_58094704/article/details/127850749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值