index.vue 6.0 KB

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