index.js 11 KB

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