element-ui直接在表格中點擊單元格編輯
最近由于公司開始使用elementUI,開發(fā)的過程中需要用到對表格的單元格進行編輯,下面是我自己實現(xiàn)表格可編輯的方式,感興趣的可以了解一下
實現(xiàn)效果

編輯之后對應(yīng)表格數(shù)據(jù)該字段值也就發(fā)生了變化,控制臺輸出所有數(shù)據(jù)即可查看變化
實現(xiàn)代碼
1、自定義編輯組件
<template>
<div class="editCell">
<div class="canEdit" v-if="CanEdit" @click="beginEdit">
<label v-show="!editStatus">
<span v-if="this.value!== null && this.value !== undefined && this.value !== ''">{{ value }}{{this.suffix}}</span>
<span v-else style="padding-left: 100%;padding-top: 100%;"/>
</label>
<label v-show="editStatus">
<input
type="text"
class="inputClass"
ref="input"
v-on:keyup.13="loseFocus"
:value="value"
@blur="loseFocus"
/>
</label>
</div>
<label class="cannotEdit" v-else>
<span>{{ value }}{{ suffix }}</span>
</label>
</div>
</template>
<script>
export default {
name: "EditCell",
props: {
/**
* 綁定值
*/
value: {
required: true
},
/**
* 是否可編輯
*/
CanEdit: {
type: Boolean,
default: true
},
/**
* 格式化函數(shù)
*/
formatData: {
type: Function,
default: value => {
return value;
}
},
/**
* 編輯后事件
*/
afterEdit: {
type: Function,
default: () => {}
},
/**
* 是否初始格式化
*/
initFormat: {
type: Boolean,
default: false
},
suffix: {
default: ""
}
},
data() {
return {
editStatus: false,
showData: "",
defaultData: "",
timeout: null
};
},
methods: {
/**
* 單擊開始編輯
*/
beginEdit() {
this.editStatus = true;
setTimeout(() => {
this.$refs.input.focus();
}, 1);
},
/**
* @param {event} event
* 丟失焦點關(guān)閉編輯狀態(tài),并保存數(shù)據(jù)
*/
loseFocus(event) {
let value = this.formatData(event.target.value);
this.editData(value);
this.closeEditStatus(value);
this.afterEdit(value);
},
/**
* 發(fā)布input事件修改數(shù)據(jù)
* @param value
*/
editData(value) {
this.$emit("input", value);
},
/**
* 關(guān)閉編輯狀態(tài)
* @param value
*/
closeEditStatus(value) {
this.editStatus = false;
},
/**
* 初始格式化數(shù)據(jù)
*/
initData() {
let newValue = this.formatData(this.value);
this.$emit("input", newValue);
}
},
mounted() {
if (this.initFormat) {
this.initData();
}
},
watch: {
'value': function(newVal){
this.$emit("input", this.formatData(newVal));
}
}
};
</script>
<style scoped>
.editCell {
height: 100%;
width: 100%;
}
.inputClass {
height: 30px;
width: 100%;
background-color: #fff;
border-radius: 4px;
border: 1px solid #dcdfe6;
color: #606266;
display: inline-block;
font-size: inherit;
line-height: 30px;
outline: 0;
padding: 0 15px;
transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
overflow: visible;
touch-action: manipulation;
margin: 0;
}
</style>
頁面調(diào)用
import EditCell from "@/components/EditCell/EditCell";
components: { EditCell},
<el-table-column
v-for="item in tableColumn"
:prop="item.dataIndex"
:label="item.title"
:width="item.width"
:align="item.align"
:key="item.id"
:fixed="item.fixed"
>
//此處調(diào)用自定義組件(dataIndex 為表頭數(shù)據(jù)中字段,相當于 展示表頭 老師對應(yīng)的 teacher名稱)
<template slot-scope="scope">
<span v-if="item.dataIndex !== 'batchInvest' && item.dataIndex !== 'remark'">{{scope.row[item.dataIndex]}}</span>
// 若需要格式化數(shù)據(jù) 可設(shè)置 :format-data="formatFun" formatFun此方法在當前頁methods中定義即可
<edit-cell v-else v-model="scope.row[item.dataIndex]" :can-edit="true"/>
</template>
<el-table-column
v-for="item2 in item.children"
:prop="item2.dataIndex"
:label="item2.title"
:width="item2.width"
:align="item2.align"
:key="item2.id"
:fixed="item2.fixed"
>
</el-table-column>
</el-table-column>
到此這篇關(guān)于element-ui直接在表格中點擊單元格編輯的文章就介紹到這了,更多相關(guān)element 單元格編輯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue偵聽器watch,調(diào)用this時出現(xiàn)undefined的問題
這篇文章主要介紹了解決vue偵聽器watch,調(diào)用this時出現(xiàn)undefined的問題,具有很好的參考2020-10-10
Vue設(shè)置別名聯(lián)想路徑即@/生效的方法
這篇文章主要給大家介紹了Vue設(shè)置別名聯(lián)想路徑即@/生效的方法,文中有詳細的代碼示例和圖文講解,具有一定的參考價值,需要的朋友可以參考下2023-11-11

