The problem is that the row selection handles the mouse click, so we want to ensure that we don't allow the click event to propagate to the row selection handler, which we can do using the method event.stopPropagation
.
To get this working, I first changed the ItemViewModel
constructor to perform the actual navigation.
function ItemViewModel(name, age) {
var self = this;
self.name = name;
self.age = age;
self.ageUrl = "/Home/Index/" + self.age;
function navigateTo(url){
// Before navigation we want to stop propagation of the event to avoid
// other handlers to handle the click and replace the url (this will
// ensure the row selection isn't triggered by clicking the age link)
event.stopPropagation();
window.location.href = url;
}
self.navigateToName = function(){
navigateTo("/Home/Index?Name=" + self.name);
};
self.navigateToAge = function(){
navigateTo(self.ageUrl);
};
};
Then I updated your cell template to use properties and methods on the ItemViewModel
object.
cellTemplate: "<a data-bind='click: $parent.entity.navigateToAge, attr: {href: $parent.entity.ageUrl}, text: $parent.entity.age'></a>"
And finally also updated the row selection handler to use methods on the ItemViewModel
object.
afterSelectionChange: function (rowItem, event) {
if (event.type == 'click') {
rowItem.entity.navigateToName();
}
}
After doing these changes everything worked well for me (if I put it in a custom html page, since jsfiddle isn't very keen on navigations).