table.vue 6.4 KB

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