editModal.vue 4.9 KB

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