index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import T from "ant-design-vue/es/table/Table";
  2. import get from "lodash.get"
  3. export default {
  4. data() {
  5. return {
  6. needTotalList: [],
  7. selectedRows: [],
  8. selectedRowKeys: [],
  9. localLoading: false,
  10. localDataSource: [],
  11. localPagination: Object.assign({}, T.props.pagination)
  12. };
  13. },
  14. props: Object.assign({}, T.props, {
  15. rowKey: {
  16. type: String,
  17. default: 'id'
  18. },
  19. data: {
  20. type: Function,
  21. required: true
  22. },
  23. pageNum: {
  24. type: Number,
  25. default: 1
  26. },
  27. pageSize: {
  28. type: Number,
  29. default: 10
  30. },
  31. showSizeChanger: {
  32. type: Boolean,
  33. default: true
  34. },
  35. showAlertInfo: {
  36. type: Boolean,
  37. default: false
  38. },
  39. showPagination: {
  40. default: 'auto'
  41. }
  42. }),
  43. watch: {
  44. 'localPagination.current'(val) {
  45. this.$router.push({
  46. name: this.$route.name,
  47. params: Object.assign({}, this.$route.params, {
  48. pageNo: val
  49. }),
  50. });
  51. },
  52. pageNum(val) {
  53. Object.assign(this.localPagination, {
  54. current: val
  55. });
  56. },
  57. pageSize(val) {
  58. Object.assign(this.localPagination, {
  59. pageSize: val
  60. });
  61. },
  62. showSizeChanger(val) {
  63. Object.assign(this.localPagination, {
  64. showSizeChanger: val
  65. });
  66. }
  67. },
  68. created() {
  69. this.localPagination = ['auto', true].includes(this.showPagination) && Object.assign({}, this.localPagination, {
  70. current: this.pageNum,
  71. pageSize: this.pageSize,
  72. showSizeChanger: this.showSizeChanger
  73. });
  74. this.needTotalList = this.initTotalList(this.columns)
  75. this.loadData();
  76. },
  77. methods: {
  78. refresh() {
  79. this.loadData();
  80. },
  81. loadData(pagination, filters, sorter) {
  82. this.localLoading = true
  83. var result = this.data(
  84. Object.assign({
  85. pageNo: (pagination && pagination.current) ||
  86. this.localPagination.current,
  87. pageSize: (pagination && pagination.pageSize) ||
  88. this.localPagination.pageSize
  89. },
  90. (sorter && sorter.field && {
  91. sortField: sorter.field
  92. }) || {},
  93. (sorter && sorter.order && {
  94. sortOrder: sorter.order
  95. }) || {}, {
  96. ...filters
  97. }
  98. )
  99. );
  100. if (result instanceof Promise) {
  101. result.then(r => {
  102. this.localPagination = Object.assign({}, this.localPagination, {
  103. current: r.pageNo, // 返回结果中的当前分页数
  104. total: r.totalCount, // 返回结果中的总记录数
  105. showSizeChanger: this.showSizeChanger,
  106. pageSize: (pagination && pagination.pageSize) ||
  107. this.localPagination.pageSize
  108. });
  109. !r.totalCount && ['auto', false].includes(this.showPagination) && (this.localPagination = false)
  110. console.log(this.localPagination);
  111. this.localDataSource = r.data; // 返回结果中的数组数据
  112. this.localLoading = false
  113. });
  114. }
  115. },
  116. initTotalList(columns) {
  117. const totalList = []
  118. columns && columns instanceof Array && columns.forEach(column => {
  119. if (column.needTotal) {
  120. totalList.push({ ...column,
  121. total: 0
  122. })
  123. }
  124. })
  125. return totalList
  126. },
  127. updateSelect(selectedRowKeys, selectedRows) {
  128. this.selectedRowKeys = selectedRowKeys
  129. this.selectedRows = selectedRows
  130. let list = this.needTotalList
  131. this.needTotalList = list.map(item => {
  132. return {
  133. ...item,
  134. total: selectedRows.reduce((sum, val) => {
  135. let total = sum + get(val, item.dataIndex)
  136. return isNaN(total) ? 0 : total
  137. }, 0)
  138. }
  139. })
  140. // this.$emit('change', selectedRowKeys, selectedRows)
  141. },
  142. updateEdit() {
  143. this.selectedRows = []
  144. },
  145. onClearSelected() {
  146. this.selectedRowKeys = []
  147. this.updateSelect([], [])
  148. },
  149. renderMsg(h) {
  150. const _vm = this
  151. let d = []
  152. // 构建 已选择
  153. d.push(
  154. h('span', {
  155. style: {
  156. marginRight: '12px'
  157. }
  158. }, ['已选择 ', h('a', {
  159. style: {
  160. fontWeight: 600
  161. }
  162. }, this.selectedRows.length)])
  163. );
  164. // 构建 列统计
  165. this.needTotalList.map(item => {
  166. d.push(h('span', {
  167. style: {
  168. marginRight: '12px'
  169. }
  170. },
  171. [
  172. `${ item.title }总计 `,
  173. h('a', {
  174. style: {
  175. fontWeight: 600
  176. }
  177. }, `${ !item.customRender ? item.total : item.customRender(item.total) }`)
  178. ]))
  179. });
  180. // 构建 清空选择
  181. d.push(h('a', {
  182. style: {
  183. marginLeft: '24px'
  184. },
  185. on: {
  186. click: _vm.onClearSelected
  187. }
  188. }, '清空'))
  189. return d
  190. },
  191. renderAlert(h) {
  192. return h('span', {
  193. slot: 'message'
  194. }, this.renderMsg(h))
  195. },
  196. },
  197. render(h) {
  198. const _vm = this
  199. let props = {},
  200. localKeys = Object.keys(this.$data);
  201. Object.keys(T.props).forEach(k => {
  202. let localKey = `local${k.substring(0,1).toUpperCase()}${k.substring(1)}`;
  203. if (localKeys.includes(localKey)) {
  204. return props[k] = _vm[localKey];
  205. }
  206. return props[k] = _vm[k];
  207. })
  208. // 显示信息提示
  209. if (this.showAlertInfo) {
  210. props.rowSelection = {
  211. selectedRowKeys: this.selectedRowKeys,
  212. onChange: (selectedRowKeys, selectedRows) => {
  213. _vm.updateSelect(selectedRowKeys, selectedRows)
  214. _vm.$emit('onSelect', { selectedRowKeys: selectedRowKeys, selectedRows: selectedRows })
  215. }
  216. };
  217. return h('div', {}, [
  218. h("a-alert", {
  219. style: {
  220. marginBottom: '16px'
  221. },
  222. props: {
  223. type: 'info',
  224. showIcon: true
  225. }
  226. }, [_vm.renderAlert(h)]),
  227. h("a-table", {
  228. tag: "component",
  229. attrs: props,
  230. on: {
  231. change: _vm.loadData
  232. },
  233. scopedSlots: this.$scopedSlots
  234. }, this.$slots.default)
  235. ]);
  236. }
  237. return h("a-table", {
  238. tag: "component",
  239. attrs: props,
  240. on: {
  241. change: _vm.loadData
  242. },
  243. scopedSlots: this.$scopedSlots
  244. }, this.$slots.default);
  245. }
  246. };