details.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div>
  3. <el-dialog :title="exData.id ? '编辑': '新增'"
  4. :visible.sync="dialog"
  5. width="50%"
  6. :close-on-click-modal="false"
  7. top="50px">
  8. <el-form ref="form"
  9. :model="form"
  10. :rules="formRules"
  11. label-width="160px">
  12. <el-form-item prop="attach_name"
  13. :rules="formRules.required"
  14. label="规格名称:">
  15. <el-col :span="16">
  16. <el-input v-model="form.attach_name"
  17. placeholder="请输入规格名称"
  18. clearable></el-input>
  19. </el-col>
  20. </el-form-item>
  21. <el-form-item v-for="(option, index) in form.attach_content"
  22. :label="(index + 1).toString()"
  23. :key="index"
  24. :prop="'attach_content.' + index + '.name'"
  25. :rules="formRules.required">
  26. <el-col :span="16">
  27. <el-input v-model="option.name"
  28. placeholder="请输入附加内容"
  29. clearable></el-input>
  30. </el-col>
  31. <el-col :span="2" :offset="1">
  32. <el-button type="warning"
  33. :disabled="form.attach_content.length <= 1"
  34. @click.prevent="removeAttachContent(option)">删除
  35. </el-button>
  36. </el-col>
  37. </el-form-item>
  38. </el-form>
  39. <div slot="footer" class="dialog-footer text-center">
  40. <el-button @click="dialog = false">取 消</el-button>
  41. <el-button @click="addAttachContent">新增选项名</el-button>
  42. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  43. </div>
  44. </el-dialog>
  45. </div>
  46. </template>
  47. <script>
  48. export default {
  49. components: {},
  50. props: {
  51. value: {
  52. type: Boolean,
  53. default: true
  54. },
  55. exData: {
  56. type: Object,
  57. default: function () {
  58. return {}
  59. }
  60. }
  61. },
  62. data () {
  63. return {
  64. dialog: !!this.value,
  65. form: {
  66. attach_name: '', // 附加名称
  67. attach_content: [
  68. {
  69. name: ''
  70. }
  71. ]
  72. }
  73. }
  74. },
  75. methods: {
  76. removeAttachContent (item) {
  77. var index = this.form.attach_content.indexOf(item)
  78. if (index !== -1) {
  79. this.form.attach_content.splice(index, 1)
  80. }
  81. },
  82. addAttachContent () {
  83. this.form.attach_content.push({
  84. name: ''
  85. })
  86. },
  87. handleSubmit () {
  88. const url = this.exData.id ? '/v1/bar/product/attach/modify' : '/v1/bar/product/attach/add'
  89. this.$refs.form.validate(async valid => {
  90. if (valid) {
  91. const postData = {
  92. ...this.form,
  93. attach_content: this.form.attach_content.map(item => item.name).join(',')
  94. }
  95. const data = await this.$fetch(url, postData)
  96. if (data.code === 200) {
  97. this.$message.success('提交成功')
  98. this.$emit('success')
  99. this.dialog = false
  100. }
  101. }
  102. })
  103. }
  104. },
  105. mounted () {
  106. if (this.exData.id) {
  107. this.$set(this.form, 'id', this.exData.id)
  108. for (const key in this.exData) {
  109. if (this.form.hasOwnProperty(key)) {
  110. let value = this.exData[key]
  111. if ((Array.isArray(value) && value.length > 0) || value === 0 || value) {
  112. if (key === 'attach_content') {
  113. value = value.split(',').map(item => ({
  114. name: item
  115. }))
  116. }
  117. this.$set(this.form, key, value)
  118. }
  119. }
  120. }
  121. }
  122. },
  123. watch: {
  124. dialog (val) {
  125. if (!val) this.$emit('input', val)
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .top-tip {
  132. margin-top: -20px;
  133. margin-bottom: 20px;
  134. }
  135. </style>