CommonTree.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <template>
  2. <!-- 公共树形组件:基于 Element UI el-tree 封装 -->
  3. <!-- common_tree_wrapper:用作非 scoped 样式的全局命名空间,防止样式泄漏到其他组件 -->
  4. <div class="common_tree_wrapper common_tree">
  5. <!--
  6. el-tree 核心配置说明:
  7. - ref: 用于父组件通过 this.$refs 调用 el-tree 原生方法(如 setCheckedKeys、getCheckedNodes 等)
  8. - data: 树形数据源(由父组件通过 props 传入)
  9. - props: 配置 el-tree 如何读取数据结构中的字段(label/children)
  10. - node-key: 每个树节点的唯一标识字段,用于选中/高亮定位
  11. - show-checkbox: 开启多选复选框
  12. - highlight-current: 高亮当前点击选中的节点
  13. - default-expand-all: 默认展开所有节点
  14. -->
  15. <el-tree ref="commonTree" class="tree_container" :data="treeData" :props="treeProps" :node-key="nodeKey"
  16. :show-checkbox="showCheckbox" :highlight-current="highlightCurrent" :default-expand-all="defaultExpandAll"
  17. :default-expanded-keys="defaultExpandedKeys" :current-node-key="currentNodeKey"
  18. :expand-on-click-node="expandOnClickNode" @node-click="handleNodeClick" @check="handleNodeCheck">
  19. <!--
  20. 自定义树节点内容插槽(slot-scope)
  21. - node: 当前节点的完整数据对象(label、children、data 等)
  22. - data: 当前节点绑定的用户数据(即 treeData 中的每一项)
  23. -->
  24. <span class="custom_tree_node" slot-scope="{ node, data }">
  25. <!-- 节点名称区域 -->
  26. <span class="node_label" :title="data[treeProps.label]">
  27. <!-- 名称文本单独包裹一层,确保省略号只作用于真正承载文字的元素 -->
  28. <span class="node_label_text">{{ data[treeProps.label] }}</span>
  29. <!-- 右侧计数(可选,数据存在 count 字段时显示) -->
  30. <span class="node_count" v-if="data.count !== undefined && data.count !== null">
  31. ({{ data.count }})
  32. </span>
  33. </span>
  34. <!--
  35. AI 讲解按钮(Element UI 文字按钮 type="text")
  36. - 默认隐藏(opacity: 0)
  37. - 通过父级 hover 显示
  38. - @click.stop 阻止冒泡,避免触发树节点点击
  39. -->
  40. <!-- <el-button class="ai_explain_btn" type="text" size="mini" icon="" @click.stop="handleAiExplain(node, data)">
  41. <img src="@/assets/knowledgeGraph/aiIcon.png" alt="AI讲解" style="width: 16px; height: 16px;">
  42. <span>AI讲解</span>
  43. </el-button> -->
  44. </span>
  45. </el-tree>
  46. </div>
  47. </template>
  48. <script>
  49. /**
  50. * CommonTree 公共树形组件
  51. *
  52. * 功能说明:
  53. * 1. 基于 Element UI el-tree 二次封装,支持数据传入渲染
  54. * 2. 支持多选复选框
  55. * 3. 节点点击回传、节点勾选回传
  56. * 4. 鼠标悬停节点行显示 "AI讲解" 按钮,点击打印并回传当前节点
  57. *
  58. * 父组件使用示例:
  59. * <CommonTree
  60. * :tree-data="knowledgeTreeData"
  61. * :tree-props="{ label: 'knowledgeName', children: 'children' }"
  62. * node-key="knowledgeId"
  63. * show-checkbox
  64. * @node-click="handleTreeClick"
  65. * @node-check="handleTreeCheck"
  66. * @ai-explain="handleAiExplain"
  67. * />
  68. */
  69. export default {
  70. name: 'CommonTree',
  71. // ==================== Props 配置(由父组件传入) ====================
  72. props: {
  73. /**
  74. * 树形数据源
  75. * 数据结构示例:
  76. * [
  77. * { id: 1, label: '节点1', count: 10, children: [
  78. * { id: 11, label: '子节点1', count: 5 }
  79. * ]}
  80. * ]
  81. */
  82. treeData: {
  83. type: Array,
  84. default: () => []
  85. },
  86. /**
  87. * el-tree 的 props 配置,用于映射数据结构的字段
  88. * 默认:{ label: 'label', children: 'children' }
  89. * 如使用其他字段名,可传:{ label: 'knowledgeName', children: 'childList' }
  90. */
  91. treeProps: {
  92. type: Object,
  93. default: () => ({
  94. label: 'label',
  95. children: 'children'
  96. })
  97. },
  98. /**
  99. * 节点唯一标识字段名
  100. * 用于选中、高亮、展开等操作的定位
  101. */
  102. nodeKey: {
  103. type: String,
  104. default: 'id'
  105. },
  106. /** 是否显示多选复选框 */
  107. showCheckbox: {
  108. type: Boolean,
  109. default: false
  110. },
  111. /** 是否高亮当前点击的节点 */
  112. highlightCurrent: {
  113. type: Boolean,
  114. default: true
  115. },
  116. /** 是否默认展开所有节点 */
  117. defaultExpandAll: {
  118. type: Boolean,
  119. default: true
  120. },
  121. /**
  122. * 默认展开的节点 key 数组
  123. * 与 defaultExpandAll 互斥使用
  124. */
  125. defaultExpandedKeys: {
  126. type: Array,
  127. default: () => []
  128. },
  129. /** 当前高亮选中的节点 key */
  130. currentNodeKey: {
  131. type: [String, Number],
  132. default: ''
  133. },
  134. /**
  135. * 点击节点时是否展开/收起
  136. * false:只有点击小箭头才展开;点击节点文字只触发选中
  137. * true:点击节点文字也会触发展开/收起
  138. */
  139. expandOnClickNode: {
  140. type: Boolean,
  141. default: false
  142. }
  143. },
  144. // ==================== 数据状态 ====================
  145. data() {
  146. return {
  147. // 当前选中的节点数据(单选时使用)
  148. currentSelectedNode: null
  149. };
  150. },
  151. // ==================== 方法 ====================
  152. methods: {
  153. /**
  154. * 树节点点击事件(单选)
  155. * @param {Object} data - 当前节点绑定的用户数据
  156. * @param {Object} node - el-tree 节点对象(包含 level、parent 等信息)
  157. * 触发事件:this.$emit('node-click', data, node)
  158. */
  159. handleNodeClick(data, node) {
  160. this.currentSelectedNode = data;
  161. this.$emit('node-click', data, node);
  162. },
  163. /**
  164. * 树节点勾选事件(多选,showCheckbox=true 时生效)
  165. * @param {Object} data - 当前勾选/取消的节点数据
  166. * @param {Object} checkedInfo - 勾选状态详情
  167. * @param {Array} checkedInfo.checkedNodes - 所有被勾选的节点对象数组
  168. * @param {Array} checkedInfo.checkedKeys - 所有被勾选的节点 key 数组(含半选)
  169. * @param {Array} checkedInfo.halfCheckedKeys - 半选状态的节点 key 数组
  170. */
  171. handleNodeCheck(data, checkedInfo) {
  172. this.$emit('node-check', data, checkedInfo);
  173. },
  174. /**
  175. * AI 讲解按钮点击事件
  176. * 1. 打印当前节点数据(方便调试)
  177. * 2. 通过 $emit 回传给父组件处理
  178. *
  179. * @param {Object} node - el-tree 节点对象
  180. * @param {Object} data - 当前节点绑定的用户数据
  181. */
  182. handleAiExplain(node, data) {
  183. // ===== 打印调试信息 =====
  184. console.log('========== [CommonTree] AI讲解点击 ==========');
  185. console.log('当前节点数据 data:', data);
  186. console.log('当前节点对象 node:', node);
  187. console.log('节点标签 label:', data[this.treeProps.label]);
  188. console.log('节点唯一键 key:', data[this.nodeKey]);
  189. console.log('节点层级 level:', node.level);
  190. console.log('是否叶子节点 isLeaf:', node.isLeaf);
  191. console.log('===============================================');
  192. // ===== 回传给父组件 =====
  193. this.$emit('ai-explain', {
  194. nodeData: data, // 当前节点的用户数据
  195. nodeInstance: node, // el-tree 节点实例
  196. nodeKey: data[this.nodeKey], // 当前节点 key
  197. nodeLabel: data[this.treeProps.label] // 当前节点显示名称
  198. });
  199. },
  200. /**
  201. * 对外暴露方法:获取所有勾选的节点(仅多选模式 showCheckbox=true 有效)
  202. * @param {Boolean} leafOnly - 是否只返回叶子节点
  203. * @returns {Array} 选中的节点对象数组
  204. */
  205. getCheckedNodes(leafOnly = false) {
  206. return this.$refs.commonTree.getCheckedNodes(leafOnly);
  207. },
  208. /**
  209. * 对外暴露方法:获取所有勾选节点的 key 数组
  210. * @param {Boolean} leafOnly
  211. * @returns {Array} 选中的节点 key 数组
  212. */
  213. getCheckedKeys(leafOnly = false) {
  214. return this.$refs.commonTree.getCheckedKeys(leafOnly);
  215. },
  216. /**
  217. * 对外暴露方法:通过 keys 设置哪些节点被选中
  218. * @param {Array} keys - 要选中的节点 key 数组
  219. * @param {Boolean} leafOnly - 是否只设置叶子节点
  220. */
  221. setCheckedKeys(keys, leafOnly = false) {
  222. this.$refs.commonTree.setCheckedKeys(keys, leafOnly);
  223. },
  224. /**
  225. * 对外暴露方法:清空所有选中节点
  226. */
  227. clearChecked() {
  228. this.$refs.commonTree.setCheckedKeys([]);
  229. },
  230. /**
  231. * 对外暴露方法:设置当前高亮的节点
  232. * @param {String|Number} key - 节点 key
  233. */
  234. setCurrentKey(key) {
  235. this.$refs.commonTree.setCurrentKey(key);
  236. }
  237. }
  238. };
  239. </script>
  240. <!--
  241. ==========================================================================
  242. 样式分两块:
  243. 1. 下面这个不带 scoped 的 style 块:专门覆盖 Element UI 原生类名(连接线、padding、间距、选中态)
  244. - scoped + :deep 在 el-tree 深层级嵌套下经常不生效,改用非 scoped 搭配组件唯一根类
  245. .common_tree_wrapper 做命名空间隔离,不会污染全局其他 el-tree
  246. 2. 后面带 scoped 的 style 块:放自定义节点内容(custom_tree_node / node_label / ai_explain_btn 等)
  247. ==========================================================================
  248. -->
  249. <style lang="scss">
  250. /* ============== 可配置常量 ============== */
  251. $line_color: #DCDFE6; // 连接线颜色(虚线)
  252. $line_style: dashed; // 连接线样式
  253. $line_width: 1px; // 连接线宽度
  254. $indent_size: 35px; // 每层缩进
  255. $node_height: 36px; // 单行节点高度
  256. $node_padding_l: 15px; // 节点左侧 padding (用户要求:15px)
  257. $node_gap: 10px; // 节点上下间距 (用户要求:10px)
  258. $label_color: #333333; // 节点文字 (用户要求)
  259. $label_active: #2E64FA; // 选中节点文字 (用户要求)
  260. $label_fs: 14px; // 文字字号 (用户要求)
  261. $label_fw: 400; // 文字字重 (用户要求)
  262. $btn_color: #4F3EFF; // AI讲解按钮颜色 (用户要求)
  263. /* =========================================================
  264. 命名空间:.common_tree_wrapper(只影响本组件)
  265. ========================================================= */
  266. .common_tree_wrapper {
  267. width: 100%;
  268. height: 100%;
  269. box-sizing: border-box;
  270. padding: 8px 0;
  271. /* ===================== 基础 ===================== */
  272. .tree_container,
  273. .el-tree {
  274. width: 100%;
  275. background: transparent;
  276. }
  277. /* ===================== 节点行样式 =====================
  278. padding-left: 15px,上下间距 10px,字体 400/14/#333
  279. */
  280. .el-tree-node__content {
  281. padding-left: 0 !important;
  282. padding-right: 12px;
  283. height: $node_height;
  284. line-height: $node_height;
  285. background: transparent;
  286. border-radius: 4px;
  287. margin-top: $node_gap;
  288. margin-bottom: $node_gap;
  289. font-weight: $label_fw;
  290. font-size: $label_fs;
  291. color: $label_color;
  292. &:hover {
  293. background-color: #f0f7ff;
  294. }
  295. }
  296. /* 第一级也统一 15px */
  297. .el-tree>.el-tree-node>.el-tree-node__content {
  298. padding-left: $node_padding_l !important;
  299. }
  300. /* ===================== 自定义展开收起图标样式 ===================== */
  301. .el-tree-node__expand-icon {
  302. width: 16px;
  303. height: 16px;
  304. display: flex !important;
  305. align-items: center !important;
  306. justify-content: center !important;
  307. cursor: pointer;
  308. margin-right: 8px;
  309. padding: 0 !important;
  310. border: none !important;
  311. /* 去掉Element UI默认的transform旋转过渡动画,直接切换图标 */
  312. transform: none !important;
  313. transition: none !important;
  314. }
  315. /* 去掉展开状态的transform旋转 */
  316. .el-tree-node__expand-icon.expanded {
  317. transform: none !important;
  318. -webkit-transform: none !important;
  319. }
  320. /* 隐藏Element UI默认的箭头伪元素 */
  321. .el-tree-node__expand-icon::before {
  322. display: none !important;
  323. }
  324. /* 收起状态:显示加号 icon_jia */
  325. .el-tree-node__expand-icon:not(.expanded):not(.is-leaf)::after {
  326. content: '\e64e';
  327. font-family: 'iconfont' !important;
  328. font-size: 16px;
  329. color: #E0E3E9;
  330. display: inline-block;
  331. width: 16px;
  332. height: 16px;
  333. line-height: 16px;
  334. text-align: center;
  335. }
  336. /* 展开状态:显示减号 icon_jian */
  337. .el-tree-node__expand-icon.expanded:not(.is-leaf)::after {
  338. content: '\e64f';
  339. font-family: 'iconfont' !important;
  340. font-size: 16px;
  341. color: #2E64FA;
  342. display: inline-block;
  343. width: 16px;
  344. height: 16px;
  345. line-height: 16px;
  346. text-align: center;
  347. }
  348. /* 叶子节点的占位符,保持对齐一致 */
  349. .el-tree-node__expand-icon.is-leaf {
  350. width: 16px !important;
  351. height: 16px !important;
  352. display: inline-block !important;
  353. cursor: default;
  354. visibility: hidden;
  355. margin-right: 8px;
  356. padding: 0 !important;
  357. }
  358. /* ===================== 虚线连接线(核心) ===================== */
  359. /* 1. 竖虚线:画在 children 容器的左侧,贯穿所有子节点 */
  360. .el-tree-node .el-tree-node__children {
  361. overflow: visible;
  362. position: relative;
  363. margin-left: 0;
  364. padding-left: $indent_size; // 层级缩进
  365. &::before {
  366. content: '';
  367. position: absolute;
  368. top: 0;
  369. bottom: $node_gap + $node_height / 2; // 末端停在最后一个节点"半高"处,和横线接上
  370. left: 18px; // 竖线 x 坐标
  371. width: $line_width;
  372. border-left: $line_width $line_style $line_color;
  373. }
  374. }
  375. /* 2. 横虚线:画在每个节点行的左侧,从竖线方向延伸到文字左边 */
  376. .el-tree-node {
  377. position: relative;
  378. >.el-tree-node__content::before {
  379. content: '';
  380. position: absolute;
  381. top: 50%;
  382. left: -($indent_size - 18px); // 起点对齐竖线
  383. width: $indent_size - 20px; // 横线长度
  384. height: 0;
  385. border-top: $line_width $line_style $line_color;
  386. pointer-events: none;
  387. }
  388. }
  389. /* 3. 第一级节点不画横线 */
  390. .el-tree>.el-tree-node>.el-tree-node__content::before {
  391. display: none;
  392. }
  393. /* 4. 最后一个子节点:竖线在横线之下的部分用白色盖掉,形成"L"拐角 */
  394. .el-tree-node>.el-tree-node__children>.el-tree-node:last-child {
  395. >.el-tree-node__content::after {
  396. content: '';
  397. position: absolute;
  398. top: 50%;
  399. bottom: -$node_gap;
  400. left: 18px - $indent_size; // 与竖线同 x
  401. width: $line_width + 2px;
  402. background: #fff; // 白色遮挡(面板必须是白色背景)
  403. z-index: 1;
  404. }
  405. }
  406. /* ===================== 选中 & 复选框 ===================== */
  407. .el-tree-node.is-current>.el-tree-node__content {
  408. background-color: #e6f0ff;
  409. color: $label_active; // 让文字内容(含原生 el-tree label 模式)也变色
  410. /* 额外命中自定义 slot 中的 .node_label,防止 scoped 里写的 #333 覆盖掉选中色 */
  411. .node_label {
  412. color: $label_active !important;
  413. font-weight: $label_fw;
  414. }
  415. }
  416. .el-checkbox {
  417. margin-right: 6px;
  418. }
  419. /* ===================== AI 讲解按钮(Element UI 文字按钮) =====================
  420. 字体 400 / 14 / #4F3EFF;opacity 0 默认隐藏,hover 节点行显示
  421. */
  422. .ai_explain_btn.el-button--text {
  423. display: inline-flex !important;
  424. align-items: center;
  425. justify-content: center;
  426. gap: 4px;
  427. padding: 4px 6px !important;
  428. height: auto !important;
  429. min-height: 24px;
  430. margin-left: 8px;
  431. opacity: 0;
  432. transition: opacity 0.2s ease;
  433. white-space: nowrap;
  434. flex-shrink: 0;
  435. position: relative;
  436. z-index: 3;
  437. font-weight: 400; // 用户要求
  438. font-size: 14px; // 用户要求
  439. color: $btn_color !important; // 用户要求:#4F3EFF
  440. // 确保内部img和span都居中对齐
  441. img {
  442. display: inline-block;
  443. vertical-align: middle;
  444. flex-shrink: 0;
  445. margin-right: 4px;
  446. }
  447. span {
  448. display: inline-block;
  449. vertical-align: middle;
  450. line-height: 1;
  451. }
  452. }
  453. .ai_explain_btn.el-button--text:hover {
  454. color: darken($btn_color, 10%) !important;
  455. }
  456. .ai_explain_btn .ai_icon {
  457. display: inline-block;
  458. width: 16px;
  459. height: 16px;
  460. line-height: 16px;
  461. text-align: center;
  462. font-size: 10px;
  463. font-weight: 600;
  464. color: #fff;
  465. background: $btn_color;
  466. border-radius: 3px;
  467. font-style: normal;
  468. flex-shrink: 0;
  469. }
  470. .ai_explain_btn.el-button--text:hover .ai_icon {
  471. background: darken($btn_color, 10%);
  472. }
  473. /* hover 节点行时,让 AI 讲解按钮淡入 */
  474. .el-tree-node__content:hover .ai_explain_btn.el-button--text {
  475. opacity: 1 !important;
  476. }
  477. }
  478. </style>
  479. <!-- ===============================================================
  480. 以下 scoped 样式块:只处理"自定义节点内容"(我们自己模板里写的)
  481. 类名是自定义的,能被 scoped 正常命中,无需 :deep
  482. =============================================================== -->
  483. <style lang="scss" scoped>
  484. .custom_tree_node {
  485. display: flex;
  486. align-items: center;
  487. justify-content: space-between;
  488. flex: 1;
  489. padding-right: 8px;
  490. height: 36px;
  491. line-height: 36px;
  492. overflow: hidden;
  493. position: relative;
  494. z-index: 2;
  495. min-width: 0; /* flex子项关键:允许收缩到内容宽度以下 */
  496. /* ===== 节点文字(用户要求:400 / 14px / #333333) ===== */
  497. .node_label {
  498. flex: 1;
  499. display: inline-flex;
  500. align-items: center;
  501. font-weight: 400;
  502. font-size: 14px;
  503. color: #333333;
  504. min-width: 0; /* flex子项关键:允许收缩 */
  505. /* 名称文本:唯一承载文字的元素,在这里设置省略号 */
  506. .node_label_text {
  507. display: inline-block;
  508. flex: 1;
  509. min-width: 0; /* 关键:防止flex子项被最小宽度撑开 */
  510. white-space: nowrap;
  511. overflow: hidden;
  512. text-overflow: ellipsis;
  513. line-height: 1;
  514. }
  515. /* 计数(60):灰色小字,不收缩 */
  516. .node_count {
  517. color: #909399;
  518. margin-left: 4px;
  519. font-size: 13px;
  520. flex-shrink: 0;
  521. }
  522. }
  523. }
  524. </style>