JVxeTableMixin.js 5.3 KB

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