index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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({}, this.pagination),
  12. // 存储表格onchange时的filters, sorter对象
  13. filters: {},
  14. sorter: {}
  15. }
  16. },
  17. props: Object.assign({}, T.props, {
  18. rowKey: {
  19. type: [String, Function],
  20. default: 'key'
  21. },
  22. data: {
  23. type: Function,
  24. required: true
  25. },
  26. pageNum: {
  27. type: Number,
  28. default: 1
  29. },
  30. pageSize: {
  31. type: Number,
  32. default: 10
  33. },
  34. showSizeChanger: {
  35. type: Boolean,
  36. default: true
  37. },
  38. size: {
  39. type: String,
  40. default: 'default'
  41. },
  42. /**
  43. * alert: {
  44. * show: true,
  45. * clear: Function
  46. * }
  47. */
  48. alert: {
  49. type: [Object, Boolean],
  50. default: null
  51. },
  52. rowSelection: {
  53. type: Object,
  54. default: null
  55. },
  56. /** @Deprecated */
  57. showAlertInfo: {
  58. type: Boolean,
  59. default: false
  60. },
  61. showPagination: {
  62. type: String | Boolean,
  63. default: 'auto'
  64. },
  65. /**
  66. * enable page URI mode
  67. *
  68. * e.g:
  69. * /users/1
  70. * /users/2
  71. * /users/3?queryParam=test
  72. * ...
  73. */
  74. pageURI: {
  75. type: Boolean,
  76. default: false
  77. }
  78. }),
  79. watch: {
  80. 'localPagination.current' (val) {
  81. this.pageURI && this.$router.push({
  82. ...this.$route,
  83. name: this.$route.name,
  84. params: Object.assign({}, this.$route.params, {
  85. pageNo: val
  86. })
  87. })
  88. // change pagination, reset total data
  89. this.needTotalList = this.initTotalList(this.columns)
  90. this.selectedRowKeys = []
  91. this.selectedRows = []
  92. },
  93. pageNum (val) {
  94. Object.assign(this.localPagination, {
  95. current: val
  96. })
  97. },
  98. pageSize (val) {
  99. Object.assign(this.localPagination, {
  100. pageSize: val
  101. })
  102. },
  103. showSizeChanger (val) {
  104. Object.assign(this.localPagination, {
  105. showSizeChanger: val
  106. })
  107. }
  108. },
  109. created () {
  110. const { pageNo } = this.$route.params
  111. const localPageNum = this.pageURI && (pageNo && parseInt(pageNo)) || this.pageNum
  112. this.localPagination = ['auto', true].includes(this.showPagination) && Object.assign({}, this.localPagination, {
  113. current: localPageNum,
  114. pageSize: this.pageSize,
  115. showSizeChanger: this.showSizeChanger
  116. }) || false
  117. this.needTotalList = this.initTotalList(this.columns)
  118. this.loadData()
  119. },
  120. methods: {
  121. /**
  122. * 表格重新加载方法
  123. * 如果参数为 true, 则强制刷新到第一页
  124. * @param Boolean bool
  125. */
  126. refresh (bool = false) {
  127. bool && (this.localPagination = Object.assign({}, {
  128. current: 1, pageSize: this.pageSize
  129. }))
  130. this.loadData()
  131. },
  132. /**
  133. * 加载数据方法
  134. * @param {Object} pagination 分页选项器
  135. * @param {Object} filters 过滤条件
  136. * @param {Object} sorter 排序条件
  137. */
  138. loadData (pagination, filters = this.filters, sorter = this.sorter) {
  139. this.filters = filters
  140. this.sorter = sorter
  141. this.localLoading = true
  142. const parameter = Object.assign({
  143. pageNo: (pagination && pagination.current) ||
  144. this.showPagination && this.localPagination.current || this.pageNum,
  145. pageSize: (pagination && pagination.pageSize) ||
  146. this.showPagination && this.localPagination.pageSize || this.pageSize
  147. },
  148. (sorter && sorter.field && {
  149. sortField: sorter.field
  150. }) || {},
  151. (sorter && sorter.order && {
  152. sortOrder: sorter.order
  153. }) || {}, {
  154. ...filters
  155. }
  156. )
  157. const result = this.data(parameter)
  158. // 对接自己的通用数据接口需要修改下方代码中的 r.pageNo, r.totalCount, r.data
  159. // eslint-disable-next-line
  160. if ((typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function') {
  161. result.then(r => {
  162. this.localPagination = this.showPagination && Object.assign({}, this.localPagination, {
  163. current: r.pageNo, // 返回结果中的当前分页数
  164. total: r.totalCount, // 返回结果中的总记录数
  165. showSizeChanger: this.showSizeChanger,
  166. pageSize: (pagination && pagination.pageSize) ||
  167. this.localPagination.pageSize
  168. }) || false
  169. // 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
  170. if (r.data.length === 0 && this.showPagination && this.localPagination.current > 1) {
  171. this.localPagination.current--
  172. this.loadData()
  173. return
  174. }
  175. // 这里用于判断接口是否有返回 r.totalCount 且 this.showPagination = true 且 pageNo 和 pageSize 存在 且 totalCount 小于等于 pageNo * pageSize 的大小
  176. // 当情况满足时,表示数据不满足分页大小,关闭 table 分页功能
  177. try {
  178. if ((['auto', true].includes(this.showPagination) && r.totalCount <= (r.pageNo * this.localPagination.pageSize))) {
  179. this.localPagination.hideOnSinglePage = true
  180. }
  181. } catch (e) {
  182. this.localPagination = false
  183. }
  184. this.localDataSource = r.data // 返回结果中的数组数据
  185. })
  186. .finally(() => {
  187. this.localLoading = false
  188. })
  189. }
  190. },
  191. initTotalList (columns) {
  192. const totalList = []
  193. columns && columns instanceof Array && columns.forEach(column => {
  194. if (column.needTotal) {
  195. totalList.push({
  196. ...column,
  197. total: 0
  198. })
  199. }
  200. })
  201. return totalList
  202. },
  203. /**
  204. * 用于更新已选中的列表数据 total 统计
  205. * @param selectedRowKeys
  206. * @param selectedRows
  207. */
  208. updateSelect (selectedRowKeys, selectedRows) {
  209. this.selectedRows = selectedRows
  210. this.selectedRowKeys = selectedRowKeys
  211. const list = this.needTotalList
  212. this.needTotalList = list.map(item => {
  213. return {
  214. ...item,
  215. total: selectedRows.reduce((sum, val) => {
  216. const total = sum + parseInt(get(val, item.dataIndex))
  217. return isNaN(total) ? 0 : total
  218. }, 0)
  219. }
  220. })
  221. },
  222. /**
  223. * 清空 table 已选中项
  224. */
  225. clearSelected () {
  226. if (this.rowSelection) {
  227. this.rowSelection.onChange([], [])
  228. this.updateSelect([], [])
  229. }
  230. },
  231. /**
  232. * 处理交给 table 使用者去处理 clear 事件时,内部选中统计同时调用
  233. * @param callback
  234. * @returns {*}
  235. */
  236. renderClear (callback) {
  237. if (this.selectedRowKeys.length <= 0) return null
  238. return (
  239. <a style="margin-left: 24px" onClick={() => {
  240. callback()
  241. this.clearSelected()
  242. }}>清空</a>
  243. )
  244. },
  245. renderAlert () {
  246. // 绘制统计列数据
  247. const needTotalItems = this.needTotalList.map((item) => {
  248. return (<span style="margin-right: 12px">
  249. {item.title}总计 <a style="font-weight: 600">{!item.customRender ? item.total : item.customRender(item.total)}</a>
  250. </span>)
  251. })
  252. // 绘制 清空 按钮
  253. const clearItem = (typeof this.alert.clear === 'boolean' && this.alert.clear) ? (
  254. this.renderClear(this.clearSelected)
  255. ) : (this.alert !== null && typeof this.alert.clear === 'function') ? (
  256. this.renderClear(this.alert.clear)
  257. ) : null
  258. // 绘制 alert 组件
  259. return (
  260. <a-alert showIcon={true} style="margin-bottom: 16px">
  261. <template slot="message">
  262. <span style="margin-right: 12px">已选择: <a style="font-weight: 600">{this.selectedRows.length}</a></span>
  263. {needTotalItems}
  264. {clearItem}
  265. </template>
  266. </a-alert>
  267. )
  268. }
  269. },
  270. render () {
  271. const props = {}
  272. const localKeys = Object.keys(this.$data)
  273. const showAlert = (typeof this.alert === 'object' && this.alert !== null && this.alert.show) && typeof this.rowSelection.selectedRowKeys !== 'undefined' || this.alert
  274. Object.keys(T.props).forEach(k => {
  275. const localKey = `local${k.substring(0, 1).toUpperCase()}${k.substring(1)}`
  276. if (localKeys.includes(localKey)) {
  277. props[k] = this[localKey]
  278. return props[k]
  279. }
  280. if (k === 'rowSelection') {
  281. if (showAlert && this.rowSelection) {
  282. // 如果需要使用alert,则重新绑定 rowSelection 事件
  283. props[k] = {
  284. ...this.rowSelection,
  285. selectedRows: this.selectedRows,
  286. selectedRowKeys: this.selectedRowKeys,
  287. onChange: (selectedRowKeys, selectedRows) => {
  288. this.updateSelect(selectedRowKeys, selectedRows)
  289. typeof this[k].onChange !== 'undefined' && this[k].onChange(selectedRowKeys, selectedRows)
  290. }
  291. }
  292. return props[k]
  293. } else if (!this.rowSelection) {
  294. // 如果没打算开启 rowSelection 则清空默认的选择项
  295. props[k] = null
  296. return props[k]
  297. }
  298. }
  299. this[k] && (props[k] = this[k])
  300. return props[k]
  301. })
  302. const table = (
  303. <a-table {...{ props, scopedSlots: { ...this.$scopedSlots } }} onChange={this.loadData} onExpand={ (expanded, record) => { this.$emit('expand', expanded, record) } }>
  304. { Object.keys(this.$slots).map(name => (<template slot={name}>{this.$slots[name]}</template>)) }
  305. </a-table>
  306. )
  307. return (
  308. <div class="table-wrapper">
  309. { showAlert ? this.renderAlert() : null }
  310. { table }
  311. </div>
  312. )
  313. }
  314. }