element el-table如何實(shí)現(xiàn)表格動態(tài)增加/刪除/編輯表格行(帶校驗(yàn)規(guī)則)
本篇文章記錄el-table增加一行可編輯的數(shù)據(jù)列,進(jìn)行增刪改。
1.增加空白行
直接在頁面mounted時(shí)對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)品名稱 price: '',//單價(jià)(元/㎡) productCount: '1', //產(chǎn)品件數(shù) totalAmount: '', //小計(jì)¥元 }) }) },
2.產(chǎn)品名稱選中拿到數(shù)據(jù)展示到列表行
因?yàn)楫?dāng)前案例的產(chǎn)品名是下拉選擇的,所以我們要請求接口拿到數(shù)據(jù)渲染到下拉列表,這里直接使用了假數(shù)據(jù)。
data() { return { addFormData: { // 產(chǎn)品列表 productList: [], }, addFormRules: { productName: [{ required: true, message: '請選擇產(chǎn)品', trigger: 'blur' }], price: [{ required: true, message: '請輸入單價(jià)', trigger: 'blur' } ], productCount: [{ required: true, message: '請輸入產(chǎn)品件數(shù)', trigger: 'blur' }] }, optionsList: [ { id:1, productName:'橘子', price:'10', }, { id:2, productName:'蘋果', 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)品名稱" 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="請選擇" @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)賦值給我們這一行對應(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ì)通過(計(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)品名稱 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)擊提交對表單校驗(yàn)
// 添加訂單表單提交 handleSubmitAdd() { this.$refs.addFormRef.validate(async (valid) => { if (!valid) return //校驗(yàn)通過 往下執(zhí)行 }) },
到此這篇關(guān)于element el-table實(shí)現(xiàn)表格動態(tài)增加/刪除/編輯表格行,帶校驗(yàn)規(guī)則的文章就介紹到這了,更多相關(guān)element el-table表格動態(tài)增加內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue任意關(guān)系組件通信與跨組件監(jiān)聽狀態(tài)vue-communication
這篇文章主要介紹了vue任意關(guān)系組件通信與跨組件監(jiān)聽狀態(tài)vue-communication,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Vue?通過this.$emit()方法子組件向父組件傳值(步驟分享)
這篇文章主要介紹了Vue?this.$emit()方法通過子組件向父組件傳值,第一步在父組件中引入子組件,第二步子組件向父組件傳值,本文通過需要的朋友可以參考下2022-11-11Jenkins?Sidebar?Link插件實(shí)現(xiàn)添加側(cè)邊欄功能詳解
這篇文章主要介紹了vue框架實(shí)現(xiàn)添加側(cè)邊欄,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12element-UI el-table修改input值視圖不更新問題
這篇文章主要介紹了element-UI el-table修改input值視圖不更新問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02vue中el-table實(shí)現(xiàn)可拖拽移動列和動態(tài)排序字段
最近公司需要做個(gè)項(xiàng)目,需要拖拽表格和自定義表格字段,本文主要介紹了vue中el-table實(shí)現(xiàn)可拖拽移動列和動態(tài)排序字段,具有一定吃參考價(jià)值,感興趣的可以了解一下2023-12-12如何使用VuePress搭建一個(gè)類型element ui文檔
這篇文章主要介紹了如何使用VuePress搭建一個(gè)類型element ui文檔,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02vue3在單個(gè)組件中實(shí)現(xiàn)類似mixin的事件調(diào)用
這篇文章主要為大家詳細(xì)介紹了vue3如何在單個(gè)組件中實(shí)現(xiàn)類似mixin的事件調(diào)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01