vue el-table字段點(diǎn)擊出現(xiàn)el-input輸入框,失焦保存方式
一、效果展示
當(dāng)沒有數(shù)據(jù)初始化展示如下:

有數(shù)據(jù)展示數(shù)據(jù),點(diǎn)擊出現(xiàn)輸入框, 失焦保存修改

二、代碼實(shí)現(xiàn)
<!-- @cell-click="cellClick" 當(dāng)前單擊的單元格 -->
<el-table
ref="table"
size="mini"
height="100%"
row-key="id"
:data="tableData"
:row-class-name="getRowClass"
@cell-click="cellClick"
>
<el-table-column width="100" label="排序" show-overflow-tooltip>
<template slot-scope="scope">
<span
v-if="
scope.row.index === dbClickIndex &&
dbClickLabel === '排序'
"
>
<el-input
ref="sortNumRef"
v-model="sortNum"
placeholder="請(qǐng)輸入"
@blur="inputBlur(scope.row)"
:pattern="numberPattern"
/>
</span>
<div v-else>
<div class="flex_between cursor_pointer">
<span
:style="{ color: !scope.row.sortNum ? '#bbb' : '' }"
>{{ scope.row.sortNum || '請(qǐng)輸入' }}</span
>
<i class="el-icon-edit" style="color: #1989fe"></i>
</div>
</div>
</template>
</el-table-column>
</el-table> data() {
return {
sortNum: null,
dbClickIndex: null, // 點(diǎn)擊的單元格
dbClickLabel: '', // 當(dāng)前點(diǎn)擊的列名
numberPattern: '[1-9]\\d*', // 正則表達(dá)式,限制只能輸入正整數(shù)
}
}
methods:{
// 把每一行的索引放進(jìn)row
getRowClass({ row, rowIndex }) {
row.index = rowIndex
},
// row 當(dāng)前行 column 當(dāng)前列
cellClick(row, column, cell, event) {
if (column.label === '排序') {
this.dbClickIndex = row.index
this.dbClickLabel = column.label
this.sortNum = row.sortNum
//聚焦input
this.$nextTick(() => {
this.$refs.sortNumRef.focus()
})
}
},
// 失去焦點(diǎn)初始化
inputBlur(row, event, column) {
this.editThemeIndex(row)
this.dbClickIndex = null
this.dbClickLabel = ''
this.sortNum = null
},
// 編輯主題指標(biāo)列表-排序字段
editThemeIndex(row) {
if (this.sortNum === '' || this.sortNum === row.sortNum) return
const params = {
sortNum: Number(this.sortNum) || '',
id: row.id
}
//接口請(qǐng)求
xxxApi(params).then((res) => {
if (res.code === 200) {
this.$message.success('修改成功')
this.refreshClick()
}
})
},
// 刷新
refreshClick(val) {
this.pages.pageIndex = 1
this.initTable()
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue數(shù)組動(dòng)態(tài)刷新失敗問題及解決
這篇文章主要介紹了vue數(shù)組動(dòng)態(tài)刷新失敗問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue 實(shí)現(xiàn)登錄界面驗(yàn)證碼功能
本文通過實(shí)例代碼給大家介紹了Vue 實(shí)現(xiàn)登錄界面 驗(yàn)證碼功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Vue3 構(gòu)建 Web Components使用詳解
這篇文章主要為大家介紹了Vue3 構(gòu)建 Web Components使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Vue清除定時(shí)器setInterval優(yōu)化方案分享
這篇文章主要介紹了Vue清除定時(shí)器setInterval優(yōu)化方案分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
解決vue 使用axios.all()方法發(fā)起多個(gè)請(qǐng)求控制臺(tái)報(bào)錯(cuò)的問題
這篇文章主要介紹了解決vue 使用axios.all()方法發(fā)起多個(gè)請(qǐng)求控制臺(tái)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

