index.js 12 KB

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