Commit 22f37fa0f40b6c740e7f296c34f3dafe9a8513a4
1 parent
feaa7fa5
1.主页kpi导航增加保存树形节点到浏览器缓存功能,退出登录后,从已保存的树形节点拿数据
Showing
2 changed files
with
94 additions
and
14 deletions
src/components/IndexCenter/IndexCenter.js
| 1 | 1 | /* eslint-disable */ |
| 2 | 2 | /* eslint-disable function-paren-newline */ |
| 3 | 3 | import React, { Component } from 'react'; |
| 4 | -import { BankOutlined, CloseOutlined, FolderFilled, FolderOpenFilled } from '@ant-design/icons'; | |
| 4 | +import { BankOutlined, CloseOutlined, FolderFilled, FolderOpenFilled, SaveOutlined } from '@ant-design/icons'; | |
| 5 | 5 | import { Layout, Card, Checkbox, Button, Table, Menu, Tabs, message, Tree, Input } from 'antd-v4'; |
| 6 | 6 | import logo from '@/assets/foot_logo.png'; |
| 7 | 7 | import config from '@/utils/config'; |
| ... | ... | @@ -88,12 +88,42 @@ class IndexCenter extends Component { |
| 88 | 88 | tableData: [], |
| 89 | 89 | bFilter: 'Pending,PendingCheck,MyWarning', |
| 90 | 90 | app: props.app, |
| 91 | + expandedKeys: [], | |
| 91 | 92 | }; |
| 92 | 93 | this.activeTree = ''; |
| 93 | 94 | this.treeId = ''; |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | componentWillMount() { |
| 98 | + // 从localStorage读取展开状态 | |
| 99 | + try { | |
| 100 | + const { userinfo } = this.props.app; | |
| 101 | + | |
| 102 | + const sUserNo = userinfo?.sUserNo; | |
| 103 | + | |
| 104 | + if (sUserNo) { | |
| 105 | + const key = `xlybusinessExpandKeys_${sUserNo}`; | |
| 106 | + | |
| 107 | + const xlybusinessExpandKeys = localStorage.getItem(key); | |
| 108 | + | |
| 109 | + if (xlybusinessExpandKeys) { | |
| 110 | + try { | |
| 111 | + const expandedKeys = JSON.parse(xlybusinessExpandKeys); | |
| 112 | + | |
| 113 | + this.setState({ expandedKeys }); | |
| 114 | + } catch (error) { | |
| 115 | + console.error('解析展开节点失败:', error); | |
| 116 | + } | |
| 117 | + } else { | |
| 118 | + console.log('localStorage中没有保存的展开节点'); | |
| 119 | + } | |
| 120 | + } else { | |
| 121 | + console.error('用户编号不存在,无法读取展开状态'); | |
| 122 | + } | |
| 123 | + } catch (error) { | |
| 124 | + console.error('读取展开状态失败:', error); | |
| 125 | + } | |
| 126 | + | |
| 97 | 127 | setTimeout(() => { |
| 98 | 128 | this.handleGetModelCenter('init'); |
| 99 | 129 | const commonUrl = `${config.server_host}parameter/getParameter?sModelsId=100`; |
| ... | ... | @@ -198,6 +228,20 @@ class IndexCenter extends Component { |
| 198 | 228 | disabled: true, |
| 199 | 229 | }]; |
| 200 | 230 | |
| 231 | + let expandedKeys = this.state.expandedKeys; | |
| 232 | + // 重新读取展开状态,确保应用到新构建的树结构上 | |
| 233 | + if (sUserNo) { | |
| 234 | + const key = `xlybusinessExpandKeys_${sUserNo}`; | |
| 235 | + const xlybusinessExpandKeys = localStorage.getItem(key); | |
| 236 | + if (xlybusinessExpandKeys) { | |
| 237 | + try { | |
| 238 | + expandedKeys = JSON.parse(xlybusinessExpandKeys); | |
| 239 | + | |
| 240 | + } catch (error) { | |
| 241 | + console.error('解析展开节点失败:', error); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + } | |
| 201 | 245 | if (model !== undefined && !this.treeId) { |
| 202 | 246 | // if (this.tableTree) { |
| 203 | 247 | // this.tableTree.scrollTop = 0; |
| ... | ... | @@ -440,10 +484,20 @@ class IndexCenter extends Component { |
| 440 | 484 | } |
| 441 | 485 | } |
| 442 | 486 | this.setState({ |
| 443 | - tabsPanes, tableTreeData, ...addState | |
| 487 | + tabsPanes, tableTreeData, ...addState, | |
| 488 | + expandedKeys | |
| 489 | + }); | |
| 490 | + } else { | |
| 491 | + // 如果没有重新构建树结构,也更新展开状态 | |
| 492 | + this.setState({ | |
| 493 | + comParameterList, | |
| 494 | + comThirdAppList, | |
| 495 | + dToDayNumSum, | |
| 496 | + dTobeDoneSum, | |
| 497 | + logoImageInfo, | |
| 498 | + expandedKeys | |
| 444 | 499 | }); |
| 445 | 500 | } |
| 446 | - this.setState({ comParameterList, comThirdAppList, dToDayNumSum, dTobeDoneSum, logoImageInfo }); | |
| 447 | 501 | } |
| 448 | 502 | componentDidMount() { |
| 449 | 503 | if (this.kpiTable && this.kpiTable.querySelector('.ant-table-body')) { |
| ... | ... | @@ -501,6 +555,27 @@ class IndexCenter extends Component { |
| 501 | 555 | localStorage.setItem(`xlybusinessActiveTree_${sUserNo}`, `${this.activeTree},${this.tableTreeScrollTop || 0},${this.kpiTableScrollTop || 0}`); |
| 502 | 556 | } |
| 503 | 557 | |
| 558 | + handleSaveExpandState = () => { | |
| 559 | + try { | |
| 560 | + const { userinfo } = this.props.app; | |
| 561 | + const sUserNo = userinfo?.sUserNo; | |
| 562 | + const { expandedKeys } = this.state; | |
| 563 | + if (sUserNo) { | |
| 564 | + const key = `xlybusinessExpandKeys_${sUserNo}`; | |
| 565 | + localStorage.setItem(key, JSON.stringify(expandedKeys)); | |
| 566 | + // 验证存储是否成功 | |
| 567 | + const storedValue = localStorage.getItem(key); | |
| 568 | + message.success('展开状态已保存'); | |
| 569 | + } else { | |
| 570 | + console.error('用户编号不存在,无法保存展开状态'); | |
| 571 | + message.error('保存失败:用户信息不完整'); | |
| 572 | + } | |
| 573 | + } catch (error) { | |
| 574 | + console.error('保存展开状态失败:', error); | |
| 575 | + message.error('保存失败:' + error.message); | |
| 576 | + } | |
| 577 | + } | |
| 578 | + | |
| 504 | 579 | handleDoubleClick = (record) => { |
| 505 | 580 | const newsId = record.sId; /* 表格行的sId */ |
| 506 | 581 | const newKey = new Date().getTime().toString(); |
| ... | ... | @@ -849,7 +924,10 @@ class IndexCenter extends Component { |
| 849 | 924 | </div> |
| 850 | 925 | <div className={styles.indexTabs}> |
| 851 | 926 | <div className={styles.navType}> |
| 852 | - <div className={styles.tableTreeTitle}>{NavigationType}</div> | |
| 927 | + <div className={styles.tableTreeTitle} style={{ display: 'flex', alignItems: 'center', marginBottom: '8px' }}> | |
| 928 | + <span style={{marginRight: '10px'}}>{NavigationType}</span> | |
| 929 | + <Button size="middle" type="text" icon={<SaveOutlined />} onClick={this.handleSaveExpandState} style={{ background:'none', border: 'none', padding: '0 4px' }} /> | |
| 930 | + </div> | |
| 853 | 931 | <div className={styles.tableTree} ref={(ref) => { |
| 854 | 932 | this.tableTree = ref; |
| 855 | 933 | if (this.tableTree && this.tableTreeScrollTop) { |
| ... | ... | @@ -857,9 +935,7 @@ class IndexCenter extends Component { |
| 857 | 935 | } |
| 858 | 936 | }}> |
| 859 | 937 | <Tree |
| 860 | - key={ | |
| 861 | - commonUtils.isNotEmptyArr(tableTreeData) ? this.treeId : Math.random() | |
| 862 | - } | |
| 938 | + key="navigationTree" | |
| 863 | 939 | className={"hide-file-icon"} |
| 864 | 940 | // switcherIcon={<img src={file} alt="svg" />} |
| 865 | 941 | onSelect={(e, select) => { |
| ... | ... | @@ -893,8 +969,12 @@ class IndexCenter extends Component { |
| 893 | 969 | tableData: data |
| 894 | 970 | }) |
| 895 | 971 | }} |
| 896 | - defaultSelectedKeys={[this.activeTree]} | |
| 897 | - defaultExpandAll={true} | |
| 972 | + onExpand={(expandedKeys) => { | |
| 973 | + this.setState({ expandedKeys }); | |
| 974 | + }} | |
| 975 | + expandedKeys={this.state.expandedKeys} | |
| 976 | + selectedKeys={[this.activeTree]} | |
| 977 | + defaultExpandAll={false} | |
| 898 | 978 | > |
| 899 | 979 | {this.renderTreeNodes(tableTreeData)} |
| 900 | 980 | </Tree> | ... | ... |
src/components/login/Login.js
| ... | ... | @@ -150,16 +150,16 @@ class LoginForm extends Component { |
| 150 | 150 | |
| 151 | 151 | handleLogin = async (values = {}, bSsoLogin = false) => { |
| 152 | 152 | this.setState({ loginLoading: true }); |
| 153 | - /* 点击登录按钮时 清空localStorage,保留左侧树定位 */ | |
| 154 | - const xlybusinessActiveTreeArr = []; | |
| 153 | + /* 点击登录按钮时 清空localStorage,保留左侧树定位和展开状态 */ | |
| 154 | + const xlybusinessDataArr = []; | |
| 155 | 155 | for (let i = 0; i < localStorage.length; i++) { |
| 156 | 156 | const key = localStorage.key(i); |
| 157 | - if (key && key.includes("xlybusinessActiveTree")) { | |
| 158 | - xlybusinessActiveTreeArr.push([key, localStorage.getItem(key)]); | |
| 157 | + if (key && (key.includes("xlybusinessActiveTree") || key.includes("xlybusinessExpandKeys"))) { | |
| 158 | + xlybusinessDataArr.push([key, localStorage.getItem(key)]); | |
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | localStorage.clear(); |
| 162 | - xlybusinessActiveTreeArr.forEach(item => { | |
| 162 | + xlybusinessDataArr.forEach(item => { | |
| 163 | 163 | localStorage.setItem(item[0], item[1]); |
| 164 | 164 | }); |
| 165 | 165 | const value = values; | ... | ... |