antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式
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ú)寫,不然無(wú)法視圖上無(wú)法顯示-->
<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è)置為可編輯,可編輯單元格無(wú)法動(dòng)態(tài)獲取數(shù)據(jù)變化,所以無(wú)法實(shí)時(shí)更新到頁(yè)面上
antd vue 表格可編輯問(wèn)題
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è)問(wèn)題就是點(diǎn)擊單元格會(huì)出現(xiàn)一個(gè)border,取消掉的css樣式:
[contenteditable]:focus{outline: none;}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)大文件切片斷點(diǎn)續(xù)傳
上傳文件,基本上用了input框就可以解決,但大文件應(yīng)該怎樣上傳呢,一次性上傳明顯不現(xiàn)實(shí),因?yàn)槊看我粩嗑W(wǎng),那就會(huì)從頭開始上傳,所以切片勢(shì)在必行,關(guān)鍵就是如何切,怎么保障文件數(shù)據(jù)不會(huì)丟失,同時(shí)優(yōu)化上傳保障機(jī)制,本文就來(lái)給大家講講如何上傳大文件2023-07-07
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
vue3中使用scss加上scoped導(dǎo)致樣式失效問(wèn)題
這篇文章主要介紹了vue3中使用scss加上scoped導(dǎo)致樣式失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

