I using this method on my view model to switch between the normal template and the editable template of table rows:
contactsViewModel.templateToUse = function (contact) {
return contactsViewModel.selectedItem() === contact ? 'contacts-editTmpl' : 'contacts-itemsTmpl';
};
so instead of template name I call this method:
<tbody data-bind="template: { name: templateToUse, foreach: filterItems }"></tbody>
It works but I have serious concerns over it rendering a whole list again everytime I hit edit:
contactsViewModel.edit = function (contact) {
contactsViewModel.selectedItem(contact);
};
I would like it to only change the template for the selectedItem and not the rest, but not sure how to go about it.
Thanks for helping
One option is to just use
foreachon yourfilteredItemsand then use thetemplatebinding on each row with row-level templates.Something like:
view model like:
Sample: http://jsfiddle.net/rniemeyer/3rvTA/
Another option would be to move the “selected” observable to the item’s themselves. This would work well if you wanted to be able to put multiple rows into edit mode.