CategroyEditForm.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <el-dialog
  3. width="500px"
  4. :title="title"
  5. class="bs-dialog-wrap dialogClass bs-el-dialog"
  6. :visible.sync="dialogFormVisible"
  7. :append-to-body="true"
  8. :before-close="handleClose"
  9. >
  10. <div style="margin: 20px;">
  11. <el-form
  12. ref="ruleForm"
  13. :model="dataForm"
  14. :rules="rules"
  15. label-position="left"
  16. label-width="90px"
  17. class="bs-el-form"
  18. >
  19. <el-form-item
  20. label="分组名称"
  21. prop="name"
  22. >
  23. <el-input
  24. v-model.trim="dataForm.name"
  25. class="bs-el-input"
  26. clearable
  27. />
  28. </el-form-item>
  29. </el-form>
  30. </div>
  31. <span
  32. slot="footer"
  33. class="dialog-footer"
  34. >
  35. <el-button
  36. class="bs-el-button-default"
  37. @click="cancel"
  38. >
  39. 取消
  40. </el-button>
  41. <el-button
  42. type="primary"
  43. @click="submitForm('ruleForm')"
  44. >
  45. 确定
  46. </el-button>
  47. </span>
  48. </el-dialog>
  49. </template>
  50. <script>
  51. import { categoryAdd, categoryUpdate } from 'data-room-ui/js/utils/datasetConfigService'
  52. import { categoryNameRepeat } from "../../js/utils/datasetConfigService";
  53. export default {
  54. name: 'CategroyEditForm',
  55. props: {
  56. appCode: {
  57. type: String,
  58. default: ''
  59. }
  60. },
  61. data () {
  62. const nameRepeatCheck = (rule, value, callback) => {
  63. let parentId = ''
  64. if (this.nodeFlag) {
  65. // 新增节点
  66. if (this.radio === 0) {
  67. // 新增同级
  68. parentId = this.nodeData.parentId
  69. } else {
  70. // 新增子级
  71. parentId = this.nodeData.id
  72. }
  73. }
  74. categoryNameRepeat({
  75. ...this.dataForm,
  76. parentId
  77. }).then(res => {
  78. if (res) {
  79. callback(new Error('分组名称已存在'))
  80. } else {
  81. callback()
  82. }
  83. })
  84. }
  85. return {
  86. type: 'dataset',
  87. dataForm: {
  88. id: '',
  89. name: '',
  90. parentId: ''
  91. },
  92. title: '',
  93. dialogFormVisible: false,
  94. radio: 0,
  95. nodeFlag: false,
  96. rules: {
  97. name: [
  98. { required: true, message: '分组名称不能为空', trigger: 'blur' },
  99. { validator: nameRepeatCheck, trigger: 'blur' }
  100. ]
  101. },
  102. nodeData: {}
  103. }
  104. },
  105. watch: {
  106. dialogFormVisible (val) {
  107. if (!val) {
  108. this.dataForm = {
  109. id: '',
  110. name: '',
  111. parentId: ''
  112. }
  113. }
  114. }
  115. },
  116. methods: {
  117. init (row, flag) {
  118. this.nodeFlag = flag
  119. this.nodeData = row
  120. if (!flag) {
  121. this.dataForm.name = row.name
  122. this.dataForm.id = row.id
  123. }
  124. },
  125. cancel () {
  126. this.dialogFormVisible = false
  127. this.$nextTick(() => {
  128. this.$parent.addOrUpdateTreeVisible = false
  129. })
  130. },
  131. handleClose () {
  132. this.dialogFormVisible = false
  133. this.$parent.addOrUpdateTreeVisible = false
  134. },
  135. submitForm (formName) {
  136. this.$refs[formName].validate((valid) => {
  137. if (valid) {
  138. let id = ''
  139. let parentId = ''
  140. if (this.nodeFlag) {
  141. // 新增节点
  142. if (this.radio === 0) {
  143. // 新增同级
  144. parentId = this.nodeData.parentId
  145. } else {
  146. // 新增子级
  147. parentId = this.nodeData.id
  148. }
  149. } else {
  150. // 修改节点
  151. id = this.dataForm.id
  152. parentId = this.nodeData.parentId
  153. }
  154. const params = {
  155. id: id,
  156. name: this.dataForm.name,
  157. parentId: parentId,
  158. type: this.type,
  159. moduleCode: this.appCode
  160. }
  161. if (id) {
  162. categoryUpdate(params).then((r) => {
  163. params.id = r
  164. this.$message.success('保存成功')
  165. this.cancel()
  166. try {
  167. this.$emit('addOrUpdateNode', params, this.nodeFlag)
  168. } catch (error) {
  169. this.$parent.initLazyDatasetTypeTree()
  170. }
  171. })
  172. } else {
  173. categoryAdd(params).then((r) => {
  174. params.id = r
  175. this.$message.success('保存成功')
  176. this.cancel()
  177. try {
  178. this.$emit('addOrUpdateNode', params, this.nodeFlag)
  179. } catch (error) {
  180. this.$parent.initLazyDatasetTypeTree()
  181. }
  182. })
  183. }
  184. } else {
  185. return false
  186. }
  187. })
  188. }
  189. }
  190. }
  191. </script>