JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://github.com/downloads/SteveSanderson/knockout/knockout-1.3.0beta.js"></script>
<h3>Products</h3>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th/>
</tr>
</thead>
<tbody data-bind="foreach: contacts">
<tr class="edit" data-bind="ifnot: editable">
<td data-bind="text: firstName"/>
<td data-bind="text: lastName"/>
<td data-bind="text: age"/>
</tr>
<tr data-bind="with: editable">
<td><input data-bind="value: firstName"/></td>
<td><input data-bind="value: lastName"/></td>
<td><input data-bind="value: age"/></td>
<td>
<button class="save">Save</button>
<button class="cancel">Cancel</button>
</td>
</tr>
</tbody>
</table>
CSS
.edit:hover {
background-color: #ffffcc;
}
.edit:active {
background-color: #ccffcc;
}
JavaScript
function appViewModel() {
this.contacts = ko.observableArray([
{
firstName: ko.observable("John"),
lastName: ko.observable("Smith"),
age: ko.observable(31),
editable: ko.observable(null)
},
{
firstName: ko.observable("Alice"),
lastName: ko.observable("Fox"),
age: ko.observable(31),
editable: ko.observable(null)
},
{
firstName: ko.observable("Michael"),
lastName: ko.observable("Wilkes"),
age: ko.observable(33),
editable: ko.observable(null)
},
]);
}
$('.edit').live('click', function() {
data = ko.dataFor(this);
var editable = {
firstName: ko.observable(data.firstName()),
lastName: ko.observable(data.lastName()),
age: ko.observable(data.age())
}
console.log(editable);
data.editable(editable);
});
$('.save').live('click', function() {
context = ko.contextFor(this);
context.$parent.firstName(context.$data.firstName());
context.$parent.lastName(context.$data.lastName());
context.$parent.age(context.$data.age());
context.$parent.editable(false);
});
$('.cancel').live('click', function() {
context = ko.contextFor(this);
context.$parent.editable(false);
});
ko.applyBindings(new appViewModel());