editModal.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <a-modal
  3. centered
  4. class="productCategoryEdit-modal"
  5. :footer="null"
  6. :maskClosable="false"
  7. :title="modalTit"
  8. v-model="isShow"
  9. @cancle="isShow=false"
  10. :width="800">
  11. <!-- 表单 -->
  12. <div>
  13. <a-form-model
  14. id="productCategoryEdit-form"
  15. ref="ruleForm"
  16. :model="form"
  17. :rules="rules"
  18. :label-col="formItemLayout.labelCol"
  19. :wrapper-col="formItemLayout.wrapperCol"
  20. >
  21. <a-form-model-item label="所属分类" v-if="parentType">
  22. {{ parentType }}
  23. </a-form-model-item>
  24. <a-form-model-item label="产品分类名称" prop="productTypeName">
  25. <a-input
  26. v-model.trim="form.productTypeName"
  27. @change="filterEmpty"
  28. id="productCategoryEdit-name"
  29. :maxLength="30"
  30. allowClear
  31. placeholder="请输入产品分类名称(最多30个字符)" />
  32. </a-form-model-item>
  33. </a-form-model>
  34. <div class="btn-cont">
  35. <a-button type="primary" id="product-category-edit-modal-save" @click="handleSave">保存</a-button>
  36. <a-button id="product-category-edit-modal-back" @click="isShow = false" style="margin-left: 15px;">返回列表</a-button>
  37. </div>
  38. </div>
  39. </a-modal>
  40. </template>
  41. <script>
  42. import { STable } from '@/components'
  43. import { dealerProductTypeSave } from '@/api/dealerProductType'
  44. export default {
  45. name: 'ProductCategoryEditModal',
  46. components: { STable },
  47. props: {
  48. openModal: { // 弹框显示状态
  49. type: Boolean,
  50. default: false
  51. },
  52. itemId: {
  53. type: [Number, String],
  54. default: ''
  55. },
  56. nowData: {
  57. type: Object,
  58. default: null
  59. }
  60. },
  61. computed: {
  62. modalTit () {
  63. return (this.itemId ? '编辑' : '新增') + '产品分类'
  64. }
  65. },
  66. data () {
  67. return {
  68. isShow: this.openModal, // 是否打开弹框
  69. formItemLayout: {
  70. labelCol: { span: 6 },
  71. wrapperCol: { span: 16 }
  72. },
  73. form: {
  74. productTypeName: '' // 产品分类名称
  75. },
  76. rules: {
  77. productTypeName: [
  78. { required: true, message: '请输入产品分类名称', trigger: 'blur' }
  79. ]
  80. },
  81. parentType: '' // 父级分类
  82. }
  83. },
  84. methods: {
  85. filterEmpty () {
  86. let str = this.form.productTypeName
  87. str = str.replace(/\ +/g, '')
  88. str = str.replace(/[ ]/g, '')
  89. str = str.replace(/[\r\n]/g, '')
  90. this.form.productTypeName = str
  91. },
  92. // 保存
  93. handleSave () {
  94. const _this = this
  95. _this.$refs.ruleForm.validate(valid => {
  96. if (valid) {
  97. const formData = JSON.parse(JSON.stringify(_this.form))
  98. formData.id = _this.itemId ? _this.itemId : undefined
  99. formData.psn = _this.nowData && _this.nowData.psn ? _this.nowData.psn : undefined
  100. dealerProductTypeSave(formData).then(res => {
  101. if (res.status == 200) {
  102. _this.$message.success(res.message)
  103. _this.$emit('ok')
  104. _this.isShow = false
  105. }
  106. })
  107. } else {
  108. return false
  109. }
  110. })
  111. }
  112. },
  113. watch: {
  114. // 父页面传过来的弹框状态
  115. openModal (newValue, oldValue) {
  116. this.isShow = newValue
  117. },
  118. // 重定义的弹框状态
  119. isShow (newValue, oldValue) {
  120. if (!newValue) {
  121. this.$emit('close')
  122. this.$refs.ruleForm.resetFields()
  123. } else {
  124. // 获取父级分类 名称
  125. if (this.nowData && this.nowData.namePaths) {
  126. let namePathsArr = []
  127. namePathsArr = this.nowData.namePaths.split(',')
  128. if (this.itemId) { // 编辑 编辑时不显示当前分类
  129. namePathsArr = namePathsArr.slice(0, namePathsArr.length - 1)
  130. }
  131. this.parentType = ''
  132. namePathsArr.map((item, index) => {
  133. this.parentType += item + ' > '
  134. })
  135. this.parentType = this.parentType ? this.parentType.substr(0, this.parentType.length - 3) : ''
  136. } else {
  137. this.parentType = ''
  138. }
  139. }
  140. },
  141. itemId (newValue, oldValue) {
  142. if (this.isShow && newValue) {
  143. this.form.productTypeName = this.nowData && this.nowData.productTypeName ? this.nowData.productTypeName : ''
  144. } else {
  145. this.form.productTypeName = ''
  146. }
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="less">
  152. .productCategoryEdit-modal{
  153. .ant-modal-body {
  154. padding: 40px 40px 24px;
  155. }
  156. .btn-cont {
  157. text-align: center;
  158. margin: 35px 0 10px;
  159. }
  160. }
  161. </style>