JSFiddle - React, Tailwind, and code Playground
HTML
<ul data-bind="foreach: items">
<li><span data-bind="text: id"></span>
<span data-bind="visible: showName">Yes! show the name</span>
<a href="#" data-bind="click: $root.removePerson">Remove</a>
</li>
</ul>
JavaScript
var Item = function(data) {
var self = this;
self.id = data.id;
self.name = ko.observable(data.name);
// or plain "self.name = data.name;" if you don't need 2way binding
self.showName = ko.computed(function() {
return self.name() && self.name().length > 0;
});
}
var data = [{ id: 1, name: "John Doe" }, { id: 2, name: ""}, { id: 3, name: "Peter Parker"}];
var viewModel = {
items: ko.observableArray(data.map(function(item) { return new Item(item); }))
};
viewModel.removePerson = function (person) {
console.log(person);
};
ko.applyBindings(viewModel);