tree-select.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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:not(.leaf)[data-level="0"]::before,
  36. .tree-node:not(.leaf)[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:is(.leaf)[data-level="0"]::before {
  49. left: 14px;
  50. }
  51. .tree-node:is(.leaf)[data-level="0"]::after {
  52. left: 14px;
  53. }
  54. .tree-node:is(.leaf)[data-level="0"] {
  55. padding-left: 22px;
  56. }
  57. .tree-node:last-child::before {
  58. height: 14px;
  59. }
  60. .tree-node.leaf {
  61. cursor: pointer;
  62. color: #007bff;
  63. background-color: #fff;
  64. }
  65. .tree-node.leaf:hover {
  66. background-color: #e9ecef;
  67. }
  68. .tree-label {
  69. display: inline-block;
  70. vertical-align: middle;
  71. }
  72. .tree-toggle {
  73. display: inline-block;
  74. width: 14px;
  75. text-align: center;
  76. margin-right: 6px;
  77. user-select: none;
  78. font-size: 12px;
  79. vertical-align: middle;
  80. }
  81. .tree-children {
  82. display: none;
  83. margin-left: 18px;
  84. padding-left: 8px;
  85. }
  86. .tree-expanded > .tree-children {
  87. display: block;
  88. }
  89. .tree-select button {
  90. color: #495057;
  91. border: 1px solid #ced4da;
  92. padding: .25rem .5rem;
  93. }
  94. .tree-select button:active {
  95. color: #6c757d !important;
  96. border: 1px solid #ced4da;
  97. background-color: #fff !important;
  98. }
  99. #tree-container .show>.btn-outline-secondary.dropdown-toggle {
  100. color: #6c757d !important;
  101. border: 1px solid #80bdff;
  102. background-color: #fff !important;
  103. box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  104. }
  105. #tree-container .show>.btn-outline-secondary.dropdown-toggle:focus {
  106. border-color: #80bdff;
  107. box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  108. }
  109. .tree-select .btn-outline-secondary:hover {
  110. color: #6c757d;
  111. border: 1px solid #ced4da;
  112. background-color: #fff;
  113. }
  114. .tree-select .btn-outline-secondary:focus {
  115. border-color: #80bdff;
  116. box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  117. }
  118. .tree-select .dropdown-toggle {
  119. position: relative;
  120. padding-right: 2rem; /* 给箭头留空间 */
  121. text-align: left; /* 让文字靠左 */
  122. }
  123. .tree-select .dropdown-toggle::after {
  124. position: absolute;
  125. right: 0.75rem; /* 你可以根据需要微调 */
  126. top: 50%;
  127. transform: translateY(-50%);
  128. }
  129. `;
  130. document.head.appendChild(style);
  131. class TreeSelect {
  132. constructor({ element, data, selected, onSelect }) {
  133. this.container = typeof element === 'string' ? document.querySelector(element) : element;
  134. this.onSelect = onSelect;
  135. this.selectedId = selected;
  136. this.data = this.buildTree(data);
  137. this.selectedNode = this.findNodeById(this.data, this.selectedId);
  138. this.render();
  139. }
  140. // ✅ flat → tree 转换函数
  141. buildTree(data, parentId = '-1') {
  142. // 找出所有属于 parentId 的节点
  143. const children = data
  144. .filter(item => item.tree_pid === parentId)
  145. .sort((a, b) => a.tree_order - b.tree_order); // ✅ 按 tree_order 升序排序
  146. return children.map(item => {
  147. const nodeChildren = this.buildTree(data, item.id);
  148. return {
  149. id: item.id,
  150. label: item.name,
  151. selected: item.id === this.selectedId,
  152. children: nodeChildren.length ? nodeChildren : undefined
  153. };
  154. });
  155. }
  156. findNodeById(nodes, id) {
  157. for (const node of nodes) {
  158. if (node.id === id) return node;
  159. if (node.children) {
  160. const found = this.findNodeById(node.children, id);
  161. if (found) return found;
  162. }
  163. }
  164. return null;
  165. }
  166. render() {
  167. this.container.innerHTML = `
  168. <div class="dropdown tree-select">
  169. <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">
  170. ${this.selectedNode ? this.selectedNode.label : '请选择分类'}
  171. </button>
  172. <div class="dropdown-menu tree-select-menu"></div>
  173. </div>
  174. `;
  175. this.button = this.container.querySelector('button');
  176. this.menu = this.container.querySelector('.dropdown-menu');
  177. this.button.dataset.selectedId = this.selectedNode?.id || '';
  178. this.renderTree(this.data, this.menu, 0);
  179. }
  180. renderTree(nodes, parentEl, level = 0) {
  181. nodes.forEach(node => {
  182. const wrapper = document.createElement('div');
  183. wrapper.className = 'tree-node tree-expanded'; // 默认展开
  184. wrapper.dataset.level = level;
  185. const isLeaf = !node.children || node.children.length === 0;
  186. if (isLeaf) wrapper.classList.add('leaf');
  187. const toggle = document.createElement('span');
  188. toggle.className = 'tree-toggle';
  189. toggle.innerHTML = isLeaf ? '' : '<i class="fa fa-minus-square-o"></i>';
  190. if (!isLeaf) {
  191. wrapper.appendChild(toggle);
  192. const toggle2 = document.createElement('span');
  193. toggle2.className = 'tree-folder';
  194. toggle2.innerHTML = '<i class="fa fa-folder-o"></i> ';
  195. wrapper.appendChild(toggle2);
  196. }
  197. const label = document.createElement('span');
  198. label.className = 'tree-label';
  199. label.innerHTML = isLeaf ? '&nbsp; ' + node.label : node.label;
  200. wrapper.appendChild(label);
  201. if (!isLeaf) {
  202. const childrenContainer = document.createElement('div');
  203. childrenContainer.className = 'tree-children';
  204. wrapper.appendChild(childrenContainer);
  205. this.renderTree(node.children, childrenContainer, level + 1);
  206. if (this.isNodeOrChildSelected(node)) {
  207. wrapper.classList.add('tree-expanded');
  208. toggle.innerHTML = '<i class="fa fa-minus-square-o"></i>';
  209. }
  210. // 👇 点击整行展开/收起
  211. wrapper.addEventListener('click', e => {
  212. e.stopPropagation();
  213. const expanded = wrapper.classList.toggle('tree-expanded');
  214. toggle.innerHTML = expanded ? '<i class="fa fa-minus-square-o"></i>' : '<i class="fa fa-plus-square-o"></i>';
  215. });
  216. } else {
  217. // 👇 叶子节点可选中
  218. wrapper.addEventListener('click', e => {
  219. e.stopPropagation();
  220. this.selectNode(node);
  221. });
  222. }
  223. parentEl.appendChild(wrapper);
  224. });
  225. }
  226. isNodeOrChildSelected(node) {
  227. if (node.selected) return true;
  228. if (!node.children) return false;
  229. return node.children.some(child => this.isNodeOrChildSelected(child));
  230. }
  231. selectNode(node) {
  232. this.selectedNode = node;
  233. this.button.textContent = node.label;
  234. this.button.dataset.selectedId = node.id;
  235. $('.tree-select button').dropdown('toggle');
  236. if (typeof this.onSelect === 'function') {
  237. this.onSelect(node.id, node.label);
  238. }
  239. }
  240. getValue() {
  241. return this.button.dataset.selectedId || null;
  242. }
  243. }
  244. window.TreeSelect = TreeSelect;
  245. })();