From ffb1bd190e9d4263c8c7d6c0096e0ec844cb3b52 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期四, 19 六月 2025 17:30:55 +0800
Subject: [PATCH] 1.系统管理样式修改

---
 src/components/Table/ETable.vue |  165 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 141 insertions(+), 24 deletions(-)

diff --git a/src/components/Table/ETable.vue b/src/components/Table/ETable.vue
index 6fc7f1d..24fe060 100644
--- a/src/components/Table/ETable.vue
+++ b/src/components/Table/ETable.vue
@@ -1,15 +1,33 @@
-<template>
-  <el-table v-loading="loading" :data="tableData" :border="border" :show-selection="showSelection" :max-height="maxHeight"
-    :header-cell-style="{ background: '#EBEEF5', color: '#3D3D3D' }" @selection-change="handleSelectionChange"
-    @row-click="handleRowClick" @row-dblclick="handleRowDblClick" @cell-click="handleCellClick" :max-width="maxWidth"
-    @export="handleExport">
+<template>  <el-table 
+    v-loading="loading" 
+    :data="tableData" 
+    :border="border" 
+    :show-selection="showSelection" 
+    :max-height="maxHeight"
+    :header-cell-style="{ background: '#EBEEF5', color: '#3D3D3D' }" 
+    @selection-change="handleSelectionChange"
+    @row-click="handleRowClick" 
+    @row-dblclick="handleRowDblClick" 
+    @cell-click="handleCellClick" 
+    :max-width="maxWidth"
+    @export="handleExport" 
+    :default-selection="defaultSelection"
+    :show-overflow-tooltip="showOverflowTooltip"
+    ref="tableRef" 
+    :row-key="rowKey"
+    style="width: 100%"
+  >
     <el-table-column v-if="showSelection" type="selection" width="55" align="center" />
-    <el-table-column v-if="showIndex" label="搴忓彿" type="index" width="60" align="center" /> <template
-      v-for="col in columns" :key="col.prop">
-      <el-table-column v-bind="col" :show-overflow-tooltip="shouldShowTooltip(col, tableData)"
+    <el-table-column v-if="showIndex" label="搴忓彿" width="60" align="center" fixed="left">
+      <template #default="scope">
+        {{ getRowIndex(scope.$index) }}
+      </template>
+    </el-table-column>
+    <template v-for="col in columns" :key="col.prop">
+      <el-table-column v-bind="col"
         :formatter="col.formatter || defaultFormatter" align="center">
-        <template v-if="col.slot" #default>
-          <slot></slot>
+        <template v-if="col.slot" #default="scope">
+          <slot :name="col.prop" :row="scope.row" :column="scope.column" :index="scope.$index"></slot>
         </template>
       </el-table-column>
     </template>
@@ -33,7 +51,7 @@
 </template>
   
   <script setup>
-import { defineEmits } from 'vue'
+import { defineEmits, ref , defineProps, onMounted ,defineExpose, watch, nextTick} from 'vue'
 import { ElMessage, ElMessageBox } from 'element-plus'
 const props = defineProps({
   // 鏈�澶у搴�
@@ -111,24 +129,36 @@
   operations: {
     type: Array,
     default: () => ['edit', 'delete', 'export']
-  },
-  // 鍒犻櫎纭淇℃伅
+  },  // 鍒犻櫎纭淇℃伅
   deleteConfirmText: {
     type: String,
     default: '纭鍒犻櫎璇ヨ褰曪紵'
+  },
+  // 榛樿閫変腑鐨勮 ID 鏁扮粍
+  defaultSelectedIds: {
+    type: Array,
+    default: () => []
+  },
+  // 琛屽敮涓�鏍囪瘑瀛楁鍚嶏紙榛樿涓� id锛�
+  rowKey: {
+    type: String,
+    default: 'id'
+  },  showOverflowTooltip: {
+    type: Boolean,
+    default: true
+  },
+  // 褰撳墠椤电爜
+  currentPage: {
+    type: Number,
+    default: 1
+  },
+  // 姣忛〉澶у皬
+  pageSize: {
+    type: Number,
+    default: 10
   }
 })
