index.js 9.4 KB

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