editModal.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. <!-- 表单 -->
  12. <div>
  13. <a-spin :spinning="spinning" tip="Loading...">
  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="30"
  31. allowClear
  32. placeholder="请输入产品分类名称(最多30个字符)" />
  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. </a-spin>
  40. </div>
  41. </a-modal>
  42. </template>
  43. <script>
  44. import { commonMixin } from '@/utils/mixin'
  45. import { STable } from '@/components'
  46. import { productTypeSave } from '@/api/productType'
  47. export default {
  48. name: 'ProductCategoryEditModal',
  49. mixins: [commonMixin],
  50. components: { STable },
  51. props: {
  52. openModal: { // 弹框显示状态
  53. type: Boolean,
  54. default: false
  55. },
  56. itemSn: {
  57. type: [Number, String],
  58. default: ''
  59. },
  60. nowData: {
  61. type: Object,
  62. default: null
  63. }
  64. },
  65. computed: {
  66. modalTit () {
  67. return (this.itemSn ? '编辑' : '新增') + '产品分类'
  68. }
  69. },
  70. data () {
  71. return {
  72. isShow: this.openModal, // 是否打开弹框
  73. spinning: false,
  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. filterEmpty () {
  91. let str = this.form.productTypeName
  92. str = str.replace(/\ +/g, '')
  93. str = str.replace(/[ ]/g, '')
  94. str = str.replace(/[\r\n]/g, '')
  95. this.form.productTypeName = str
  96. },
  97. // 保存
  98. handleSave () {
  99. const _this = this
  100. _this.$refs.ruleForm.validate(valid => {
  101. if (valid) {
  102. const formData = JSON.parse(JSON.stringify(_this.form))
  103. if (_this.itemSn) { // 编辑
  104. formData.productTypeSn = _this.nowData && _this.nowData.productTypeSn ? _this.nowData.productTypeSn : undefined
  105. } else { // 新增子级
  106. formData.parentSn = _this.nowData && _this.nowData.productTypeSn ? _this.nowData.productTypeSn : undefined
  107. }
  108. _this.spinning = true
  109. productTypeSave(formData).then(res => {
  110. if (res.status == 200) {
  111. _this.$message.success(res.message)
  112. _this.$emit('ok')
  113. _this.isShow = false
  114. _this.spinning = false
  115. } else {
  116. _this.spinning = false
  117. }
  118. })
  119. } else {
  120. return false
  121. }
  122. })
  123. }
  124. },
  125. watch: {
  126. // 父页面传过来的弹框状态
  127. openModal (newValue, oldValue) {
  128. this.isShow = newValue
  129. },
  130. // 重定义的弹框状态
  131. isShow (newValue, oldValue) {
  132. if (!newValue) {
  133. this.$emit('close')
  134. this.$refs.ruleForm.resetFields()
  135. } else {
  136. // 获取父级分类 名称
  137. if (this.nowData && this.nowData.namePaths) {
  138. let namePathsArr = []
  139. namePathsArr = this.nowData.namePaths.split(',')
  140. if (this.itemSn) { // 编辑 编辑时不显示当前分类
  141. namePathsArr = namePathsArr.slice(0, namePathsArr.length - 1)
  142. }
  143. this.parentType = ''
  144. namePathsArr.map((item, index) => {
  145. this.parentType += item + ' > '
  146. })
  147. this.parentType = this.parentType ? this.parentType.substr(0, this.parentType.length - 3) : ''
  148. } else {
  149. this.parentType = ''
  150. }
  151. }
  152. },
  153. itemSn (newValue, oldValue) {
  154. if (this.isShow && newValue) {
  155. this.form.productTypeName = this.nowData && this.nowData.productTypeName ? this.nowData.productTypeName : ''
  156. } else {
  157. this.form.productTypeName = ''
  158. }
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="less">
  164. .productCategoryEdit-modal{
  165. .ant-modal-body {
  166. padding: 40px 40px 24px;
  167. }
  168. .btn-cont {
  169. text-align: center;
  170. margin: 35px 0 10px;
  171. }
  172. }
  173. </style>