subForm.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import _, { method } from 'lodash'
  2. import { addClass, removeClass } from '../util'
  3. import { updateClassName } from '../util/reuse-methods'
  4. export const subFormMixin = {
  5. props: ['config', 'showControl', 'list', 'value', 'models', 'remote', 'blanks', 'disableddata',
  6. 'rules', 'name', 'remoteOption', 'preview', 'platform', 'dataSourceValue', 'eventFunction',
  7. 'widget', 'printRead', 'paging', 'pageSize', 'containerKey',
  8. 'isAdd', 'isDelete', 'isDialog', 'dialogName', 'group', 'fieldNode'],
  9. emits: ['update:value'],
  10. data () {
  11. return {
  12. subformData: [],
  13. displayFields: {},
  14. changeItem: {},
  15. pagingData: [],
  16. pagingLength: 0,
  17. currentPage: 1,
  18. itemModel: {},
  19. subHideFields: [],
  20. subDisabledFields: [],
  21. itemDisabled: {}
  22. }
  23. },
  24. created () {
  25. this.subformData = this.value ? this.value.map(item => {
  26. return {
  27. ...item,
  28. fm_key: item['fm_key'] || Math.random().toString(36).slice(-8)
  29. }
  30. }) : []
  31. this._generateModel(this.list)
  32. this.loadPagingData()
  33. this.subHideFields = this.subformData.map(v => Object.fromEntries(Object.keys(this.displayFields).map(field => [field, !this.displayFields[field]])))
  34. this.subDisabledFields = this.subformData.map(v => Object.fromEntries(Object.keys(this.itemDisabled).map(field => [field, !this.itemDisabled[field]])))
  35. },
  36. provide () {
  37. return {
  38. 'setSubformData': this.setSubformData
  39. }
  40. },
  41. inject: ['onFormDisabled', 'onFormHide', 'onFormDisplay'],
  42. methods: {
  43. handleMouseover (key) {
  44. addClass(this.$refs[`formSubformItem_${key}`][0], 'is-hover')
  45. },
  46. handleMouseout (key) {
  47. removeClass(this.$refs[`formSubformItem_${key}`][0], 'is-hover')
  48. },
  49. setSubformData (value, rowIndex, field) {
  50. const newData = _.cloneDeep(this.subformData)
  51. newData[rowIndex][field] = value
  52. this.subformData = newData
  53. },
  54. setData (rowIndex, value) {
  55. if (typeof rowIndex !== 'number') {
  56. return new Promise((resolve) => {
  57. value = rowIndex
  58. this.subformData.forEach((_, index) => {
  59. Object.keys(value).forEach(item => {
  60. this.setSubformData(value[item], index, item)
  61. })
  62. })
  63. resolve()
  64. })
  65. }
  66. return new Promise((resolve) => {
  67. this.$nextTick(() => {
  68. Object.keys(value).forEach(item => {
  69. this.setSubformData(value[item], rowIndex, item)
  70. })
  71. resolve()
  72. })
  73. })
  74. },
  75. handleAddRow () {
  76. this.subformData = [...this.subformData, {...this.itemModel, fm_key: Math.random().toString(36).slice(-8)}]
  77. this.subHideFields.push(
  78. Object.fromEntries(Object.keys(this.displayFields).map(field => [field, !this.displayFields[field]]))
  79. )
  80. this.subDisabledFields.push(
  81. Object.fromEntries(Object.keys(this.itemDisabled).map(field => [field, !this.itemDisabled[field]]))
  82. )
  83. if (this.widget && this.widget.events && this.widget.events.onRowAdd) {
  84. let funcKey = this.widget.events.onRowAdd
  85. this.eventFunction[funcKey]({
  86. rowIndex: this.subformData.length - 1,
  87. field: this.widget.model,
  88. currentRef: this,
  89. group: this.group,
  90. fieldNode: this.fieldNode
  91. })
  92. }
  93. if (this.paging) {
  94. this.$nextTick(() => {
  95. if (this.subformData.length > this.currentPage * this.pageSize) {
  96. this.currentPage = parseInt((this.subformData.length - 1) / this.pageSize) + 1
  97. }
  98. this.loadPagingData()
  99. })
  100. }
  101. },
  102. handleRemove (index) {
  103. const removeData = {...this.subformData[index]}
  104. const tmpdata = [...this.subformData]
  105. tmpdata.splice(index, 1)
  106. this.subformData = tmpdata
  107. this.subHideFields.splice(index, 1)
  108. this.subDisabledFields.splice(index, 1)
  109. if (this.widget && this.widget.events && this.widget.events.onRowRemove) {
  110. let funcKey = this.widget.events.onRowRemove
  111. this.eventFunction[funcKey]({
  112. removeIndex: index,
  113. removeData: removeData,
  114. field: this.widget.model,
  115. currentRef: this,
  116. group: this.group,
  117. fieldNode: this.fieldNode
  118. })
  119. }
  120. this.pagingData = []
  121. this.pagingLength = 0
  122. if (this.paging) {
  123. this.$nextTick(() => {
  124. if (this.subformData.length % this.pageSize == 0 && this.currentPage > parseInt(this.subformData.length / this.pageSize)) {
  125. this.currentPage = parseInt(this.subformData.length / this.pageSize)
  126. }
  127. this.loadPagingData()
  128. })
  129. }
  130. },
  131. hide (fields) {
  132. if (typeof fields === 'string') {
  133. fields = [fields]
  134. }
  135. fields.forEach(field => {
  136. this.onFormHide(`${this.fieldNode}.${field}`)
  137. })
  138. },
  139. display (fields) {
  140. if (typeof fields === 'string') {
  141. fields = [fields]
  142. }
  143. fields.forEach(field => {
  144. this.onFormDisplay(`${this.fieldNode}.${field}`)
  145. })
  146. },
  147. disabled (fields, disabled) {
  148. if (typeof fields === 'string') {
  149. fields = [fields]
  150. }
  151. this.subformData.forEach((_, index) => {
  152. fields.forEach(field => {
  153. this.subDisabledFields[index][field] = disabled
  154. })
  155. })
  156. this._setDisabled(this.list, fields, disabled)
  157. },
  158. hideChild (rowIndex, fields) {
  159. if (typeof fields === 'string') {
  160. fields = [fields]
  161. }
  162. fields.forEach(field => {
  163. this.onFormHide(`${this.fieldNode}.${rowIndex}.${field}`)
  164. })
  165. },
  166. displayChild (rowIndex, fields) {
  167. if (typeof fields === 'string') {
  168. fields = [fields]
  169. }
  170. fields.forEach(field => {
  171. this.onFormDisplay(`${this.fieldNode}.${rowIndex}.${field}`)
  172. })
  173. },
  174. disabledChild (rowIndex, fields, disabled) {
  175. if (typeof fields === 'string') {
  176. fields = [fields]
  177. }
  178. fields.forEach(field => {
  179. this.subDisabledFields[rowIndex][field] = disabled
  180. this.onFormDisabled(`${this.fieldNode}.${rowIndex}.${field}`, disabled)
  181. })
  182. },
  183. setOptions (fields, options) {
  184. if (typeof fields === 'string') {
  185. fields = [fields]
  186. }
  187. this._setOptions(this.list, fields, options)
  188. },
  189. addClassName (fields, className) {
  190. if (typeof fields === 'string') {
  191. fields = [fields]
  192. }
  193. fields.forEach(item => {
  194. updateClassName(this.list, item.split('.'), className, 'add')
  195. })
  196. },
  197. removeClassName (fields, className) {
  198. if (typeof fields === 'string') {
  199. fields = [fields]
  200. }
  201. fields.forEach(item => {
  202. updateClassName(this.list, item.split('.'), className, 'remove')
  203. })
  204. },
  205. handlePageChange (val) {
  206. this.currentPage = val
  207. this.pagingData = []
  208. this.pagingLength = 0
  209. this.$nextTick(() => {
  210. this.loadPagingData()
  211. })
  212. if (this.widget && this.widget.events && this.widget.events.onPageChange) {
  213. let funcKey = this.widget.events.onPageChange
  214. this.eventFunction[funcKey]({
  215. currentPage: val,
  216. field: this.widget.model,
  217. currentRef: this,
  218. group: this.group,
  219. fieldNode: this.fieldNode
  220. })
  221. }
  222. },
  223. loadPagingData () {
  224. let beginIndex = (this.currentPage - 1) * this.pageSize
  225. let endIndex = beginIndex + this.pageSize
  226. this.pagingData = this.subformData.slice(beginIndex, endIndex)
  227. this.pagingLength = this.pagingData.length
  228. },
  229. _generateModel (genList) {
  230. for (let i = 0; i < genList.length; i++) {
  231. if (genList[i].type == 'grid') {
  232. this.displayFields[genList[i].model] = !genList[i].options.hidden
  233. genList[i].columns.forEach(item => {
  234. this._generateModel(item.list)
  235. })
  236. } else if (genList[i].type === 'tabs') {
  237. genList[i].tabs.forEach(item => {
  238. this._generateModel(item.list)
  239. })
  240. this.displayFields[genList[i].model] = !genList[i].options.hidden
  241. } else if (genList[i].type === 'collapse') {
  242. genList[i].tabs.forEach(item => {
  243. this._generateModel(item.list)
  244. })
  245. this.displayFields[genList[i].model] = !genList[i].options.hidden
  246. } else if (genList[i].type === 'report') {
  247. genList[i].rows.forEach(row => {
  248. row.columns.forEach(column => {
  249. this._generateModel(column.list)
  250. })
  251. })
  252. this.displayFields[genList[i].model] = !genList[i].options.hidden
  253. } else if (genList[i].type === 'inline') {
  254. this._generateModel(genList[i].list)
  255. this.displayFields[genList[i].model] = !genList[i].options.hidden
  256. } else if (genList[i].type === 'card') {
  257. this._generateModel(genList[i].list)
  258. this.displayFields[genList[i].model] = !genList[i].options.hidden
  259. } else {
  260. if (genList[i].type == 'blank') {
  261. this.itemModel[genList[i].model] = genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : [])
  262. } else {
  263. this.itemModel[genList[i].model] = _.cloneDeep(genList[i].options.defaultValue)
  264. }
  265. this.displayFields[genList[i].model] = !genList[i].options.hidden
  266. this.itemDisabled[genList[i].model] = !genList[i].options.disabled
  267. }
  268. }
  269. },
  270. _setDisabled (genList, fields, disabled) {
  271. for (let i = 0; i < genList.length; i++) {
  272. if (genList[i].type === 'grid') {
  273. genList[i].columns.forEach(item => {
  274. this._setDisabled(item.list, fields, disabled)
  275. })
  276. } else if (genList[i].type === 'tabs') {
  277. genList[i].tabs.forEach(item => {
  278. this._setDisabled(item.list, fields, disabled)
  279. })
  280. } else if (genList[i].type === 'collapse') {
  281. genList[i].tabs.forEach(item => {
  282. this._setDisabled(item.list, fields, disabled)
  283. })
  284. } else if (genList[i].type === 'report') {
  285. genList[i].rows.forEach(row => {
  286. row.columns.forEach(column => {
  287. this._setDisabled(column.list, fields, disabled)
  288. })
  289. })
  290. } else if (genList[i].type === 'inline') {
  291. this._setDisabled(genList[i].list, fields, disabled)
  292. } else if (genList[i].type === 'card') {
  293. this._setDisabled(genList[i].list, fields, disabled)
  294. } else {
  295. if (fields.indexOf(genList[i].model) >= 0) {
  296. genList[i].options.disabled = disabled
  297. this.itemDisabled[genList[i].model] = !disabled
  298. }
  299. }
  300. }
  301. },
  302. _setOptions (genList, fields, opts) {
  303. for (let i = 0; i < genList.length; i++) {
  304. if (genList[i].type === 'grid') {
  305. genList[i].columns.forEach(item => {
  306. this._setOptions(item.list, fields, opts)
  307. })
  308. } else if (genList[i].type === 'tabs') {
  309. genList[i].tabs.forEach(item => {
  310. this._setOptions(item.list, fields, opts)
  311. })
  312. } else if (genList[i].type === 'collapse') {
  313. genList[i].tabs.forEach(item => {
  314. this._setOptions(item.list, fields, opts)
  315. })
  316. } else if (genList[i].type === 'report') {
  317. genList[i].rows.forEach(row => {
  318. row.columns.forEach(column => {
  319. this._setOptions(column.list, fields, opts)
  320. })
  321. })
  322. } else if (genList[i].type === 'inline') {
  323. this._setOptions(genList[i].list, fields, opts)
  324. } else if (genList[i].type === 'card') {
  325. this._setOptions(genList[i].list, fields, opts)
  326. }
  327. if (fields.indexOf(genList[i].model) >= 0) {
  328. Object.keys(opts).forEach(key => {
  329. genList[i].options[key] = opts[key]
  330. })
  331. }
  332. }
  333. },
  334. },
  335. watch: {
  336. value (val) {
  337. val.forEach(item => {
  338. if (!item['fm_key']) {
  339. item['fm_key'] = Math.random().toString(36).slice(-8)
  340. }
  341. })
  342. this.subformData = val
  343. let hideFields = []
  344. let disabledFields = []
  345. for (let i = 0; i < this.value.length; i++) {
  346. let rowArray = Object.keys(this.displayFields)
  347. let hideRow = {}
  348. for (let f = 0; f < rowArray.length; f++) {
  349. hideRow[rowArray[f]] = this.subHideFields?.[i]?.[rowArray[f]] ?? !this.displayFields[rowArray[f]]
  350. }
  351. hideFields.push(hideRow)
  352. let disabledArray = Object.keys(this.itemDisabled)
  353. let disabledRow = {}
  354. for (let d = 0; d < disabledArray.length; d++) {
  355. disabledRow[disabledArray[d]] = this.subDisabledFields?.[i]?.[disabledArray[d]] ?? !this.itemDisabled[disabledArray[d]]
  356. }
  357. disabledFields.push(disabledRow)
  358. }
  359. this.subHideFields = hideFields
  360. this.subDisabledFields = disabledFields
  361. },
  362. subformData: {
  363. deep: true,
  364. handler (val) {
  365. this.loadPagingData()
  366. this.$emit('update:value', val)
  367. }
  368. }
  369. }
  370. }