JEditableTableMixin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. tab.dataSource = res.result || []
  82. typeof success === 'function' ? success(res) : ''
  83. }).finally(() => {
  84. tab.loading = false
  85. })
  86. },
  87. /** 发起请求,自动判断是执行新增还是修改操作 */
  88. request(formData) {
  89. let url = this.url.add, method = 'post'
  90. if (this.model.id) {
  91. url = this.url.edit
  92. method = 'put'
  93. }
  94. this.confirmLoading = true
  95. httpAction(url, formData, method).then((res) => {
  96. if (res.success) {
  97. this.$message.success(res.message)
  98. this.$emit('ok')
  99. this.close()
  100. } else {
  101. this.$message.warning(res.message)
  102. }
  103. }).finally(() => {
  104. this.confirmLoading = false
  105. })
  106. },
  107. /* --- handle 事件 --- */
  108. /** ATab 选项卡切换事件 */
  109. handleChangeTabs(key) {
  110. // 自动重置scrollTop状态,防止出现白屏
  111. getRefPromise(this, key).then(editableTable => {
  112. editableTable.resetScrollTop()
  113. })
  114. },
  115. /** 关闭按钮点击事件 */
  116. handleCancel() {
  117. this.close()
  118. },
  119. /** 确定按钮点击事件 */
  120. handleOk() {
  121. /** 触发表单验证 */
  122. this.getAllTable().then(tables => {
  123. /** 一次性验证主表和所有的次表 */
  124. return validateFormAndTables(this.form, tables)
  125. }).then(allValues => {
  126. if (typeof this.classifyIntoFormData !== 'function') {
  127. throw this.throwNotFunction('classifyIntoFormData')
  128. }
  129. let formData = this.classifyIntoFormData(allValues)
  130. // 发起请求
  131. return this.request(formData)
  132. }).catch(e => {
  133. if (e.error === VALIDATE_NO_PASSED) {
  134. // 如果有未通过表单验证的子表,就自动跳转到它所在的tab
  135. this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
  136. } else {
  137. console.error(e)
  138. }
  139. })
  140. },
  141. /* --- throw --- */
  142. /** not a function */
  143. throwNotFunction(name) {
  144. return `${name} 未定义或不是一个函数`
  145. },
  146. /** not a array */
  147. throwNotArray(name) {
  148. return `${name} 未定义或不是一个数组`
  149. }
  150. }
  151. }