網(wǎng)站首頁 編程語言 正文
需求:動態(tài)增減表單組件
動態(tài)表單組件
模板部分
<el-form :rules="dynamicRules" ref="dynamicForm" label-width="140px" class="demo-dynamic" :inline="true" v-for="(item,index) in dynamicInfo" :key="index" :model="item" >
<el-form-item v-if="dynamicInfo.length > 1" class="removebutton">
<el-button size="small" type="danger" @click.prevent="remove(item, index)">移除</el-button>
</el-form-item>
<el-form-item prop="name" label="名稱">
<el-input maxlength="32" v-model="item.name" placeholder=""></el-input>
</el-form-item>
<el-form-item label="類別" prop="type">
<el-select placeholder="請選擇" v-model="item.type" size="small" clearable>
<el-option v-for="(item, index) in types" :key="index" :label="item.label" :value="item.value"></el-option>
</el-select>
</el-form-item>
</el-form>
JS部分
export default {
props: {
value: Array
},
data() {
return {
dynamicInfo: [{
name: '',
type: ''
}],
dynamicRules: {
name: [{ required: true, message: '請輸入名稱', trigger: 'blur' }],
type: [{ required: true, message: '請選擇類別', trigger: 'change' }]
}
}
},
mounted() {
if (this.value) {
this.dynamicInfo = this.value
}
},
methods: {
validateForm(formName) {
let returnValue
// 多個表單,所以驗(yàn)證時需要遍歷
this.$refs[formName].forEach(item => {
item.validate((valid) => {
if (valid) {
returnValue = true
} else {
returnValue = false
}
})
})
return returnValue
},
resetForm(formName) {
// 多個表單,所以驗(yàn)證時需要遍歷
this.$refs[formName].forEach(item => {
item.resetFields()
})
},
remove(item, index) {
if (this.dynamicInfo.length > 1) {
this.dynamicInfo.splice(index, 1)
}
},
add() {
this.dynamicInfo.push({
name: '',
type: ''
})
}
},
watch: {
value: {
handler(data) {
this.dynamicInfo = this.value
},
deep: true
},
dynamicInfo: {
handler(data) {
this.$emit('input', this.dynamicInfo)
},
deep: true
}
}
}
使用動態(tài)表單組件
<account-form ref="accountInfo" v-model="dynamicInfo" />
...
data() {
return {
dynamicInfo: []
}
}
...
原文鏈接:https://blog.csdn.net/qq_38157825/article/details/119773888
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-10-02 Flutter的鍵值存儲數(shù)據(jù)庫使用示例詳解_Android
- 2023-04-07 深度解讀Python如何實(shí)現(xiàn)dbscan算法_python
- 2022-06-10 C++?STL?中的數(shù)值算法示例講解_C 語言
- 2024-01-29 理解并使用 XPath 中的 `normalize-space` 函數(shù)
- 2023-01-23 redis實(shí)現(xiàn)多級緩存同步方案詳解_Redis
- 2022-06-09 詳解Python中*args和**kwargs的使用_python
- 2022-03-31 nginx平滑重啟和平滑升級的圖文教程_nginx
- 2022-10-29 CSS實(shí)現(xiàn)單行、多行文本溢出顯示省略號的實(shí)現(xiàn)方法
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支