element el-table如何實(shí)現(xiàn)表格動(dòng)態(tài)增加/刪除/編輯表格行(帶校驗(yàn)規(guī)則)
本篇文章記錄el-table增加一行可編輯的數(shù)據(jù)列,進(jìn)行增刪改。

1.增加空白行
直接在頁(yè)面mounted時(shí)對(duì)form里面的table列表增加一行數(shù)據(jù),直接使用push() 方法增加一列數(shù)據(jù)這個(gè)時(shí)候也可以設(shè)置一些默認(rèn)值。比如案例里面的 產(chǎn)品件數(shù) 。
mounted() {
this.$nextTick(() => {
this.addFormData.productList.push({
productName: '',//產(chǎn)品名稱(chēng)
price: '',//單價(jià)(元/㎡)
productCount: '1', //產(chǎn)品件數(shù)
totalAmount: '', //小計(jì)¥元
})
})
},2.產(chǎn)品名稱(chēng)選中拿到數(shù)據(jù)展示到列表行
因?yàn)楫?dāng)前案例的產(chǎn)品名是下拉選擇的,所以我們要請(qǐng)求接口拿到數(shù)據(jù)渲染到下拉列表,這里直接使用了假數(shù)據(jù)。
data() {
return {
addFormData: {
// 產(chǎn)品列表
productList: [],
},
addFormRules: {
productName: [{
required: true,
message: '請(qǐng)選擇產(chǎn)品',
trigger: 'blur'
}],
price: [{
required: true,
message: '請(qǐng)輸入單價(jià)',
trigger: 'blur'
}
],
productCount: [{
required: true,
message: '請(qǐng)輸入產(chǎn)品件數(shù)',
trigger: 'blur'
}]
},
optionsList: [
{
id:1,
productName:'橘子',
price:'10',
},
{
id:2,
productName:'蘋(píng)果',
price:'8',
}
]
}
},
<el-form ref="addFormRef" :model="addFormData" :rules="addFormRules" size="mini" :inline="true">
<el-table tooltip-effect="light" :data="addFormData.productList" >
<el-table-column label="產(chǎn)品名稱(chēng)" prop="productName" min-width="150">
<template slot-scope="scope">
<el-form-item size="mini" :prop="'productList.' + scope.$index + '.productName'"
:rules="addFormRules.productName" class="all">
<el-select v-model="scope.row.productName" filterable value-key="id" placeholder="請(qǐng)選擇"
@change="pestChange($event, scope.$index)">
<el-option v-for="item in optionsList" :key="item.id" :label="item.productName"
:value="item">
</el-option>
</el-select>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
width="150">
<template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateYes(scope.row)"
v-hasPermi="['system:order:edit']">增加</el-button>
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteProduct(scope.row)"
v-hasPermi="['system:order:remove']">刪除</el-button>
</template>
</el-table-column>
</el-table>
<el-form-item size="large">
<el-button type="primary" @click="handleSubmitAdd">提交</el-button>
<el-button @click="handleCancelAdd">取消</el-button>
</el-form-item>
</el-form>
pestChange(e, index) {
//此時(shí)的e 就是optionsList中的某一項(xiàng)
//讓后解構(gòu)賦值給我們這一行對(duì)應(yīng)的值
let data = this.addFormData.productList[index]
Object.keys(data).forEach(key => {
data[key] = e[key]
})
this.addFormData.productList[index].productCount = 1
},3.小計(jì)通過(guò)(計(jì)算屬性計(jì)算值)
<el-form ref="addFormRef" :model="addFormData" :rules="addFormRules" size="mini" :inline="true">
<el-table tooltip-effect="light" :data="addFormData.productList" >
<el-table-column label="小計(jì)¥元" prop="totalAmount" width="100">
<template slot-scope="scope">
<div class="notext">
{{ getTotalAmount(scope.row) }}
</div>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
width="150">
<template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateYes(scope.row)"
v-hasPermi="['system:order:edit']">增加</el-button>
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteProduct(scope.row)"
v-hasPermi="['system:order:remove']">刪除</el-button>
</template>
</el-table-column>
</el-table>
<el-form-item size="large">
<el-button type="primary" @click="handleSubmitAdd">提交</el-button>
<el-button @click="handleCancelAdd">取消</el-button>
</el-form-item>
</el-form>computed: {
getTotalAmount(){
return (data) => {
//先判斷單價(jià)和數(shù)量必須存在
if (data.productCount && data.price) {
data.totalAmount = parseInt(data.productCount) * parseInt(parseFloat(data.price))
return data.totalAmount
} else {
return 0.00
}
}
}
},4.再增加一行復(fù)用上一行的數(shù)據(jù)
handleUpdateYes(row) {
//拿到上一行數(shù)據(jù)再往數(shù)組中push()新的數(shù)據(jù)
this.addFormData.productList.push({
productName: row.productName,//產(chǎn)品名稱(chēng)
price: row.price,//單價(jià)(元/㎡)
productCount: row.productCount, //產(chǎn)品件數(shù)
totalAmount: '', //小計(jì)¥元
})
},5.刪除某一行
// 刪除產(chǎn)品
handleDeleteProduct(row) {
this.$confirm('此操作將永久刪除該產(chǎn)品信息, 是否繼續(xù)?', '提示', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.$message({
type: 'success',
message: '刪除成功!'
});
this.addFormData.productList.splice(row.index, 1)
}).catch(() => {
this.$message({
type: 'info',
message: '已取消刪除'
});
});
},6.點(diǎn)擊提交對(duì)表單校驗(yàn)
// 添加訂單表單提交
handleSubmitAdd() {
this.$refs.addFormRef.validate(async (valid) => {
if (!valid) return
//校驗(yàn)通過(guò) 往下執(zhí)行
})
},到此這篇關(guān)于element el-table實(shí)現(xiàn)表格動(dòng)態(tài)增加/刪除/編輯表格行,帶校驗(yàn)規(guī)則的文章就介紹到這了,更多相關(guān)element el-table表格動(dòng)態(tài)增加內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中g(shù)et和post請(qǐng)求的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于vue中g(shù)et和post請(qǐng)求的區(qū)別點(diǎn)總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-12-12
Vue對(duì)Element中的el-tag添加@click事件無(wú)效的解決
本文主要介紹了Vue對(duì)Element中的el-tag添加@click事件無(wú)效的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
利用Vue模擬實(shí)現(xiàn)element-ui的分頁(yè)器效果
這篇文章主要為大家詳細(xì)介紹了如何利用Vue模擬實(shí)現(xiàn)element-ui的分頁(yè)器效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-11-11
vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能
這篇文章主要為大家詳細(xì)介紹了vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
使用vue3實(shí)現(xiàn)簡(jiǎn)單的滑塊組件
這篇文章主要給大家介紹一下如何使用vue3實(shí)現(xiàn)簡(jiǎn)單的滑塊組件,文中有詳細(xì)的代碼示例講解,具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來(lái)看看吧2023-08-08
關(guān)于electron-vue打包后運(yùn)行白屏的解決方案
這篇文章主要介紹了關(guān)于electron-vue打包后運(yùn)行白屏的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue項(xiàng)目通過(guò)a標(biāo)簽下載圖片至zip包的示例代碼
在vue項(xiàng)目中,將圖片下載可使用流的形式,下載成單個(gè)圖片,或者將多個(gè)圖片下載至zip包,本文就是介紹使用a標(biāo)簽下載圖片的用法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10
uniapp組件uni-file-picker中設(shè)置使用照相機(jī)和相冊(cè)權(quán)限的操作方法
這篇文章主要介紹了uniapp組件uni-file-picker中設(shè)置使用照相機(jī)和相冊(cè)的權(quán)限,在uniapp中,我們通常會(huì)使用uni-file-picker這個(gè)組件,但是這個(gè)組件中,有點(diǎn)缺陷,就是沒(méi)有對(duì)這個(gè)功能的傳值設(shè)置,這里就要給組件進(jìn)行修改了,需要的朋友可以參考下2022-11-11

