c17faf55edb3dc8823713713c21761c14b7bc8e6.svn-base 6.1 KB

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