056e2acaa77d91fb44794ee2a42045a51f2d0944.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <a-table
  3. :rowKey="rowKey"
  4. :columns="columns"
  5. :dataSource="dataSource"
  6. :expandedRowKeys="expandedRowKeys"
  7. v-bind="tableAttrs"
  8. v-on="$listeners"
  9. @expand="handleExpand"
  10. @expandedRowsChange="expandedRowKeys=$event">
  11. <template v-for="(slotItem) of slots" :slot="slotItem" slot-scope="text, record, index">
  12. <slot :name="slotItem" v-bind="{text,record,index}"></slot>
  13. </template>
  14. </a-table>
  15. </template>
  16. <script>
  17. import { getAction } from '@/api/manage'
  18. export default {
  19. name: 'JTreeTable',
  20. props: {
  21. rowKey: {
  22. type: String,
  23. default: 'id'
  24. },
  25. // 根据什么查询,如果传递 id 就根据 id 查询
  26. queryKey: {
  27. type: String,
  28. default: 'parentId'
  29. },
  30. queryParams: {
  31. type: Object,
  32. default: () => ({})
  33. },
  34. // 查询顶级时的值,如果顶级为0,则传0
  35. topValue: {
  36. type: String,
  37. default: null
  38. },
  39. columns: {
  40. type: Array,
  41. required: true
  42. },
  43. url: {
  44. type: String,
  45. required: true
  46. },
  47. childrenUrl: {
  48. type: String,
  49. default: null
  50. },
  51. tableProps: {
  52. type: Object,
  53. default: () => ({})
  54. },
  55. /** 是否在创建组件的时候就查询数据 */
  56. immediateRequest: {
  57. type: Boolean,
  58. default: true
  59. },
  60. condition:{
  61. type:String,
  62. default:'',
  63. required:false
  64. }
  65. },
  66. data() {
  67. return {
  68. dataSource: [],
  69. expandedRowKeys: []
  70. }
  71. },
  72. computed: {
  73. getChildrenUrl() {
  74. if (this.childrenUrl) {
  75. return this.childrenUrl
  76. } else {
  77. return this.url
  78. }
  79. },
  80. slots() {
  81. let slots = []
  82. for (let column of this.columns) {
  83. if (column.scopedSlots && column.scopedSlots.customRender) {
  84. slots.push(column.scopedSlots.customRender)
  85. }
  86. }
  87. return slots
  88. },
  89. tableAttrs() {
  90. return Object.assign(this.$attrs, this.tableProps)
  91. }
  92. },
  93. watch: {
  94. queryParams: {
  95. deep: true,
  96. handler() {
  97. this.loadData()
  98. }
  99. }
  100. },
  101. created() {
  102. if (this.immediateRequest) this.loadData()
  103. },
  104. methods: {
  105. /** 加载数据*/
  106. loadData(id = this.topValue, first = true, url = this.url) {
  107. this.$emit('requestBefore', { first })
  108. if (first) {
  109. this.expandedRowKeys = []
  110. }
  111. let params = Object.assign({}, this.queryParams || {})
  112. params[this.queryKey] = id
  113. if(this.condition && this.condition.length>0){
  114. params['condition'] = this.condition
  115. }
  116. return getAction(url, params).then(res => {
  117. let list = []
  118. if (res.result instanceof Array) {
  119. list = res.result
  120. } else if (res.result.records instanceof Array) {
  121. list = res.result.records
  122. } else {
  123. throw '返回数据类型不识别'
  124. }
  125. let dataSource = list.map(item => {
  126. // 判断是否标记了带有子级
  127. if (item.hasChildren === true) {
  128. // 查找第一个带有dataIndex的值的列
  129. let firstColumn
  130. for (let column of this.columns) {
  131. firstColumn = column.dataIndex
  132. if (firstColumn) break
  133. }
  134. // 定义默认展开时显示的loading子级,实际子级数据只在展开时加载
  135. let loadChild = { id: `${item.id}_loadChild`, [firstColumn]: 'loading...', isLoading: true }
  136. item.children = [loadChild]
  137. }
  138. return item
  139. })
  140. if (first) {
  141. this.dataSource = dataSource
  142. }
  143. this.$emit('requestSuccess', { first, dataSource, res })
  144. return Promise.resolve(dataSource)
  145. }).finally(() => this.$emit('requestFinally', { first }))
  146. },
  147. /** 点击展开图标时触发 */
  148. handleExpand(expanded, record) {
  149. // 判断是否是展开状态
  150. if (expanded) {
  151. // 判断子级的首个项的标记是否是“正在加载中”,如果是就加载数据
  152. if (record.children[0].isLoading === true) {
  153. this.loadData(record.id, false, this.getChildrenUrl).then(dataSource => {
  154. // 处理好的数据可直接赋值给children
  155. if (dataSource.length === 0) {
  156. record.children = null
  157. } else {
  158. record.children = dataSource
  159. }
  160. })
  161. }
  162. }
  163. }
  164. }
  165. }
  166. </script>
  167. <style scoped>
  168. </style>