Created
March 16, 2016 21:23
-
-
Save Scapal/4e3dd477f1fd9c7d7a4e to your computer and use it in GitHub Desktop.
Aurelia Bootstrap Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.treeview .list-group-item { | |
cursor: pointer; | |
} | |
.treeview span.indent { | |
margin-left: 10px; | |
margin-right: 10px; | |
} | |
.treeview span.icon { | |
width: 12px; | |
margin-right: 5px; | |
} | |
.treeview .node-disabled { | |
color: silver; | |
cursor: not-allowed; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<require from="./tree-node.css"></require> | |
<li if.bind="visible" class="list-group-item treeview ${selectedNode == data?'active':''}" click.delegate="selectedNode = data"> | |
<span class="indent" repeat.for="i of level"></span> | |
<span if.bind="data.children" class="icon glyphicon ${childrenVisible?'glyphicon-triangle-bottom':'glyphicon-triangle-right'}" click.delegate="toggleExpand()"></span> | |
<span if.bind="!data.children" class="icon glyphicon"></span> | |
${data.name}<span if.bind="!childrenVisible && itemCount != 0" class="badge" click.delegate="toggleExpand()">${itemCount}</span> | |
</li> | |
<tree-node if.bind="visible" repeat.for="node of data.children" data.bind="node" level.bind="level + 1" visible.bind="childrenVisible" max-level.bind="maxLevel" selected-node.bind="selectedNode"></tree-node> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {computedFrom, bindable, bindingMode, containerless} from 'aurelia-framework'; | |
@containerless | |
export class treeNode { | |
@bindable data; | |
@bindable level; | |
@bindable ({attribute: 'selected-node', defaultBindingMode: bindingMode.twoWay}) selectedNode; | |
@bindable ({attribute: 'visible', defaultBindingMode: bindingMode.twoWay}) visible = true; | |
@bindable maxLevel = 2; | |
childrenVisible = false; | |
@computedFrom('data') | |
get itemCount() { | |
return this.countNodes(this.data); | |
} | |
attached() { | |
this.childrenVisible = this.level < this.maxLevel; | |
} | |
countNodes(node_item) { | |
let count = 0; | |
if(node_item.children !== undefined) | |
node_item.children.forEach( (node_child) => count += this.countNodes(node_child) + 1); | |
return count; | |
} | |
visibleChanged(newValue) { | |
if(newValue = false) { | |
this.childrenVisible = false; | |
} | |
} | |
toggleExpand() { | |
this.childrenVisible = !this.childrenVisible; | |
} | |
} |
Do you mind creating a full demo as I don't seem to be able to get this to work.
Thanks
Not to worry, have just got it to work.
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I already did it. It was easier than I thought. Though I am still learning aurelia, so I am going a bit slow.
Best regards, and thanks for the excellent component.
Cristián