antd vue表格可编辑单元格以及求和实现方式

 更新时间:2023年04月21日 10:13:02   作者:lyckkb520m  
这篇文章主要介绍了antd vue表格可编辑单元格以及求和实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

antd vue表格可编辑单元格以及求和实现

1、参照官网根据自己需要添加可编辑单元格组件

新建EditableCell.vue

<template>
    <div class="editable-cell">
        <div v-if="editable && isEditable" class="editable-cell-input-wrapper">  
            <a-input ref='inputs' style="height: 30px" :type='type ? "number" : "text"' :value="value" @change="handleChange" @blur="check" />
        </div>
        <div v-else class="editable-cell-text-wrapper" @dblclick="edit">
            <span>{{ value }}</span>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        text: [String, Number],
        type: Boolean,
        isEditable: {
            default: true,
            type: Boolean,
        },
    },
    data() {
        return {
            value: this.text,
            editable: false,
        };
    },
    methods: {
        handleChange(e) {
	        const value = e.target.value;
	        this.value = value;
        },
        check() {
            this.editable = false;
            this.$emit('change', this.value);
        },
        edit() {
            this.editable = true;
            this.$nextTick((x) => { 
                if (this.$refs.inputs) {
                    this.$refs.inputs.focus();
                }
            })
        },
    },
};
</script>
<style scoped>
.editable-cell {
    position: relative;
}
.editable-cell-text-wrapper {
    padding: 4px 5px 5px 5px;
    cursor: pointer;
}
</style>

2、需要的页面引入

<template>
  <div style="margin: 24px">
    <a-table
      :bordered="true"
      :columns="tableColumn"
      :dataSource="tableData"
      :rowKey="record => record.index"
      size="small"
      :pagination="false"
    >
      <template v-for="item in 5" :slot="'month' + item" slot-scope="text, record">
        <div :key="item">
          <editable-cell
            v-if="record.title != '合计'"
            :text="text"
            :isEditable="true"
            @change="onCellChange(record, 'month' + item, $event, 'tableData')"
          />
          <!--合计行不可编辑,需要单独写,不然无法视图上无法显示-->
          <span v-else>{{text}}</span>
        </div>
      </template>
    </a-table>
  </div>
</template>

<script>
import EditableCell from "@/components/EditableCell";
export default {
  name: "App",
  components: {
    EditableCell
  },
  data() {
    return {
      tableData: [
        { index: 0, title: "合计" },
        { index: 1, title: "费用1" },
        { index: 2, title: "费用2" },
        { index: 3, title: "费用3" },
        { index: 4, title: "费用4" },
        { index: 5, title: "费用5" }
      ],
      tableColumn: []
    };
  },
  mounted() {
    this.initTable();
  },
  methods: {
    initTable() {
      let array = [3]; //设置可编辑列
      this.tableColumn = [
        { title: "类别", align: "center", dataIndex: "title" },
        {
          title: "01",
          align: "center",
          dataIndex: "month1",
          width: 80,
          //判断该列是否可编辑
          scopedSlots: array.includes(1) ? { customRender: "month1" } : ""
        },
        {
          title: "02",
          align: "center",
          dataIndex: "month2",
          width: 80,
          scopedSlots: array.includes(2) ? { customRender: "month2" } : ""
        },
        {
          title: "03",
          align: "center",
          dataIndex: "month3",
          width: 80,
          scopedSlots: array.includes(3) ? { customRender: "month3" } : ""
        },
        {
          title: "04",
          align: "center",
          dataIndex: "month4",
          width: 80,
          scopedSlots: array.includes(4) ? { customRender: "month4" } : ""
        },
        {
          title: "05",
          align: "center",
          dataIndex: "month5",
          width: 80,
          scopedSlots: array.includes(5) ? { customRender: "month5" } : ""
        }
      ];
    },
    onCellChange(key, dataIndex, value, tableName) {
      var obj = {
        index: key.index,
        title: key.title
      };
      obj[dataIndex] = value;
      const dataSource = [...this[tableName]];
      const target = dataSource.find(item => item.index === key.index);
      if (target) {
        if (target[dataIndex] !== value) {
          target[dataIndex] = value;
          if (!dataSource[0][dataIndex]) {
            dataSource[0][dataIndex] = 0;
          }
          dataSource[0][dataIndex] += value * 1;
          this[tableName] = dataSource;
        }
      }
    }
  }
};
</script>

注意点:合计行是直接由下面几行汇总求和的,不需要设置为可编辑的,如果设置为可编辑,可编辑单元格无法动态获取数据变化,所以无法实时更新到页面上

antd vue 表格可编辑问题

template:

<a-table :columns="tableColumns" :data-source="tableData">
          <span v-for="i in tableColumns" :key="i.dataIndex" :slot="i.dataIndex" slot-scope="text" contentEditable=true>            
                  {{text}}
          </span>
</a-table>    

在tableColumns中:

const tableColumns = [
    { title: "编号", dataIndex:"stdId",
      scopedSlots: { customRender: "stdId" }}
];

还有一个问题就是点击单元格会出现一个border,取消掉的css样式:

[contenteditable]:focus{outline: none;}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue项目中使用jquery的简单方法

    Vue项目中使用jquery的简单方法

    这篇文章主要给大家介绍了关于Vue项目中使用jquery的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • vue 跳转到其他页面并关闭当前页面的实现代码

    vue 跳转到其他页面并关闭当前页面的实现代码

    我在做一个调用虚拟机录屏的一个操作,需要在浏览器页面上,点击按钮后,关闭当前页面里的某一个页面,并且打开浏览器新页面是虚拟机的页面,本文给大家介绍vue 跳转到其他页面并关闭当前页面的实现代码,感兴趣的朋友一起看看吧
    2023-09-09
  • 浅谈Vue数据绑定的原理

    浅谈Vue数据绑定的原理

    本篇文章主要介绍了浅谈Vue数据绑定的原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Vue3+antDesignVue实现表单校验的方法

    Vue3+antDesignVue实现表单校验的方法

    这篇文章主要为大家详细介绍了基于Vue3和antDesignVue实现表单校验的方法,文中的示例代码讲解详细,具有一定的参考价值,需要的小伙伴可以了解下
    2024-01-01
  • vue解决一个方法同时发送多个请求的问题

    vue解决一个方法同时发送多个请求的问题

    今天小编就为大家分享一篇vue解决一个方法同时发送多个请求的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • vue3使用vue-router及路由权限拦截方式

    vue3使用vue-router及路由权限拦截方式

    这篇文章主要介绍了vue3使用vue-router及路由权限拦截方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • Vue项目打包并部署nginx服务器的详细步骤

    Vue项目打包并部署nginx服务器的详细步骤

    vue项目开发好之后需要部署到服务器上进行外网访问,下面这篇文章主要给大家介绍了关于Vue项目打包并部署nginx服务器的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • vue如何使用文件流进行下载(new Blob)

    vue如何使用文件流进行下载(new Blob)

    这篇文章主要介绍了vue如何使用文件流进行下载(new Blob),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Vite3迁移Webpack5的实现

    Vite3迁移Webpack5的实现

    本文主要介绍了Vite3迁移Webpack5的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • vue实现图形验证码

    vue实现图形验证码

    这篇文章主要为大家详细介绍了vue实现图形验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04

最新评论