vue+elementui通用彈窗的實(shí)現(xiàn)(新增+編輯)
本文主要介紹了vue+elementui通用彈窗的實(shí)現(xiàn)(新增+編輯),分享給大家,具體如下:
組件模板
<el-dialog :title="title" :visible.sync="dialogShow" :close-on-click-modal="false"> <div class="ym-common-dialog" :class="customClass"> <div v-for="(col,index) in cols"> <span><em v-if="!!col.isRequire">*</em>{{col.name}}</span> <template v-if="col.type === 'text'"> <div>{{submitData[col.key]}}</div> </template> <template v-if="col.type === 'input'"> <input type="text" v-model="submitData[col.key]" :placeholder="'請(qǐng)輸入' + col.name"> </template> <template v-if="col.type === 'radio'"> <div class="flexX"> <el-radio v-for="radio in col.data" v-model="submitData[col.key]" :label="radio.label">{{radio.name}}</el-radio> </div> </template> <template v-if="col.type === 'select'"> <el-select v-model="submitData[col.key]" placeholder="請(qǐng)選擇"> <el-option v-for="option in col.data" :key="option.value" :label="option.label" :value="option.value"> </el-option> </el-select> </template> </div> </div> <span slot="footer" class="dialog-footer"> <el-button @click="dialogShow = false">取 消</el-button> <el-button type="primary" @click="confirm">確 定</el-button> </span> </el-dialog>
彈窗的內(nèi)容根據(jù)傳入的數(shù)據(jù)去渲染,數(shù)據(jù)格式如下
cols: [{ name: '輸入框', key: 'ccc', // 提交時(shí)對(duì)應(yīng)的字段 type: 'input', // 類(lèi)型 isRequire: true // 是否必填 }, { name: '單選框', key: 'aaa', type: 'radio', data: [{ label: '1', name: '長(zhǎng)城' }, { label: '2', name: '長(zhǎng)安' }], isRequire: true }, { name: '下拉框', key: 'bbb', type: 'select', data: [{ value: '選項(xiàng)1', label: '黃金糕' }, { value: '選項(xiàng)2', label: '雙皮奶' }], isRequire: true }],
組件data和props
data() { return { submitData: {}, // 提交數(shù)據(jù)集合 dialogShow: false } }, props: { // 彈窗顯示/隱藏 dialogVisible: { type: Number, default: 0 }, // 彈窗Title title: String, // 自定義樣式 customClass: String, // 數(shù)據(jù)列 cols: { type: Array, default: () => [] }, // 編輯時(shí)傳入初始值 data: { type: Object, default: () => {} } },
組件數(shù)據(jù)的監(jiān)聽(tīng)
watch: { dialogVisible(val) { if (val > 0) { this.dialogShow = true } }, data: { handler(val) { this.submitData = val }, immediate: true }, submitData: { // 應(yīng)對(duì) 切換單選框隱藏其他元素的問(wèn)題 // 父組件監(jiān)聽(tīng)到單選框的值變化時(shí),修改cols的值,即可實(shí)現(xiàn)元素的隱藏與顯示 handler() { this.$emit('change', this.submitData) }, deep: true } }
數(shù)據(jù)提交以及驗(yàn)證
confirm() { // 驗(yàn)證必填項(xiàng) let isMust = this.cols.filter(item => item.isRequire).map(item => item.key) Object.keys(this.submitData).forEach(key => { let index = isMust.indexOf(key) if ((index > -1) && this.submitData[key] !== '' && !!this.submitData[key]) { isMust.splice(index, 1) } }) if (isMust.length > 0) { showDefaultTips('請(qǐng)注意必填項(xiàng)!', '', 3) return } this.$emit('complete', this.submitData) this.dialogShow = false }
到此這篇關(guān)于vue+elementui通用彈窗的實(shí)現(xiàn)(新增+編輯)的文章就介紹到這了,更多相關(guān)vue elementui 彈窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue加scoped后就無(wú)法修改vant的UI組件的樣式問(wèn)題
這篇文章主要介紹了解決vue加scoped后就無(wú)法修改vant的UI組件的樣式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09Vue解決移動(dòng)端彈窗滾動(dòng)穿透問(wèn)題
這篇文章主要介紹了Vue解決移動(dòng)端彈窗滾動(dòng)穿透問(wèn)題的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12基于vue-cli 路由 實(shí)現(xiàn)類(lèi)似tab切換效果(vue 2.0)
這篇文章主要介紹了基于vue-cli 路由 實(shí)現(xiàn)類(lèi)似tab切換效果(vue 2.0),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05vue+bpmn.js實(shí)現(xiàn)自定義流程圖的完整代碼
這篇文章主要介紹了vue+bpmn.js實(shí)現(xiàn)自定義流程圖的完整代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借價(jià)值,需要的朋友參考下吧2024-03-03使用vue.js2.0 + ElementUI開(kāi)發(fā)后臺(tái)管理系統(tǒng)詳細(xì)教程(二)
這篇文章主要介紹了使用vue.js2.0 + ElementUI開(kāi)發(fā)后臺(tái)管理系統(tǒng)詳細(xì)教程(二),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01vue使用window.open()跳轉(zhuǎn)頁(yè)面的代碼案例
這篇文章主要介紹了vue中對(duì)window.openner的使用,vue使用window.open()跳轉(zhuǎn)頁(yè)面的代碼案例,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11vant開(kāi)發(fā)微信小程序安裝以及簡(jiǎn)單使用教程
這篇文章主要介紹了vant開(kāi)發(fā)微信小程序安裝以及簡(jiǎn)單使用教程,需要的朋友可以參考下2022-12-12vue頁(yè)面中使用getElementsByClassName無(wú)法獲取元素的解決
這篇文章主要介紹了vue頁(yè)面中使用getElementsByClassName無(wú)法獲取元素的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03