I'm displaying search results with a table. Each result has a button for user to click on to show the full detail of it. That's working well.
What I also want is to make it possible to navigat the search results by using keyboard's up and down arrow.
Now, user have to click on the Select
button or tab to the button and press space bar
.
I suppose I can trap the keyup and down event and then find the previous or next one I need to select and then set it, but it sounds like a lot of work. I wonder if there's a better way to do it?
javascript
var myModel = new function() {
var self = this;
self.selectedResult = ko.observable(new MyObj());
self.searchResults = ko.observableArray();
self.navUpDown = function (item, evt) {
if (evt.keyCode == 38) { // up
var id = item.ID();
// find previous one and set selectedResult
}
if (evt.keyCode == 40) { // down
var id = item.ID();
// find next one and set selectedResult
}
};
};
html
<table class="table">
<thead>
<tr>
<th> </th>
<th>table header 1</th>
<th>table header 2</th>
</tr>
</thead>
<tbody data-bind="foreach: searchResults">
<tr data-bind="css: { 'btn-info': $parent.selectedResult() == $data }, event: { keyup: $parent.navUpDown } ">
<td>
<button class="btn btn-small" data-bind="click: $parent.selectResult">Select</button>
</td>
<td data-bind="text: data1"></td>
<td data-bind="text: data2"></td>
</tr>
</tbody>
</table>
It's actually not that hard to find
next
andprev
For example
FIDDLE
I think this is what you are looking for.
See Fiddle
I hope it helps.