You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

171 lines
5.0 KiB

  1. import JEditableTable from '@/components/jeecg/JEditableTable'
  2. import { VALIDATE_NO_PASSED, getRefPromise, validateFormAndTables } from '@/utils/JEditableTableUtil'
  3. import { httpAction, getAction } from '@/api/manage'
  4. export const JEditableTableMixin = {
  5. components: {
  6. JEditableTable
  7. },
  8. data() {
  9. return {
  10. title: '操作',
  11. visible: false,
  12. form: this.$form.createForm(this),
  13. confirmLoading: false,
  14. model: {},
  15. labelCol: {
  16. xs: { span: 24 },
  17. sm: { span: 6 }
  18. },
  19. wrapperCol: {
  20. xs: { span: 24 },
  21. sm: { span: 18 }
  22. }
  23. }
  24. },
  25. methods: {
  26. /** 获取所有的editableTable实例 */
  27. getAllTable() {
  28. if (!(this.refKeys instanceof Array)) {
  29. throw this.throwNotArray('refKeys')
  30. }
  31. let values = this.refKeys.map(key => getRefPromise(this, key))
  32. return Promise.all(values)
  33. },
  34. /** 遍历所有的JEditableTable实例 */
  35. eachAllTable(callback) {
  36. // 开始遍历
  37. this.getAllTable().then(tables => {
  38. tables.forEach((item, index) => {
  39. if (typeof callback === 'function') {
  40. callback(item, index)
  41. }
  42. })
  43. })
  44. },
  45. /** 当点击新增按钮时调用此方法 */
  46. add() {
  47. if (typeof this.addBefore === 'function') this.addBefore()
  48. // 默认新增空数据
  49. let rowNum = this.addDefaultRowNum
  50. if (typeof rowNum !== 'number') {
  51. rowNum = 1
  52. console.warn('由于你没有在 data 中定义 addDefaultRowNum 或 addDefaultRowNum 不是数字,所以默认添加一条空数据,如果不想默认添加空数据,请将定义 addDefaultRowNum 为 0')
  53. }
  54. this.eachAllTable((item) => {
  55. item.add(rowNum)
  56. })
  57. if (typeof this.addAfter === 'function') this.addAfter(this.model)
  58. this.edit({})
  59. },
  60. /** 当点击了编辑(修改)按钮时调用此方法 */
  61. edit(record) {
  62. if (typeof this.editBefore === 'function') this.editBefore(record)
  63. this.visible = true
  64. this.activeKey = this.refKeys[0]
  65. this.form.resetFields()
  66. this.model = Object.assign({}, record)
  67. if (typeof this.editAfter === 'function') this.editAfter(this.model)
  68. },
  69. /** 关闭弹窗,并将所有JEditableTable实例回归到初始状态 */
  70. close() {
  71. this.visible = false
  72. this.eachAllTable((item) => {
  73. item.initialize()
  74. })
  75. this.$emit('close')
  76. },
  77. /** 查询某个tab的数据 */
  78. requestSubTableData(url, params, tab, success) {
  79. tab.loading = true
  80. getAction(url, params).then(res => {
  81. let { result } = res
  82. let dataSource = []
  83. if (result) {
  84. if (Array.isArray(result)) {
  85. dataSource = result
  86. } else if (Array.isArray(result.records)) {
  87. dataSource = result.records
  88. }
  89. }
  90. tab.dataSource = dataSource
  91. typeof success === 'function' ? success(res) : ''
  92. }).finally(() => {
  93. tab.loading = false
  94. })
  95. },
  96. /** 发起请求,自动判断是执行新增还是修改操作 */
  97. request(formData) {
  98. let url = this.url.add, method = 'post'
  99. if (this.model.id) {
  100. url = this.url.edit
  101. method = 'put'
  102. }
  103. this.confirmLoading = true
  104. httpAction(url, formData, method).then((res) => {
  105. if (res.success) {
  106. this.$message.success(res.message)
  107. this.$emit('ok')
  108. this.close()
  109. } else {
  110. this.$message.warning(res.message)
  111. }
  112. }).finally(() => {
  113. this.confirmLoading = false
  114. })
  115. },
  116. /* --- handle 事件 --- */
  117. /** ATab 选项卡切换事件 */
  118. handleChangeTabs(key) {
  119. // 自动重置scrollTop状态,防止出现白屏
  120. getRefPromise(this, key).then(editableTable => {
  121. editableTable.resetScrollTop()
  122. })
  123. },
  124. /** 关闭按钮点击事件 */
  125. handleCancel() {
  126. this.close()
  127. },
  128. /** 确定按钮点击事件 */
  129. handleOk() {
  130. /** 触发表单验证 */
  131. this.getAllTable().then(tables => {
  132. /** 一次性验证主表和所有的次表 */
  133. return validateFormAndTables(this.form, tables)
  134. }).then(allValues => {
  135. if (typeof this.classifyIntoFormData !== 'function') {
  136. throw this.throwNotFunction('classifyIntoFormData')
  137. }
  138. let formData = this.classifyIntoFormData(allValues)
  139. // 发起请求
  140. return this.request(formData)
  141. }).catch(e => {
  142. if (e.error === VALIDATE_NO_PASSED) {
  143. // 如果有未通过表单验证的子表,就自动跳转到它所在的tab
  144. this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
  145. } else {
  146. console.error(e)
  147. }
  148. })
  149. },
  150. /* --- throw --- */
  151. /** not a function */
  152. throwNotFunction(name) {
  153. return `${name} 未定义或不是一个函数`
  154. },
  155. /** not a array */
  156. throwNotArray(name) {
  157. return `${name} 未定义或不是一个数组`
  158. }
  159. }
  160. }