index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <div class="containerTable">
  3. <div class="table_box">
  4. <div class="table-title-btn">
  5. <slot name="table-title-btn"></slot>
  6. </div>
  7. <el-table ref="TableTreeRef" class="custom-table" v-loading="props.loading"
  8. :header-cell-style="{ 'background-color': '#EEF1F6', 'border-bottom': '1px solid #EEF1F6', 'font-size': '15px', 'color': '#333', 'padding': '10px 0' }"
  9. :data="props.tableData" :height="tableHeight" row-key="id" border
  10. style="width: 100%;">
  11. <!-- 空内容自定义 -->
  12. <template #empty>
  13. <div class="flex f-c a-c ">
  14. <el-empty image="../../../src/assets/images/empty.png" />
  15. </div>
  16. </template>
  17. <el-table-column type="selection" v-if="showSelect" :selectable="selectable" width="55" />
  18. <el-table-column v-if="showIndex" type="index" label="序号" width="55" />
  19. <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label"
  20. :sortable="column.sortable ? 'custom' : false" :width="column.width" :align="column.align?column.align:'center'" >
  21. <template #default="scope">
  22. <RouterLink v-if="column.component === 'RouterLink'" :to="getRouterLinkTo(scope.row, column)" class="custom-link">
  23. {{ scope.row[column.prop] }}
  24. </RouterLink>
  25. <el-tag v-else-if="column.component === 'Tag'" :type="getTagTypeAndText(scope.row, column).type">
  26. {{ getTagTypeAndText(scope.row, column).text }}
  27. </el-tag>
  28. <ImagePreview v-else-if="column.component === 'Image'" :src="getImgTo(scope.row, column)" width="50px" height="50px"></ImagePreview>
  29. <span v-else>
  30. {{ getDisplayText(scope.row, column) }}
  31. </span>
  32. </template>
  33. <template #header>
  34. <slot name="search" :slotData="column"></slot>
  35. </template>
  36. </el-table-column>
  37. <el-table-column label="操作" fixed="right" :width="props.columnwidth" align="center">
  38. <template #default="scope">
  39. <div class="flex j-c a-c">
  40. <slot name="operation" class="operationClass" :operationData="scope.row"></slot>
  41. </div>
  42. </template>
  43. </el-table-column>
  44. </el-table>
  45. </div>
  46. </div>
  47. </template>
  48. <script lang="ts" setup>
  49. import { defineProps, ref, onMounted, defineEmits } from "vue";
  50. import type { TableInstance } from "element-plus";
  51. interface TableRow {
  52. [key: string]: any;
  53. }
  54. interface Row {
  55. [key: string]: any; // 假设Row是一个具有任意属性的对象
  56. }
  57. interface RouterLink {
  58. name: string;
  59. }
  60. const emit = defineEmits([]);
  61. const props = defineProps<{
  62. tableData: Array<TableRow>;
  63. columnwidth: number,
  64. loading?:boolean,
  65. columns: {
  66. prop: string;
  67. label: string;
  68. sortable?: boolean;
  69. width?: string;
  70. style?: string;
  71. align?:string,
  72. component?:string,
  73. formatter?: (row: any, column: any, cellValue: any, index: number) => any;
  74. }[];
  75. showSelect: boolean;
  76. showIndex: boolean;
  77. tableheight?: (number | string),
  78. }>();
  79. const tableHeight = ref(props.tableheight); // 初始化表格高度
  80. const TableTreeRef = ref<TableInstance>();
  81. const TableData = ref<TableRow[]>();
  82. onMounted(() => {
  83. let counter = 1;
  84. TableData.value = props.tableData.map((row) => ({
  85. ...row,
  86. _edit: false, // 设置默认非编辑状态
  87. counter: counter++
  88. }));
  89. setTimeout(() => {
  90. const tableElement = TableTreeRef.value?.$el as HTMLElement;
  91. if (tableElement) {
  92. const offsetTop = tableElement.offsetTop; // 获取 offsetTop
  93. let height = window.innerHeight;
  94. console.log(height)
  95. tableHeight.value = height - offsetTop - 300 + "px";
  96. }
  97. }, 10);
  98. });
  99. //空值处理
  100. const displayValue = (value: any) => {
  101. return value === null || value === undefined || value === '' ? '-' : value;
  102. }
  103. const selectable = () => true;
  104. // 格式化内容配置
  105. function getRouterLinkTo(row: Row, column: any): RouterLink {
  106. return column.formatter ? column.formatter(row, column, row[column.prop]) : { name: 'OrganizationAdd' };
  107. }
  108. function getImgTo(row: Row, column: any) {
  109. return row[column.prop];
  110. }
  111. function getTagTypeAndText(row: Row, column: any) {
  112. // 使用 formatter 返回一个对象,包含 type 和 text
  113. return column.formatter ? column.formatter(row, column, row[column.prop]) : { type: 'primary', text: row[column.prop] };
  114. }
  115. function getDisplayText(row: Row, column: any): string {
  116. return column.formatter ? column.formatter(row, column, row[column.prop]) :displayValue(row[column.prop]);
  117. }
  118. </script>
  119. <style lang="scss" scoped>
  120. .el-table .el-table__cell .routerlick > a {
  121. color: red;
  122. }
  123. .containerTable {
  124. display: flex;
  125. flex-direction: column;
  126. width: 100%;
  127. height: auto;
  128. }
  129. .table_box {
  130. flex: 1;
  131. margin-bottom: 10px;
  132. overflow-y: auto;
  133. .table_item_container {
  134. padding: 0;
  135. margin-left: 20px;
  136. .table_item {
  137. margin: 5px 0;
  138. }
  139. }
  140. .table-title-btn {
  141. margin-bottom: 20px;
  142. :deep(.el-button) {
  143. margin-right: 20px;
  144. }
  145. }
  146. }
  147. /* 自定义树状表格图标 */
  148. // /* 有子节点 且未展开 */
  149. // .custom-table :deep(.el-table__expand-icon) {
  150. // display: flex;
  151. // align-items: center;
  152. // justify-content: center;
  153. // width: 18px;
  154. // height: 18px;
  155. // background: url("../../assets/icons/caretRight.svg") no-repeat;
  156. // background-size: auto;
  157. // svg {
  158. // display: none;
  159. // }
  160. // }
  161. // /* 有子节点 且已展开 */
  162. // .custom-table :deep(.el-table__expand-icon--expanded) {
  163. // display: inline-block;
  164. // display: flex;
  165. // align-items: center;
  166. // justify-content: center;
  167. // width: 18px;
  168. // height: 18px;
  169. // content: "";
  170. // background: url("../../assets/icons/caretRight.svg") no-repeat;
  171. // background-size: auto;
  172. // transform: rotate(90deg);
  173. // svg {
  174. // display: none;
  175. // }
  176. // }
  177. :deep(.el-table) {
  178. .el-table__row--level-1 {
  179. background-color: #eef1f6;
  180. }
  181. }
  182. .operationClass {
  183. .el-button {
  184. margin: 0 7px;
  185. }
  186. }
  187. </style>