index.js 8.8 KB

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