From 99a6b562335da60795f61b7c29f5414b3bfd3469 Mon Sep 17 00:00:00 2001
From: DESKTOP-IGCH756\PC <921509429@qq.com>
Date: 星期四, 28 九月 2023 14:58:34 +0800
Subject: [PATCH] 优化表格列的显示与隐藏小组件

---
 src/views/system/user/index.vue       |   14 +++---
 src/types/global.d.ts                 |    1 
 src/components/RightToolbar/index.vue |   53 ++++++++++++++------------
 3 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/src/components/RightToolbar/index.vue b/src/components/RightToolbar/index.vue
index 09036b7..0f4219f 100644
--- a/src/components/RightToolbar/index.vue
+++ b/src/components/RightToolbar/index.vue
@@ -7,13 +7,23 @@
       <el-tooltip class="item" effect="dark" content="鍒锋柊" placement="top">
         <el-button circle icon="Refresh" @click="refresh()" />
       </el-tooltip>
-      <el-tooltip class="item" effect="dark" content="鏄鹃殣鍒�" placement="top" v-if="columns">
-        <el-button circle icon="Menu" @click="showColumn()" />
+      <el-tooltip class="item" effect="dark" content="鏄剧ず/闅愯棌鍒�" placement="top" v-if="columns">
+        <el-popover placement="bottom" trigger="click">
+          <div class="tree-header">鏄剧ず/闅愯棌鍒�</div>
+          <el-tree
+            ref="columnRef"
+            :data="columns"
+            show-checkbox
+            @check="columnChange"
+            node-key="key"
+            :props="{ label: 'label', children: 'children' }"
+          ></el-tree>
+          <template #reference>
+            <el-button circle icon="Menu" />
+          </template>
+        </el-popover>
       </el-tooltip>
     </el-row>
-    <el-dialog :title="title" v-model="open" append-to-body>
-      <el-transfer :titles="['鏄剧ず', '闅愯棌']" v-model="value" :data="columns" @change="dataChange"></el-transfer>
-    </el-dialog>
   </div>
 </template>
 
@@ -29,14 +39,8 @@
     gutter: propTypes.number.def(10),
 })
 
+const columnRef = ref<ElTreeInstance>();
 const emits = defineEmits(['update:showSearch', 'queryTable']);
-
-// 鏄鹃殣鏁版嵁
-const value = ref<Array<number>>([]);
-// 寮瑰嚭灞傛爣棰�
-const title = ref("鏄剧ず/闅愯棌");
-// 鏄惁鏄剧ず寮瑰嚭灞�
-const open = ref(false);
 
 const style = computed(() => {
     const ret: any = {};
@@ -56,23 +60,19 @@
     emits("queryTable");
 }
 
-// 鍙充晶鍒楄〃鍏冪礌鍙樺寲
-function dataChange(data: TransferKey[]) {
-    props.columns?.forEach((item) => {
-        item.visible = !data.includes(item.key);
-    })
-}
-
-// 鎵撳紑鏄鹃殣鍒梔ialog
-const showColumn = () => {
-    open.value = true;
+// 鏇存敼鏁版嵁鍒楃殑鏄剧ず鍜岄殣钘�
+function columnChange(...args: any[]) {
+  props.columns?.forEach((item) => {
+    item.visible = args[1].checkedKeys.includes(item.key);
+  })
 }
 
 // 鏄鹃殣鍒楀垵濮嬮粯璁ら殣钘忓垪
 onMounted(() => {
     props.columns?.forEach((item) => {
-        if (!item.visible) {
-            value.value.push(item.key);
+        if (item.visible) {
+          columnRef.value?.setChecked(item.key, true, false);
+            // value.value.push(item.key);
         }
     })
 })
@@ -91,4 +91,9 @@
 .my-el-transfer {
   text-align: center;
 }
+.tree-header{
+  width: 100%;
+  line-height: 24px;
+  text-align: center;
+}
 </style>
diff --git a/src/types/global.d.ts b/src/types/global.d.ts
index b9bc5f4..130f855 100644
--- a/src/types/global.d.ts
+++ b/src/types/global.d.ts
@@ -13,6 +13,7 @@
     key: number;
     label: string;
     visible: boolean;
+    children: Array<FieldOption>;
   }
 
   /**
diff --git a/src/views/system/user/index.vue b/src/views/system/user/index.vue
index 9a6d82c..284d941 100644
--- a/src/views/system/user/index.vue
+++ b/src/views/system/user/index.vue
@@ -340,13 +340,13 @@
 })
 // 鍒楁樉闅愪俊鎭�
 const columns = ref<FieldOption[]>([
-  { key: 0, label: `鐢ㄦ埛缂栧彿`, visible: false },
-  { key: 1, label: `鐢ㄦ埛鍚嶇О`, visible: true },
-  { key: 2, label: `鐢ㄦ埛鏄电О`, visible: true },
-  { key: 3, label: `閮ㄩ棬`, visible: true },
-  { key: 4, label: `鎵嬫満鍙风爜`, visible: true },
-  { key: 5, label: `鐘舵�乣, visible: true },
-  { key: 6, label: `鍒涘缓鏃堕棿`, visible: true }
+  { key: 0, label: `鐢ㄦ埛缂栧彿`, visible: false,children: [] },
+  { key: 1, label: `鐢ㄦ埛鍚嶇О`, visible: true,children: [] },
+  { key: 2, label: `鐢ㄦ埛鏄电О`, visible: true,children: [] },
+  { key: 3, label: `閮ㄩ棬`, visible: true,children: [] },
+  { key: 4, label: `鎵嬫満鍙风爜`, visible: true,children: [] },
+  { key: 5, label: `鐘舵�乣, visible: true,children: [] },
+  { key: 6, label: `鍒涘缓鏃堕棿`, visible: true,children: [] }
 ])
 
 

--
Gitblit v1.9.3