123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <template>
- <div>
- <el-dialog :title="exData.id ? '编辑': '新增'"
- :visible.sync="dialog"
- width="50%"
- :close-on-click-modal="false"
- top="50px">
- <el-form ref="form"
- :model="form"
- :rules="formRules"
- label-width="160px">
- <el-form-item prop="attach_name"
- :rules="formRules.required"
- label="规格名称:">
- <el-col :span="16">
- <el-input v-model="form.attach_name"
- placeholder="请输入规格名称"
- clearable></el-input>
- </el-col>
- </el-form-item>
- <el-form-item v-for="(option, index) in form.attach_content"
- :label="(index + 1).toString()"
- :key="index"
- :prop="'attach_content.' + index + '.name'"
- :rules="formRules.required">
- <el-col :span="16">
- <el-input v-model="option.name"
- placeholder="请输入附加内容"
- clearable></el-input>
- </el-col>
- <el-col :span="2" :offset="1">
- <el-button type="warning"
- :disabled="form.attach_content.length <= 1"
- @click.prevent="removeAttachContent(option)">删除
- </el-button>
- </el-col>
- </el-form-item>
- </el-form>
- <div slot="footer" class="dialog-footer text-center">
- <el-button @click="dialog = false">取 消</el-button>
- <el-button @click="addAttachContent">新增选项名</el-button>
- <el-button type="primary" @click="handleSubmit">确 定</el-button>
- </div>
- </el-dialog>
- </div>
- </template>
- <script>
- export default {
- components: {},
- props: {
- value: {
- type: Boolean,
- default: true
- },
- exData: {
- type: Object,
- default: function () {
- return {}
- }
- }
- },
- data () {
- return {
- dialog: !!this.value,
- form: {
- attach_name: '', // 附加名称
- attach_content: [
- {
- name: ''
- }
- ]
- }
- }
- },
- methods: {
- removeAttachContent (item) {
- var index = this.form.attach_content.indexOf(item)
- if (index !== -1) {
- this.form.attach_content.splice(index, 1)
- }
- },
- addAttachContent () {
- this.form.attach_content.push({
- name: ''
- })
- },
- handleSubmit () {
- const url = this.exData.id ? '/v1/bar/product/attach/modify' : '/v1/bar/product/attach/add'
- this.$refs.form.validate(async valid => {
- if (valid) {
- const postData = {
- ...this.form,
- attach_content: this.form.attach_content.map(item => item.name).join(',')
- }
- const data = await this.$fetch(url, postData)
- if (data.code === 200) {
- this.$message.success('提交成功')
- this.$emit('success')
- this.dialog = false
- }
- }
- })
- }
- },
- mounted () {
- if (this.exData.id) {
- this.$set(this.form, 'id', this.exData.id)
- for (const key in this.exData) {
- if (this.form.hasOwnProperty(key)) {
- let value = this.exData[key]
- if ((Array.isArray(value) && value.length > 0) || value === 0 || value) {
- if (key === 'attach_content') {
- value = value.split(',').map(item => ({
- name: item
- }))
- }
- this.$set(this.form, key, value)
- }
- }
- }
- }
- },
- watch: {
- dialog (val) {
- if (!val) this.$emit('input', val)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .top-tip {
- margin-top: -20px;
- margin-bottom: 20px;
- }
- </style>
|