page.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // 分页,分页几乎每个列表页面都会存在,代码的重合度很高,所以提出来了
  2. const pageMixins = {
  3. data () {
  4. return {
  5. // 当前页
  6. current: 1,
  7. // 每页大小
  8. size: 10,
  9. // 总页数
  10. totalPage: 0,
  11. // 总数据条数
  12. totalCount: 0,
  13. prevText: '',
  14. nextText: '',
  15. // 排序相关
  16. sortForm: {
  17. /**
  18. * key: 字段名称
  19. * value: descending 、ascending、''
  20. */
  21. sortFieldMap: {},
  22. // 定义排序字段的排序,如果不支持多个字段同时排序,可以传入一个即可
  23. sortFieldOrderList: [],
  24. // 缓存列和类名信息,方便后面清空排序字段信息
  25. columnCacheMap: {}
  26. },
  27. // 允许多字段排序
  28. enableMultiFieldOrder: false,
  29. sortFieldHeaderTipMap: {}
  30. }
  31. },
  32. watch: {
  33. 'sortForm.sortFieldMap': {
  34. handler (sortFieldMap, oldV) {
  35. for (const columnName in sortFieldMap) {
  36. const order = sortFieldMap[columnName]
  37. if (!order) {
  38. this.$set(this.sortFieldHeaderTipMap, columnName, '点击升序')
  39. } else if (order === 'ascending') {
  40. this.$set(this.sortFieldHeaderTipMap, columnName, '点击降序')
  41. } else if (order === 'descending') {
  42. this.$set(this.sortFieldHeaderTipMap, columnName, '取消排序')
  43. } else {
  44. this.$set(this.sortFieldHeaderTipMap, columnName, '点击升序')
  45. }
  46. }
  47. },
  48. deep: true
  49. }
  50. },
  51. methods: {
  52. /**
  53. * 初始化排序信息
  54. * @param sortFieldOrderList 排序字段的优先级
  55. * @param defaultSortFieldMap 默认排序字段信息 descending: 降序,ascending:升序
  56. */
  57. initSortField (sortFieldOrderList = [], defaultSortFieldMap = {}) {
  58. if (defaultSortFieldMap) {
  59. for (const field in defaultSortFieldMap) {
  60. const order = defaultSortFieldMap[field]
  61. this.$set(this.sortForm.sortFieldMap, field, order)
  62. }
  63. }
  64. for (let i = 0; i < sortFieldOrderList.length; i++) {
  65. const field = sortFieldOrderList[i]
  66. const order = this.sortForm.sortFieldMap[field]
  67. if (!order) {
  68. // 解决未设置默认排序值字段提示为空
  69. this.$set(this.sortForm.sortFieldMap, field, '')
  70. }
  71. }
  72. this.sortForm.sortFieldOrderList = sortFieldOrderList
  73. },
  74. // 排序状态记录并激活,否则和页面上的排序对不上
  75. sortStyle ({ row, column, rowIndex, columnIndex }) {
  76. const sortColumnOrder = this.sortForm.sortFieldMap[column.property]
  77. if (sortColumnOrder) {
  78. column.order = sortColumnOrder
  79. }
  80. this.sortForm.columnCacheMap[column.property] = column
  81. },
  82. // 对应表格的 @sort-change 事件,当用户改变了排序的状态时触发
  83. reSort (column) {
  84. if (!this.enableMultiFieldOrder) {
  85. // 不允许多个字段同时排序,清空之前的排序信息
  86. for (const field in this.sortForm.columnCacheMap) {
  87. const column = this.sortForm.columnCacheMap[field]
  88. column.order = ''
  89. }
  90. for (const field in this.sortForm.sortFieldMap) {
  91. this.sortForm.sortFieldMap[field] = ''
  92. }
  93. }
  94. this.$set(this.sortForm.sortFieldMap, column.prop, column.order)
  95. this.reSearch()
  96. },
  97. reSearch () {
  98. this.current = 1
  99. this.getDataList()
  100. },
  101. getDataList () {
  102. console.error('你应该重写getDataList方法')
  103. },
  104. // 每页大小改变触发
  105. sizeChangeHandle (val) {
  106. this.size = val
  107. this.current = 1
  108. this.getDataList()
  109. },
  110. // 当前页数改变
  111. currentChangeHandle (val) {
  112. this.current = val
  113. this.getDataList()
  114. },
  115. getSortForm () {
  116. return {
  117. sortFieldMap: this.sortForm.sortFieldMap,
  118. sortFieldOrderList: this.sortForm.sortFieldOrderList
  119. }
  120. }
  121. }
  122. }
  123. export { pageMixins }