JEditableTableMixin.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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(
  53. '由于你没有在 data 中定义 addDefaultRowNum 或 addDefaultRowNum 不是数字,所以默认添加一条空数据,如果不想默认添加空数据,请将定义 addDefaultRowNum 为 0'
  54. )
  55. }
  56. this.eachAllTable((item) => {
  57. item.add(rowNum)
  58. })
  59. if (typeof this.addAfter === 'function') this.addAfter(this.model)
  60. this.edit({})
  61. },
  62. /** 当点击了编辑(修改)按钮时调用此方法 */
  63. edit(record) {
  64. if (typeof this.editBefore === 'function') this.editBefore(record)
  65. this.visible = true
  66. this.activeKey = this.refKeys[0]
  67. this.form.resetFields()
  68. this.model = Object.assign({}, record)
  69. if (typeof this.editAfter === 'function') this.editAfter(this.model)
  70. },
  71. /** 关闭弹窗,并将所有JEditableTable实例回归到初始状态 */
  72. close() {
  73. this.visible = false
  74. this.eachAllTable((item) => {
  75. item.initialize()
  76. })
  77. if(this.materialTable && this.materialTable.dataSource) {
  78. this.materialTable.dataSource = []
  79. }
  80. this.$emit('close')
  81. },
  82. /** 查询某个tab的数据 */
  83. requestSubTableData(url, params, tab, success) {
  84. tab.loading = true
  85. getAction(url, params)
  86. .then((res) => {
  87. if (res && res.code === 200) {
  88. tab.dataSource = res.data.rows
  89. typeof success === 'function' ? success(res) : ''
  90. }
  91. })
  92. .finally(() => {
  93. tab.loading = false
  94. })
  95. },
  96. /** 发起请求,自动判断是执行新增还是修改操作 */
  97. request(formData) {
  98. let url = this.url.add,
  99. method = 'post'
  100. if (this.model.id) {
  101. url = this.url.edit
  102. method = 'put'
  103. }
  104. this.confirmLoading = true
  105. httpAction(url, formData, method)
  106. .then((res) => {
  107. if (res.code === 200) {
  108. this.$emit('ok')
  109. this.confirmLoading = false
  110. this.close()
  111. } else {
  112. this.$message.warning(res.data.message)
  113. this.confirmLoading = false
  114. }
  115. })
  116. .finally(() => {})
  117. },
  118. /* --- handle 事件 --- */
  119. /** ATab 选项卡切换事件 */
  120. handleChangeTabs(key) {
  121. // 自动重置scrollTop状态,防止出现白屏
  122. getRefPromise(this, key).then((editableTable) => {
  123. editableTable.resetScrollTop()
  124. })
  125. },
  126. /** 关闭按钮点击事件 */
  127. handleCancel() {
  128. this.close()
  129. },
  130. /** 确定按钮点击事件 */
  131. handleOk() {
  132. /** 触发表单验证 */
  133. this.getAllTable()
  134. .then((tables) => {
  135. /** 一次性验证主表和所有的次表 */
  136. return validateFormAndTables(this.form, tables)
  137. })
  138. .then((allValues) => {
  139. if (typeof this.classifyIntoFormData !== 'function') {
  140. throw this.throwNotFunction('classifyIntoFormData')
  141. }
  142. let formData = this.classifyIntoFormData(allValues)
  143. // 发起请求
  144. return this.request(formData)
  145. })
  146. .catch((e) => {
  147. if (e.error === VALIDATE_NO_PASSED) {
  148. // 如果有未通过表单验证的子表,就自动跳转到它所在的tab
  149. this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
  150. } else {
  151. console.error(e)
  152. }
  153. })
  154. },
  155. /* --- throw --- */
  156. /** not a function */
  157. throwNotFunction(name) {
  158. return `${name} 未定义或不是一个函数`
  159. },
  160. /** not a array */
  161. throwNotArray(name) {
  162. return `${name} 未定义或不是一个数组`
  163. },
  164. },
  165. }