index.js 9.8 KB

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