欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式

 更新時間:2023年04月21日 10:13:02   作者:lyckkb520m  
這篇文章主要介紹了antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

antd vue表格可編輯單元格以及求和實(shí)現(xiàn)

1、參照官網(wǎng)根據(jù)自己需要添加可編輯單元格組件

新建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 != '合計(jì)'"
            :text="text"
            :isEditable="true"
            @change="onCellChange(record, 'month' + item, $event, 'tableData')"
          />
          <!--合計(jì)行不可編輯,需要單獨(dú)寫,不然無法視圖上無法顯示-->
          <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: "合計(jì)" },
        { index: 1, title: "費(fèi)用1" },
        { index: 2, title: "費(fèi)用2" },
        { index: 3, title: "費(fèi)用3" },
        { index: 4, title: "費(fèi)用4" },
        { index: 5, title: "費(fèi)用5" }
      ],
      tableColumn: []
    };
  },
  mounted() {
    this.initTable();
  },
  methods: {
    initTable() {
      let array = [3]; //設(shè)置可編輯列
      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>

注意點(diǎn):合計(jì)行是直接由下面幾行匯總求和的,不需要設(shè)置為可編輯的,如果設(shè)置為可編輯,可編輯單元格無法動態(tài)獲取數(shù)據(jù)變化,所以無法實(shí)時更新到頁面上

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" }}
];

還有一個問題就是點(diǎn)擊單元格會出現(xiàn)一個border,取消掉的css樣式:

[contenteditable]:focus{outline: none;}

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Vue整合axios的實(shí)例代碼

    詳解Vue整合axios的實(shí)例代碼

    本篇文章主要介紹了詳解Vue整合axios的實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Vue.js中的computed工作原理

    Vue.js中的computed工作原理

    這篇文章,我們通過實(shí)現(xiàn)一個簡單版的和Vue中computed具有相同功能的函數(shù)來了解computed是如何工作的。對Vue.js中的computed工作原理感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-03-03
  • vue實(shí)現(xiàn)大文件切片斷點(diǎn)續(xù)傳

    vue實(shí)現(xiàn)大文件切片斷點(diǎn)續(xù)傳

    上傳文件,基本上用了input框就可以解決,但大文件應(yīng)該怎樣上傳呢,一次性上傳明顯不現(xiàn)實(shí),因?yàn)槊看我粩嗑W(wǎng),那就會從頭開始上傳,所以切片勢在必行,關(guān)鍵就是如何切,怎么保障文件數(shù)據(jù)不會丟失,同時優(yōu)化上傳保障機(jī)制,本文就來給大家講講如何上傳大文件
    2023-07-07
  • Vue使用虛擬dom進(jìn)行渲染view的方法

    Vue使用虛擬dom進(jìn)行渲染view的方法

    這篇文章主要介紹了Vue使用虛擬dom進(jìn)行渲染view的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • vue-cli3 從搭建到優(yōu)化的詳細(xì)步驟

    vue-cli3 從搭建到優(yōu)化的詳細(xì)步驟

    這篇文章主要介紹了vue-cli3 從搭建到優(yōu)化的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • elementUI中el-upload文件上傳的實(shí)現(xiàn)方法

    elementUI中el-upload文件上傳的實(shí)現(xiàn)方法

    ElementUI的組件支持多種事件鉤子,如http-request、before-upload和on-change,以實(shí)現(xiàn)自定義文件上傳處理,這篇文章主要介紹了elementUI中el-upload文件上傳的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2024-11-11
  • vueCl如何查看打包后文件的大小占比

    vueCl如何查看打包后文件的大小占比

    這篇文章主要介紹了vueCl如何 查看打包后文件的大小占比問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • vue的token刷新處理的方法

    vue的token刷新處理的方法

    這篇文章主要介紹了vue的token刷新處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • vue中EventBus的使用教程詳解

    vue中EventBus的使用教程詳解

    在Vue中,使用EventBus可以實(shí)現(xiàn)組件間的通信,如何使用EventBus??都需要做哪些配置呢?他的注意事項(xiàng)是什么呢?下面就跟隨小編一起學(xué)習(xí)一下吧
    2024-02-02
  • vue3中使用scss加上scoped導(dǎo)致樣式失效問題

    vue3中使用scss加上scoped導(dǎo)致樣式失效問題

    這篇文章主要介紹了vue3中使用scss加上scoped導(dǎo)致樣式失效問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論