table.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div v-tableLoad="tableLoad">
  3. <!-- :header-cell-style="{background:'#EEEEEE',color:'#333'}" -->
  4. <el-table v-loading="loading" ref="table" :row-class-name="tableClassName" highlight-current-row :data="data != undefined ? data : list" size="mini" :height="height ? height : list.length <= 4?'260px':list.length <= 20?'calc(100vh - 420px)':'calc(100vh - 420px)'" @row-click="rowClick" style="width:100%;" :header-cell-style="{background:'#fafafafa',height:'40px',color:'#000000'}" @selection-change="selectionChange">
  5. <el-table-column
  6. type="selection"
  7. width="35" fixed v-if="checkbox">
  8. </el-table-column>
  9. <el-table-column v-for="col in layout" :key="col.tablecolid" :prop="col.columnname" :label="col.title" :width="width && col.width === 0 ? 150 : col.width" :fixed="fixedName ? fixedName == col.columnname?redirect ? redirect : 'right' :false : false">
  10. <template v-slot:header="{ column,$index }">
  11. <div style="display: flex;align-items: center;align-content: center;">
  12. <span @click="setSort(col,0)">{{ col.title }}</span>
  13. <div v-if="col.sortable == 1" class="sort-icon">
  14. <img src="@/assets/降序.svg" v-if="!col.sort" @click="setSort(col,1)">
  15. <img src="@/assets/升序.svg" v-else @click="setSort(col,0)">
  16. </div>
  17. </div>
  18. </template>
  19. <template slot-scope="scope">
  20. <div class="table-panel">
  21. <!-- 自定义表格显示内容 -->
  22. <slot v-if="custom" name="customcol" :column="{data:scope.row,columnname:col.columnname}"></slot>
  23. <!-- 否则就是默认 -->
  24. <span v-else>{{scope.row[col.columnname]}}</span>
  25. <!-- 操作结构内容 -->
  26. <slot v-if="col.columnname === 'operation'" name="opreation" :data="scope.row"></slot>
  27. </div>
  28. </template>
  29. </el-table-column>
  30. </el-table>
  31. <div class="container normal-panel" style="text-align:right" v-if="param && page">
  32. <el-pagination
  33. background
  34. @size-change="handleSizeChange"
  35. @current-change="handleCurrentChange"
  36. :current-page="currentPage"
  37. :page-sizes="[20, 50, 100, 200]"
  38. :page-size="param.content.pageSize"
  39. layout="total,sizes, prev, pager, next, jumper"
  40. :total="total">
  41. </el-pagination>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import {mapGetters} from "vuex"
  47. export default {
  48. /*
  49. layout:表结构数据;
  50. data:表渲染数据;
  51. custom:是否启用自定义结构;
  52. opwidth:操作列宽度
  53. */
  54. name:'MyTable',
  55. props:['layout','data','custom','height','fixedName','width','checkbox','redirect','customHeader','param','noQuery','page','tableName'],
  56. data () {
  57. return {
  58. list:[],
  59. total:0,
  60. currentPage:1,
  61. totalPage:0,
  62. }
  63. },
  64. computed:{
  65. ...mapGetters({
  66. loading:'loading'
  67. })
  68. },
  69. methods:{
  70. tableLoad () {
  71. if (this.param.content.pageNumber == this.totalPage) return
  72. this.param.content.pageNumber += 1
  73. console.log(this.param.content.pageNumber);
  74. this.listData(()=>{},true)
  75. },
  76. async listData (callback,init) {
  77. if (!init) this.param.content.pageNumber = 1
  78. if (this.tableName) this.param.content.tableid = this.tool.tabelCol(this.$route.name)[this.tableName].tableid
  79. let res = await this.$api.requested(this.param)
  80. this.total = res.total
  81. this.currentPage = res.pageNumber
  82. this.list = this.param.content.pageNumber == 1 ? res.data : [...this.list,...res.data]
  83. this.totalPage = res.pageTotal
  84. this.param.content.sort = res.sort
  85. callback && callback()
  86. },
  87. setSort(column,sort) {
  88. this.param.content.simplesort = {}
  89. column.cur = 1
  90. if (column.columnname == 'matchratio') {
  91. this.param.content.sort = this.param.content.sort.map(v => {
  92. return {
  93. reversed:v.sortname=='匹配度'?sort==1?0:1:v.reversed,
  94. sorted:v.sortname=='匹配度'?1:0,
  95. sortid:v.sortid,
  96. sortname:v.sortname
  97. }
  98. })
  99. delete this.param.content.simplesort
  100. column.sort = sort
  101. } else if (column.columnname == 'maxruntime') {
  102. this.param.content.sort = this.param.content.sort.map(v => {
  103. return {
  104. reversed:v.sortname=='开关时间'?sort==1?0:1:v.reversed,
  105. sorted:v.sortname=='开关时间'?1:0,
  106. sortid:v.sortid,
  107. sortname:v.sortname
  108. }
  109. })
  110. column.sort = sort
  111. delete this.param.content.simplesort
  112. } else {
  113. column.sort = this.param.content.simplesort[column.columnname] = sort
  114. delete this.param.content.sort
  115. }
  116. this.layout.forEach(e=>{
  117. if (e.columnname !== column.columnname) {
  118. e.cur = 0
  119. e.sort = 0
  120. }
  121. })
  122. this.listData()
  123. },
  124. rowClick (row) {
  125. this.$emit('rowClick',row)
  126. },
  127. tableClassName ({row,rowIndex}) {
  128. row.index = rowIndex
  129. },
  130. selectionChange(row){
  131. this.$emit('selectionChange',row)
  132. },
  133. handleSizeChange(val) {
  134. // console.log(`每页 ${val} 条`);
  135. this.param.content.pageSize = val
  136. this.listData(()=>{},true)
  137. },
  138. handleCurrentChange(val) {
  139. // console.log(`当前页: ${val}`);
  140. this.param.content.pageNumber = val
  141. this.listData(()=>{},true)
  142. },
  143. },
  144. created () {
  145. !this.noQuery && this.param && this.listData()
  146. },
  147. mounted () {
  148. }
  149. }
  150. </script>
  151. <style scoped>
  152. .sort-icon img {
  153. cursor: pointer;
  154. width: 18px;
  155. height: 18px;
  156. }
  157. </style>