KnockoutJS if statement inside foreach loop
http://stackoverflow.com/questions/9177814/knockoutjs-if-statement-inside-foreach-loop
by rniemeyer
HTML
<script src="http://knockoutjs.com/downloads/knockout-2.0.0.debug.js"></script>
<table>
<tbody data-bind="foreach: entries">
<tr>
<td>
<!-- ko if: type === 'file' -->
<i class="icon-file"></i>
<a href="#" data-bind="text: name, click: $parent.showFile"></a>
<!-- /ko -->
<!-- ko if: type !== 'file' -->
<i class="icon-folder"></i>
<a href="#" data-bind="text: name, click: $parent.goToPath"></a>
<!-- /ko -->
</td>
</tr>
</tbody>
</table>
JavaScript
var ViewModel = function() {
var self = this;
this.entries = [
{ name: "one", type: 'file' },
{ name: "two", type: 'folder' },
{ name: "three", type: 'file'}
];
this.showFile = function(file) {
alert("show file: " + file.name);
};
this.goToPath = function(path) {
alert("go to path: " + path.name);
};
};
ko.applyBindings(new ViewModel());