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.js實(shí)現(xiàn)雙擊放大預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)雙擊放大預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
一文詳解如何創(chuàng)建Vue3項(xiàng)目及組合式API
Vue3提供了一種組合式API,可以用來構(gòu)建可復(fù)用的組件和應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于如何創(chuàng)建Vue3項(xiàng)目及組合式API的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
vue項(xiàng)目history模式刷新404問題解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目history模式刷新404問題的解決辦法,需要的朋友可以參考下2023-11-11
詳解mpvue中小程序自定義導(dǎo)航組件開發(fā)指南
這篇筆記主要記錄一下基于mpvue的小程序中實(shí)現(xiàn)自定義導(dǎo)航的思路及應(yīng)用。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Vue3組合式函數(shù)Composable實(shí)戰(zhàn)ref和unref使用
這篇文章主要為大家介紹了Vue3組合式函數(shù)Composable實(shí)戰(zhàn)ref和unref使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue3發(fā)送post請(qǐng)求出現(xiàn)400?Bad?Request報(bào)錯(cuò)的解決辦法
這篇文章主要給大家介紹了關(guān)于Vue3發(fā)送post請(qǐng)求出現(xiàn)400?Bad?Request報(bào)錯(cuò)的解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
laravel-admin 與 vue 結(jié)合使用實(shí)例代碼詳解
由于 Laravel-admin 采用的是 pjax 的方式刷新頁面,意味著很多頁面刷新的操作,這篇文章主要介紹了laravel-admin 與 vue 結(jié)合使用,需要的朋友可以參考下2019-06-06

