tree-select.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. (() => {
  2. // 注入样式
  3. const style = document.createElement('style');
  4. style.textContent = `
  5. .tree-select-menu {
  6. max-height: 500px;
  7. overflow-y: auto;
  8. min-width: 250px;
  9. font-family: Arial, sans-serif;
  10. font-size: 12px;
  11. user-select: none;
  12. border: 1px solid #dee2e6;
  13. padding-left: 4px;
  14. }
  15. .tree-node {
  16. cursor: default;
  17. padding: 4px 8px;
  18. display: block;
  19. white-space: nowrap;
  20. position: relative;
  21. text-align: left;
  22. }
  23. .tree-node::before {
  24. content: '';
  25. position: absolute;
  26. top: 0;
  27. left: 0px;
  28. bottom: 0;
  29. width: 1px;
  30. border-left: 1px dashed #ccc;
  31. }
  32. .tree-node:not(.leaf) [data-level="0"] >.tree-label {
  33. font-weight: bold;
  34. }
  35. .tree-node[data-level="0"]::before,
  36. .tree-node[data-level="0"]::after {
  37. display: none !important;
  38. }
  39. .tree-node::after {
  40. content: '';
  41. position: absolute;
  42. top: 14px;
  43. left: 0px;
  44. width: 10px;
  45. height: 1px;
  46. background-color: #ccc;
  47. }
  48. .tree-node:last-child::before {
  49. height: 14px;
  50. }
  51. .tree-node.leaf {
  52. cursor: pointer;
  53. color: #007bff;
  54. background-color: #fff;
  55. }
  56. .tree-node.leaf:hover {
  57. background-color: #e9ecef;
  58. }
  59. .tree-label {
  60. display: inline-block;
  61. vertical-align: middle;
  62. }
  63. .tree-toggle {
  64. display: inline-block;
  65. width: 14px;
  66. text-align: center;
  67. margin-right: 6px;
  68. user-select: none;
  69. font-size: 12px;
  70. vertical-align: middle;
  71. }
  72. .tree-children {
  73. display: none;
  74. margin-left: 18px;
  75. padding-left: 8px;
  76. }
  77. .tree-expanded > .tree-children {
  78. display: block;
  79. }
  80. .tree-select button {
  81. color: #495057;
  82. border: 1px solid #ced4da;
  83. padding: .25rem .5rem;
  84. }
  85. .tree-select button:active {
  86. color: #6c757d !important;
  87. border: 1px solid #ced4da;
  88. background-color: #fff !important;
  89. }
  90. #tree-container .show>.btn-outline-secondary.dropdown-toggle {
  91. color: #6c757d !important;
  92. border: 1px solid #80bdff;
  93. background-color: #fff !important;
  94. box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  95. }
  96. #tree-container .show>.btn-outline-secondary.dropdown-toggle:focus {
  97. border-color: #80bdff;
  98. box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  99. }
  100. .tree-select .btn-outline-secondary:hover {
  101. color: #6c757d;
  102. border: 1px solid #ced4da;
  103. background-color: #fff;
  104. }
  105. .tree-select .btn-outline-secondary:focus {
  106. border-color: #80bdff;
  107. box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  108. }
  109. `;
  110. document.head.appendChild(style);
  111. class TreeSelect {
  112. constructor({ element, data, selected, onSelect }) {
  113. this.container = typeof element === 'string' ? document.querySelector(element) : element;
  114. this.onSelect = onSelect;
  115. this.selectedId = selected;
  116. this.data = this.buildTree(data);
  117. this.selectedNode = this.findNodeById(this.data, this.selectedId);
  118. this.render();
  119. }
  120. // ✅ flat → tree 转换函数
  121. buildTree(data, parentId = '-1') {
  122. // 找出所有属于 parentId 的节点
  123. const children = data
  124. .filter(item => item.tree_pid === parentId)
  125. .sort((a, b) => a.tree_order - b.tree_order); // ✅ 按 tree_order 升序排序
  126. return children.map(item => {
  127. const nodeChildren = this.buildTree(data, item.id);
  128. return {
  129. id: item.id,
  130. label: item.name,
  131. selected: item.id === this.selectedId,
  132. children: nodeChildren.length ? nodeChildren : undefined
  133. };
  134. });
  135. }
  136. findNodeById(nodes, id) {
  137. for (const node of nodes) {
  138. if (node.id === id) return node;
  139. if (node.children) {
  140. const found = this.findNodeById(node.children, id);
  141. if (found) return found;
  142. }
  143. }
  144. return null;
  145. }
  146. render() {
  147. this.container.innerHTML = `
  148. <div class="dropdown tree-select">
  149. <button class="btn btn-sm btn-outline-secondary w-100 dropdown-toggle text-left" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  150. ${this.selectedNode ? this.selectedNode.label : '请选择分类'}
  151. </button>
  152. <div class="dropdown-menu tree-select-menu"></div>
  153. </div>
  154. `;
  155. this.button = this.container.querySelector('button');
  156. this.menu = this.container.querySelector('.dropdown-menu');
  157. this.button.dataset.selectedId = this.selectedNode?.id || '';
  158. this.renderTree(this.data, this.menu, 0);
  159. }
  160. renderTree(nodes, parentEl, level = 0) {
  161. nodes.forEach(node => {
  162. const wrapper = document.createElement('div');
  163. wrapper.className = 'tree-node tree-expanded'; // 默认展开
  164. wrapper.dataset.level = level;
  165. const isLeaf = !node.children || node.children.length === 0;
  166. if (isLeaf) wrapper.classList.add('leaf');
  167. const toggle = document.createElement('span');
  168. toggle.className = 'tree-toggle';
  169. toggle.innerHTML = isLeaf ? '' : '<i class="fa fa-minus-square-o"></i>';
  170. if (!isLeaf) {
  171. wrapper.appendChild(toggle);
  172. const toggle2 = document.createElement('span');
  173. toggle2.className = 'tree-folder';
  174. toggle2.innerHTML = '<i class="fa fa-folder-o"></i> ';
  175. wrapper.appendChild(toggle2);
  176. }
  177. const label = document.createElement('span');
  178. label.className = 'tree-label';
  179. label.innerHTML = isLeaf ? '&nbsp; ' + node.label : node.label;
  180. wrapper.appendChild(label);
  181. if (!isLeaf) {
  182. const childrenContainer = document.createElement('div');
  183. childrenContainer.className = 'tree-children';
  184. wrapper.appendChild(childrenContainer);
  185. this.renderTree(node.children, childrenContainer, level + 1);
  186. if (this.isNodeOrChildSelected(node)) {
  187. wrapper.classList.add('tree-expanded');
  188. toggle.innerHTML = '<i class="fa fa-minus-square-o"></i>';
  189. }
  190. // 👇 点击整行展开/收起
  191. wrapper.addEventListener('click', e => {
  192. e.stopPropagation();
  193. const expanded = wrapper.classList.toggle('tree-expanded');
  194. toggle.innerHTML = expanded ? '<i class="fa fa-minus-square-o"></i>' : '<i class="fa fa-plus-square-o"></i>';
  195. });
  196. } else {
  197. // 👇 叶子节点可选中
  198. wrapper.addEventListener('click', e => {
  199. e.stopPropagation();
  200. this.selectNode(node);
  201. });
  202. }
  203. parentEl.appendChild(wrapper);
  204. });
  205. }
  206. isNodeOrChildSelected(node) {
  207. if (node.selected) return true;
  208. if (!node.children) return false;
  209. return node.children.some(child => this.isNodeOrChildSelected(child));
  210. }
  211. selectNode(node) {
  212. this.selectedNode = node;
  213. this.button.textContent = node.label;
  214. this.button.dataset.selectedId = node.id;
  215. $('.tree-select button').dropdown('toggle');
  216. if (typeof this.onSelect === 'function') {
  217. this.onSelect(node.id, node.label);
  218. }
  219. }
  220. getValue() {
  221. return this.button.dataset.selectedId || null;
  222. }
  223. }
  224. window.TreeSelect = TreeSelect;
  225. })();