最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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、需要的頁(yè)面引入

<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è)置為可編輯,可編輯單元格無法動(dòng)態(tài)獲取數(shù)據(jù)變化,所以無法實(shí)時(shí)更新到頁(yè)面上

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: "編號(hào)", dataIndex:"stdId",
? ? ? scopedSlots: { customRender: "stdId" }}
];

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

[contenteditable]:focus{outline: none;}

總結(jié)

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

相關(guān)文章

  • 一文搞懂Vue中watch偵聽器的用法

    一文搞懂Vue中watch偵聽器的用法

    在Vue.js中,您可以使用watch選項(xiàng)來創(chuàng)建偵聽器,以偵聽特定屬性的變化,偵聽器可以在屬性發(fā)生變化時(shí)執(zhí)行相關(guān)的邏輯,本文給大家詳細(xì)講講Vue中watch偵聽器的用法,需要的朋友可以參考下
    2023-11-11
  • 使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能

    使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能

    項(xiàng)目中有一個(gè)需要預(yù)覽下載pdf的需求,網(wǎng)上找了很久,決定使用 pdf.js 完成,下面這篇文章主要給大家介紹了關(guān)于使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • 如何將百度地圖包裝成Vue的組件的方法步驟

    如何將百度地圖包裝成Vue的組件的方法步驟

    這篇文章主要介紹了如何將百度地圖包裝成Vue的組件的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • vue實(shí)現(xiàn)購(gòu)物車案例

    vue實(shí)現(xiàn)購(gòu)物車案例

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 解決vue單頁(yè)面修改樣式無法覆蓋問題

    解決vue單頁(yè)面修改樣式無法覆蓋問題

    這篇文章主要介紹了vue單頁(yè)面修改樣式無法覆蓋問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Vue Element前端應(yīng)用開發(fā)之組織機(jī)構(gòu)和角色管理

    Vue Element前端應(yīng)用開發(fā)之組織機(jī)構(gòu)和角色管理

    本篇文章繼續(xù)深化Vue Element權(quán)限管理模塊管理的內(nèi)容,介紹組織機(jī)構(gòu)和角色管理模塊的處理,使得我們了解界面組件化模塊的開發(fā)思路和做法,提高我們界面設(shè)計(jì)的技巧,并減少代碼的復(fù)雜性,提高界面代碼的可讀性,同時(shí)也是利用組件的復(fù)用管理。
    2021-05-05
  • Vue3 axios配置以及cookie的使用方法實(shí)例演示

    Vue3 axios配置以及cookie的使用方法實(shí)例演示

    這篇文章主要介紹了Vue3 axios配置以及cookie的使用方法,需要的朋友可以參考下
    2023-02-02
  • 使用Vue實(shí)現(xiàn)簡(jiǎn)單的信號(hào)和電池電量組件

    使用Vue實(shí)現(xiàn)簡(jiǎn)單的信號(hào)和電池電量組件

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)簡(jiǎn)單的信號(hào)和電池電量組件效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-04-04
  • vue3常用的指令之v-bind和v-on指令用法

    vue3常用的指令之v-bind和v-on指令用法

    vue的v-on與v-bind,v-on就是用于綁定事件的,下面這篇文章主要給大家介紹了關(guān)于vue3常用的指令之v-bind和v-on指令用法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • vue簡(jiǎn)單的二維數(shù)組循環(huán)嵌套方式

    vue簡(jiǎn)單的二維數(shù)組循環(huán)嵌套方式

    這篇文章主要介紹了vue簡(jiǎn)單的二維數(shù)組循環(huán)嵌套方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評(píng)論

萨迦县| 临城县| 蓝田县| 临洮县| 丰镇市| 远安县| 洞头县| 社旗县| 肥东县| 怀宁县| 喀喇沁旗| 阿坝| 深水埗区| 彰武县| 长岭县| 辛集市| 两当县| 中方县| 卢湾区| 商南县| 青岛市| 手机| 淮滨县| 河北省| 德钦县| 枞阳县| 淳化县| 平阳县| 安康市| 宁明县| 务川| 公安县| 福贡县| 同江市| 丹寨县| 高平市| 璧山县| 安国市| 衢州市| 巧家县| 屏东市|