index.js 10 KB

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