angular-tree-component icon indicating copy to clipboard operation
angular-tree-component copied to clipboard

Custom expand/collapse icons

Open musienkoyuriy opened this issue 8 years ago • 8 comments

I need an ability to customize expand/collapse icon, for example, to "plus/minus". Is there this functionality? If no, do you plan to implement it? Also, tricks to realize it will be useful

musienkoyuriy avatar Sep 11 '18 16:09 musienkoyuriy

I also wanted the same thing. I achieved this by CSS. In my global css (styles.css), I overrode class like below:

tree-node-content::before { content: "\f07b" }

html: <tree-root [nodes]="nodes" [focused]="true" (activate)="handleTreeEvents($event)" [options]="options" class="fas">

I am using font-awesome 5. The icon can be implemented by adding class="fas" to tree-root in your html. Check the Unicode in font-awesome page and add it like above.

Little tweaking is needed though.

Krishna47 avatar Oct 07 '18 06:10 Krishna47

You can use custom template for this https://angular2-tree.readme.io/docs/templates#treenodewrappertemplate

Then change tree-node-expander to your custom element. You can use node.toggleExpanded()to toggle state and node?.isExpanded to determine what icon should be displayed.

mark53 avatar Nov 15 '18 11:11 mark53

Issue closed by provided solutions. if this issue still remain, you may open this again.

phhbr avatar Feb 23 '19 09:02 phhbr

I wish the documentation were more clear and complete. I had this same question and I went to the documentation first. I did read the Templates page, but it's not clear (because it's not documented) that this (replacing the expander) can be done. The information posted above by @mark53 helped me figure that out, but it should be documented appropriately.

drullo avatar Jun 07 '19 19:06 drullo

@drullo I still can't customize expander icon. I am trying to change tree-node-wrapper.

    <tree-root #tree [nodes]="tagTree" [options]="treeOptions">
      <ng-template #treeNodeFullTemplate let-node let-index="index" let-templates="templates">
        <div
          [class]="node.getClass()"
          [class.tree-node]="true"
          [class.tree-node-expanded]="node.isExpanded && node.hasChildren"
          [class.tree-conode-collapsed]="node.isCollapsed && node.hasChildren"
          [class.tree-node-leaf]="node.isLeaf"
          [class.tree-node-active]="node.isActive"
          [class.tree-node-focused]="node.isFocused">
          <tree-node-wrapper [node]="node" [index]="index" [templates]="templates"></tree-node-wrapper>
          <tree-node-children [node]="node" [templates]="templates"></tree-node-children>
        </div>
      </ng-template>
      <ng-template #treeNodeWrapperTemplate let-node let-index="index" let-templates="templates">
        <div class="node-wrapper" [style.padding-left]="node.getNodePadding()">
          <tree-node-expander [node]="node">
            <div>
              <fa-icon *ngIf="node?.isCollapsed" [icon]="['fas', 'plus-square']"></fa-icon>
              <fa-icon *ngIf="node?.isExpanded" [icon]="['fas', 'minus-square']"></fa-icon>
            </div>
          </tree-node-expander>
          <div class="node-content-wrapper"
               [class.node-content-wrapper-active]="node.isActive"
               [class.node-content-wrapper-focused]="node.isFocused"
               (click)="node.mouseAction('click', $event)"
               (dblclick)="node.mouseAction('dblClick', $event)"
               (contextmenu)="node.mouseAction('contextMenu', $event)"
               (treeDrop)="node.onDrop($event)"
               [treeAllowDrop]="node.allowDrop"
               [treeDrag]="node"
               [treeDragEnabled]="node.allowDrag()">

            <tree-node-content [node]="node" [index]="index" [template]="treeNodeTemplate"></tree-node-content>
          </div>
        </div>
      </ng-template>
      <ng-template #treeNodeTemplate let-node let-index="index">
        <div>
          <span> {{ node.data.tagName }}</span>
        </div>
      </ng-template>
    </tree-root>

sagunpandey avatar Sep 05 '19 16:09 sagunpandey

I have figured it out. Here my updated treeNodeWrapperTemplate.

      <ng-template #treeNodeWrapperTemplate let-node let-index="index" let-templates="templates">
        <div style="display: flex; align-items: center;" class="node-wrapper" [style.padding-left]="node.getNodePadding()">
          <div style="color: #6C757D;" (click)="node.toggleExpanded()" *ngIf="node?.data?.children?.length > 0">
            <fa-icon *ngIf="node?.isCollapsed" [icon]="['fas', 'plus-square']"></fa-icon>
            <fa-icon *ngIf="node?.isExpanded" [icon]="['fas', 'minus-square']"></fa-icon>
          </div>
          <div class="node-content-wrapper"
               [class.node-content-wrapper-active]="node.isActive"
               [class.node-content-wrapper-focused]="node.isFocused"
               (click)="node.mouseAction('click', $event)"
               (dblclick)="node.mouseAction('dblClick', $event)"
               (contextmenu)="node.mouseAction('contextMenu', $event)"
               (treeDrop)="node.onDrop($event)"
               [treeAllowDrop]="node.allowDrop"
               [treeDrag]="node"
               [treeDragEnabled]="node.allowDrag()">

            <tree-node-content [node]="node" [index]="index" [template]="treeNodeTemplate"></tree-node-content>
          </div>
        </div>
      </ng-template>

sagunpandey avatar Sep 05 '19 16:09 sagunpandey

@sagunpandey , one thing I changed from your example is using *ngIf="node.hasChildren" instead of *ngIf="node?.data?.children?.length > 0".

This way it will work even if you have overwritten the default key for children (mine was "subFolders") and also if you use async loading and the "node.data.children" is empty, but actually has data when expanded.

miroslavgrozdanovski avatar Oct 24 '19 14:10 miroslavgrozdanovski

Is there a way to do this while still using #treeNodeFullTemplate?

The issue I'm having is that I'm using the full template for the tree display, but this override doesn't include <tree-node-expander> like it does in #treeNodeWrapperTemplate

westongillette avatar Jun 03 '22 21:06 westongillette