index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. checkCustomMap: {},
  10. localLoading: false,
  11. localDataSource: [],
  12. // localPagination: Object.assign({}, this.pagination)
  13. localPagination: Object.assign({}, {
  14. ...this.pagination,
  15. showTotal: total => `共 ${total} 条数据`,
  16. showSizeChanger: true,
  17. pageSizeOptions: ['10', '20', '50']
  18. })
  19. }
  20. },
  21. props: Object.assign({}, T.props, {
  22. widthSpace: {
  23. type: Boolean,
  24. default: false
  25. },
  26. rowKey: {
  27. type: [String, Function],
  28. default: 'key'
  29. },
  30. data: {
  31. type: Function,
  32. required: true
  33. },
  34. pageNum: {
  35. type: Number,
  36. default: 1
  37. },
  38. pageSize: {
  39. type: Number,
  40. default: 10
  41. },
  42. showSizeChanger: {
  43. type: Boolean,
  44. default: true
  45. },
  46. size: {
  47. type: String,
  48. default: 'default'
  49. },
  50. /**
  51. * alert: {
  52. * show: true,
  53. * clear: Function
  54. * }
  55. */
  56. alert: {
  57. type: [Object, Boolean],
  58. default: null
  59. },
  60. rowSelection: {
  61. type: Object,
  62. default: null
  63. },
  64. /** @Deprecated */
  65. showAlertInfo: {
  66. type: Boolean,
  67. default: false
  68. },
  69. showPagination: {
  70. type: String | Boolean,
  71. default: 'auto'
  72. },
  73. /**
  74. * enable page URI mode
  75. *
  76. * e.g:
  77. * /users/1
  78. * /users/2
  79. * /users/3?queryParam=test
  80. * ...
  81. */
  82. pageURI: {
  83. type: Boolean,
  84. default: false
  85. }
  86. }),
  87. watch: {
  88. columns (val, oldVal) { // 深度监听,可监听到对象、数组的变化
  89. let isChecked = 0
  90. val.forEach((item, index) => {
  91. if (item['checked'] == null) {
  92. item['checked'] = false
  93. }
  94. if (item['checked']) {
  95. isChecked++
  96. }
  97. this.checkCustomMap[item.title] = item
  98. })
  99. if (isChecked === 0) {
  100. val.forEach((item, index) => {
  101. if (index < 8) {
  102. item['checked'] = true
  103. this.checkCustomMap[item.title] = item
  104. }
  105. if (item.title === '操作') {
  106. item['checked'] = true
  107. this.checkCustomMap[item.title] = item
  108. }
  109. })
  110. }
  111. },
  112. widthSpace (val) {
  113. Object.assign(this.localPagination, {
  114. widthSpace: val
  115. })
  116. },
  117. 'localPagination.current' (val) {
  118. this.pageURI && this.$router.push({
  119. ...this.$route,
  120. name: this.$route.name,
  121. params: Object.assign({}, this.$route.params, {
  122. pageNum: val
  123. })
  124. })
  125. },
  126. pageNum (val) {
  127. Object.assign(this.localPagination, {
  128. current: val
  129. })
  130. },
  131. pageSize (val) {
  132. Object.assign(this.localPagination, {
  133. pageSize: val
  134. })
  135. },
  136. showSizeChanger (val) {
  137. Object.assign(this.localPagination, {
  138. showSizeChanger: val
  139. })
  140. }
  141. },
  142. created () {
  143. const { pageNum } = this.$route.params
  144. const localPageNum = this.pageURI && (pageNum && parseInt(pageNum)) || this.pageNum
  145. this.localPagination = ['auto', true].includes(this.showPagination) && Object.assign({}, this.localPagination, {
  146. current: localPageNum,
  147. pageSize: this.pageSize,
  148. showSizeChanger: this.showSizeChanger
  149. }) || false
  150. this.needTotalList = this.initTotalList(this.columns)
  151. let isChecked = 0
  152. this.columns.forEach((item, index) => {
  153. if (item['checked'] == null) {
  154. item['checked'] = false
  155. }
  156. if (item['checked']) {
  157. isChecked++
  158. }
  159. this.checkCustomMap[item.title] = item
  160. })
  161. if (isChecked === 0) {
  162. this.columns.forEach((item, index) => {
  163. if (index < 8) {
  164. item['checked'] = true
  165. this.checkCustomMap[item.title] = item
  166. }
  167. if (item.title === '操作') {
  168. item['checked'] = true
  169. this.checkCustomMap[item.title] = item
  170. }
  171. })
  172. }
  173. this.loadData()
  174. },
  175. methods: {
  176. /**
  177. * 表格重新加载方法
  178. * 如果参数为 true, 则强制刷新到第一页
  179. * @param Boolean bool
  180. */
  181. refresh (bool = false) {
  182. bool && (this.localPagination = Object.assign({}, {
  183. current: 1, pageSize: this.pageSize
  184. }))
  185. this.loadData()
  186. },
  187. /**
  188. * 加载数据方法
  189. * @param {Object} pagination 分页选项器
  190. * @param {Object} filters 过滤条件
  191. * @param {Object} sorter 排序条件
  192. */
  193. loadData (pagination, filters, sorter) {
  194. this.localLoading = true
  195. const parameter = Object.assign({
  196. pageNum: (pagination && pagination.current) ||
  197. this.showPagination && this.localPagination.current || this.pageNum,
  198. pageSize: (pagination && pagination.pageSize) ||
  199. this.showPagination && this.localPagination.pageSize || this.pageSize
  200. },
  201. (sorter && sorter.field && {
  202. sortField: sorter.field
  203. }) || {},
  204. (sorter && sorter.order && {
  205. sortOrder: sorter.order
  206. }) || {}, {
  207. ...filters
  208. }
  209. )
  210. const result = this.data(parameter)
  211. // 对接自己的通用数据接口需要修改下方代码中的 r.pageNo, r.totalCount, r.data
  212. // eslint-disable-next-line
  213. if ((typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function') {
  214. result.then(r => {
  215. this.localPagination = this.showPagination && Object.assign({}, this.localPagination, {
  216. current: r.pageNum, // 返回结果中的当前分页数
  217. total: r.total, // 返回结果中的总记录数
  218. showSizeChanger: this.showSizeChanger,
  219. pageSize: (pagination && pagination.pageSize) ||
  220. this.localPagination.pageSize
  221. }) || false
  222. // 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
  223. if (r.rows.length === 0 && this.showPagination && this.localPagination.current > 1) {
  224. this.localPagination.current--
  225. this.loadData()
  226. return
  227. }
  228. // 这里用于判断接口是否有返回 r.totalCount 且 this.showPagination = true 且 pageNo 和 pageSize 存在 且 totalCount 小于等于 pageNo * pageSize 的大小
  229. // 当情况满足时,表示数据不满足分页大小,关闭 table 分页功能
  230. try {
  231. if ((['auto', true].includes(this.showPagination) && r.totalCount <= (r.pageNum * this.localPagination.pageSize))) {
  232. this.localPagination.hideOnSinglePage = true
  233. }
  234. } catch (e) {
  235. this.localPagination = false
  236. }
  237. this.localDataSource = r.rows // 返回结果中的数组数据
  238. this.localLoading = false
  239. })
  240. }
  241. },
  242. initTotalList (columns) {
  243. const totalList = []
  244. columns && columns instanceof Array && columns.forEach(column => {
  245. if (column.needTotal) {
  246. totalList.push({
  247. ...column,
  248. total: 0
  249. })
  250. }
  251. })
  252. return totalList
  253. },
  254. /**
  255. * 用于更新已选中的列表数据 total 统计
  256. * @param selectedRowKeys
  257. * @param selectedRows
  258. */
  259. updateSelect (selectedRowKeys, selectedRows) {
  260. this.selectedRows = selectedRows
  261. this.selectedRowKeys = selectedRowKeys
  262. const list = this.needTotalList
  263. this.needTotalList = list.map(item => {
  264. return {
  265. ...item,
  266. total: selectedRows.reduce((sum, val) => {
  267. const total = sum + parseInt(get(val, item.dataIndex))
  268. return isNaN(total) ? 0 : total
  269. }, 0)
  270. }
  271. })
  272. },
  273. /**
  274. * 清空 table 已选中项
  275. */
  276. clearSelected () {
  277. if (this.rowSelection) {
  278. this.rowSelection.onChange([], [])
  279. this.updateSelect([], [])
  280. }
  281. },
  282. /**
  283. * 处理交给 table 使用者去处理 clear 事件时,内部选中统计同时调用
  284. * @param callback
  285. * @returns {*}
  286. */
  287. renderClear (callback) {
  288. if (this.selectedRowKeys.length <= 0) return null
  289. return (
  290. <a style="margin-left: 24px" onClick={() => {
  291. callback()
  292. this.clearSelected()
  293. }}>清空</a>
  294. )
  295. },
  296. /** myColumns
  297. * 处理交给 table 使用者去处理 clear 事件时,内部选中统计同时调用
  298. * @returns {*}
  299. */
  300. renderColumns () {
  301. // this.columns
  302. // if (this.selectedRowKeys.length <= 0) { return null }
  303. const checkedKeys = []
  304. const treeData = []
  305. Object.keys(this.checkCustomMap).forEach((key) => {
  306. const item = this.checkCustomMap[key]
  307. if (item.checked) {
  308. checkedKeys.push(item.title)
  309. }
  310. treeData.push({ title: item.title,
  311. key: item.title })
  312. })
  313. // const choose=this.$t('m.common.choose_show_column')
  314. return (
  315. <a style="margin-left: 24px" onClick={() => {
  316. }}>
  317. <a-popover placement="bottom">
  318. <template slot="content">
  319. <a-tree
  320. checkable={true}
  321. onCheck={(checkedKeys) => {
  322. Object.keys(this.checkCustomMap).forEach((key) => {
  323. const item = this.checkCustomMap[key]
  324. item.checked = false
  325. })
  326. checkedKeys.forEach((item) => {
  327. this.checkCustomMap[item].checked = true
  328. })
  329. // 重新渲染render ()函数
  330. this.$forceUpdate()
  331. }}
  332. onSelect={(selectedKeys, info) => {
  333. }}
  334. checkedKeys={checkedKeys}
  335. treeData={treeData} />
  336. </template>
  337. <template slot="title">
  338. <span>选择显示列</span>
  339. </template>
  340. <a-icon type="appstore" />
  341. </a-popover>
  342. </a>
  343. )
  344. },
  345. renderAlert () {
  346. // 绘制统计列数据
  347. const needTotalItems = this.needTotalList.map((item) => {
  348. return (<span style="margin-right: 12px">
  349. {item.title}总计 <a style="font-weight: 600">{!item.customRender ? item.total : item.customRender(item.total)}</a>
  350. </span>)
  351. })
  352. // 绘制 清空 按钮
  353. const clearItem = (typeof this.alert.clear === 'boolean' && this.alert.clear) ? (
  354. this.renderClear(this.clearSelected)
  355. ) : (this.alert !== null && typeof this.alert.clear === 'function') ? (
  356. this.renderClear(this.alert.clear)
  357. ) : null
  358. const renderColumns = this.renderColumns()
  359. // const ss = this.$t('m.common.choose_total')
  360. // 绘制 alert 组件
  361. return (
  362. <a-alert showIcon={true} style="margin-bottom: 16px">
  363. <template slot="message">
  364. <span style="margin-right: 12px">已选择:<a style="font-weight: 600">{this.selectedRows.length}</a></span>
  365. {needTotalItems}
  366. {clearItem}
  367. {renderColumns}
  368. </template>
  369. </a-alert>
  370. )
  371. }
  372. },
  373. render () {
  374. const props = {}
  375. const localKeys = Object.keys(this.$data)
  376. const showAlert = (typeof this.alert === 'object' && this.alert !== null && this.alert.show) && typeof this.rowSelection.selectedRowKeys !== 'undefined' || this.alert
  377. Object.keys(T.props).forEach(k => {
  378. const localKey = `local${k.substring(0, 1).toUpperCase()}${k.substring(1)}`
  379. if (localKeys.includes(localKey)) {
  380. props[k] = this[localKey]
  381. return props[k]
  382. }
  383. if (k === 'rowSelection') {
  384. if (showAlert && this.rowSelection) {
  385. // 如果需要使用alert,则重新绑定 rowSelection 事件
  386. props[k] = {
  387. ...this.rowSelection,
  388. selectedRows: this.selectedRows,
  389. selectedRowKeys: this.selectedRowKeys,
  390. onChange: (selectedRowKeys, selectedRows) => {
  391. this.updateSelect(selectedRowKeys, selectedRows)
  392. typeof this[k].onChange !== 'undefined' && this[k].onChange(selectedRowKeys, selectedRows)
  393. }
  394. }
  395. return props[k]
  396. } else if (!this.rowSelection) {
  397. // 如果没打算开启 rowSelection 则清空默认的选择项
  398. props[k] = null
  399. return props[k]
  400. }
  401. }
  402. this[k] && (props[k] = this[k])
  403. return props[k]
  404. })
  405. const myColumns = []
  406. let totalWidth = 0
  407. Object.keys(this.checkCustomMap).forEach((key) => {
  408. const item = this.checkCustomMap[key]
  409. if (item.checked) {
  410. myColumns.push(item)
  411. const width = /^\d+/.exec(item.width)
  412. if (width && width.length > 0) {
  413. totalWidth += parseInt(width[0])
  414. }
  415. }
  416. })
  417. const x = props.scroll ? props.scroll.x : 0
  418. if (x && x > 0) {
  419. props.scroll.x = totalWidth
  420. }
  421. props.columns = myColumns
  422. const myClass = this.widthSpace ? 'future-table-width-space' : ''
  423. const table = (
  424. <a-table class={myClass} {...{ props, scopedSlots: { ...this.$scopedSlots } }} onChange={this.loadData}>
  425. { Object.keys(this.$slots).map(name => (<template slot={name}>{this.$slots[name]}</template>)) }
  426. </a-table>
  427. )
  428. return (
  429. <div class="table-wrapper">
  430. { showAlert ? this.renderAlert() : null }
  431. { table }
  432. </div>
  433. )
  434. }
  435. }