<el-dialog></el-dialog>用於點擊按鈕彈出對話框
其中的屬性 :title 用於判斷 不加:號的話是直接顯示內容
visible用於是否將頁面顯示出來;
點擊添加或者修改方法之后直接調用init()方法:
方法中先將表單內容清空,然后將窗口打開,拿到表單中的id值,根據id去后台拿到整個數據
在修改頁面將數據回顯在頁面
在表單提交方法中根據有無id去進行判斷該調用修改方法還是添加方法
成功失敗之后調用各自方法
<template>
<el-dialog
:title="!dataForm.id ? '新增' : '修改'"
:close-on-click-modal="false"
:visible.sync="visible">
<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<el-row>
<el-col :span="12">
<el-form-item label="常用語" prop="content">
<el-input v-model="dataForm.content" placeholder="姓名"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false">取消</el-button>
<el-button type="primary" @click="dataFormSubmit()">確定</el-button>
</span>
</el-dialog>
</template>
<script>
export default {
data () {
return {
dataForm: {
id: 0,
content: ''
},
visible: false,
dataRule: {
content: [
{ required: true, message: '內容不能為空', trigger: 'blur' }
]
}
}
},
methods: {
init (id) {
this.dataForm = {}
this.visible = true
this.dataForm.id = id
if (id) {
this.$http({
url: this.$http.adornUrl(`/cyy/apply/info/${this.dataForm.id}`),
method: 'get',
params: this.$http.adornParams()
}).then(({data}) => {
if (data && data.code === 0) {
this.dataForm = data.cyy
}
})
}
},
// 表單提交
dataFormSubmit () {
this.$refs['dataForm'].validate((valid) => {
if (valid) {
this.$http({
url: this.$http.adornUrl(`/cyy/apply/${this.dataForm.id ? 'updateCyy' : 'addCyy'}`),
method: 'post',
data: JSON.stringify(this.dataForm)
}).then(({data}) => {
if (data && data.code === 0) {
this.$message({
message: '操作成功',
type: 'success',
duration: 1500,
onClose: () => {
this.visible = false
this.$emit('refreshDataList')
}
})
} else {
this.$message.error(data.msg)
}
})
}
})
}
}
}
</script>