-// 妫�鏌ュ垪鏄惁闇�瑕佹樉绀簍ooltip
-const shouldShowTooltip = (col, data) => {
-  // 濡傛灉鍒楅厤缃腑鏄庣‘璁剧疆浜唖howOverflowTooltip锛屼娇鐢ㄨ璁剧疆
-  if (col.hasOwnProperty('showOverflowTooltip')) {
-    return col.showOverflowTooltip;
-  }
-  // 濡傛灉娌℃湁prop锛岀洿鎺ヨ繑鍥瀎alse
-  if (!col.prop) return false;
-  // 妫�鏌ヨ鍒楀湪鎵�鏈夋暟鎹腑鏄惁鏈夐潪绌哄�硷紝榛樿鏄剧ずtooltip
-  return data.some(row => row[col.prop] != null && row[col.prop] !== '');
-};
+const tableRef = ref(null)
 
 // 榛樿鐨勬牸寮忓寲鍑芥暟
 const defaultFormatter = (row, column, cellValue) => {
@@ -160,9 +190,96 @@
 const handleExport = (row) => {
   emit('export', row)
 }
+
+// 璁$畻鍒嗛〉搴忓彿
+const getRowIndex = (index) => {
+  return (props.currentPage - 1) * props.pageSize + index + 1;
+};
+
+// 姝g‘鐨� toggleRowSelection 鏂规硶锛氶拡瀵瑰崟琛�
+const toggleRowSelection = (row, selected) => {
+  if (tableRef.value && row) {
+    tableRef.value.toggleRowSelection(row, selected);
+  }
+};
+
+// 鎵归噺璁剧疆琛岄�変腑鐘舵�佺殑鏂规硶
+const setRowsSelection = (rows, selected = true) => {
+  if (tableRef.value && Array.isArray(rows)) {
+    rows.forEach((row) => {
+      tableRef.value.toggleRowSelection(row, selected);
+    });
+  }
+};
+
+// 澶嶉�夋榛樿閫変腑鐘舵��
+const defaultSelection = ref([])
+// 璁剧疆榛樿閫変腑鐘舵��
+const setDefaultSelection = () => {
+  if (!tableRef.value || !props.defaultSelectedIds.length || !props.tableData.length) {
+    return;
+  }
+  
+  
+  // 寤惰繜鎵ц锛岀‘淇濊〃鏍煎畬鍏ㄦ覆鏌�
+  nextTick(() => {
+    setTimeout(() => {
+      try {
+        tableRef.value.clearSelection();
+        const rowsToSelect = props.tableData.filter(row => {
+          const rowId = row[props.rowKey];
+          return props.defaultSelectedIds.includes(rowId);
+        });
+        rowsToSelect.forEach(row => {
+          tableRef.value.toggleRowSelection(row, true);
+        });
+      } catch (error) {
+      }
+    }, 100);
+  });
+};
+
+// 鐩戝惉鏁版嵁鍙樺寲锛岃嚜鍔ㄨ缃粯璁ら�変腑
+watch(() => [props.tableData, props.defaultSelectedIds], () => {
+  if (props.tableData.length > 0 && props.defaultSelectedIds.length > 0) {
+    setDefaultSelection();
+  }
+}, { 
+  deep: true, 
+  immediate: true 
+});
+
+// 缁勪欢鎸傝浇鍚庤缃粯璁ら�変腑
+onMounted(() => {
+  if (props.defaultSelectedIds.length > 0) {
+    setDefaultSelection();
+  }
+});
+
+// 鏆撮湶 el-table 鐨勫疄渚嬪拰甯哥敤鏂规硶
+defineExpose({
+  // 鍗曡閫変腑/鍙栨秷閫変腑
+  toggleRowSelection,
+  // 鎵归噺璁剧疆琛岄�変腑鐘舵��
+  setRowsSelection,
+  // 鏍规嵁ID鏁扮粍閫変腑琛�
+  setDefaultSelection,
+  // 娓呴櫎鎵�鏈夐�変腑
+  clearSelection: () => tableRef.value?.clearSelection(),
+  // 鑾峰彇閫変腑鐨勮
+  getSelectionRows: () => tableRef.value?.getSelectionRows() || [],
+  // 璁剧疆褰撳墠琛�
+  setCurrentRow: (row) => tableRef.value?.setCurrentRow(row),
+  // 鑾峰彇琛ㄦ牸瀹炰緥锛堝闇�鐩存帴鎿嶄綔锛�
+  getTableRef: () => tableRef.value
+});
+
 </script>
   
   <style scoped>
+  :deep(.el-tooltip) {
+    justify-content: center !important;
+  }
 .el-table {
     margin: 20px 0 !important;
   }

--
Gitblit v1.9.3