Created
November 14, 2013 09:10
-
-
Save nasum/7463776 to your computer and use it in GitHub Desktop.
2013-11-14 1st
This file contains 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
#Knockout.jsで開閉する階層リスト | |
Knockout.jsで開閉する階層リストを作りました。 |
This file contains 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
@charset "UTF-8"; | |
h1{ | |
font-size : 15pt; | |
font-family : 'ヒラギノ角ゴ Pro W3','Hiragino Kaku Gothic Pro','メイリオ',Meiryo,'MS Pゴシック',sans-serif; | |
} | |
ul{ | |
font-family : 'ヒラギノ角ゴ Pro W3','Hiragino Kaku Gothic Pro','メイリオ',Meiryo,'MS Pゴシック',sans-serif; | |
li { | |
cursor : pointer; | |
} | |
} |
This file contains 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
<h1> | |
Knockout.jsで開閉する階層リスト | |
</h1> | |
<!-- リストのルート --> | |
<ul data-bind="template: { name: 'nodeTmpl', foreach: root.nodes }"></ul> | |
<!-- ノードのテンプレート --> | |
<script id="nodeTmpl" type="text/html"> | |
<li data-bind="visible : state, click : $root.open, clickBubble: false"> | |
<span data-bind="text: name"></span> | |
<ul data-bind="template: { name: 'nodeTmpl', foreach: nodes }"></ul> | |
</li> | |
</script> |
This file contains 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
/** | |
ノードのモデル | |
**/ | |
var Node = function(name, nodes, depth) { | |
//ノードの名前 | |
this.name = ko.observable(name); | |
//他のノード | |
this.nodes = ko.observableArray(nodes); | |
//ノードの深さ | |
this.depth = depth; | |
//開いているか閉じているか | |
this.state = ko.observable(false); | |
}; | |
/** | |
ノードを格納するビューモデル | |
**/ | |
function viewModel(){ | |
var self = this; | |
//ツリーに表示するノード | |
self.root = new Node("root", [ | |
new Node("Android", [ | |
new Node("Panasonic",[],3), | |
new Node("Sony", [ | |
new Node("Xperia",[],3), | |
new Node("Xperia HD",[],3) | |
],2) | |
],1).state(true), | |
new Node("iOS", [ | |
new Node("iPhone",[],2), | |
new Node("iPad",[],2) | |
],1).state(true) | |
],0).state(true); | |
//閉じたり開いたりする関数 | |
self.open = function(place){ | |
console.log(place.name()); | |
for(var i = 0; i < place.nodes().length; i++){ | |
var state = place.nodes()[i].state(); | |
place.nodes()[i].state(!state); | |
} | |
}; | |
return { | |
root : self.root, | |
open : self.open | |
}; | |
} | |
/** | |
バインディングの実行 | |
**/ | |
ko.applyBindings(new viewModel()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment