Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind up and down arrow key to enable navigating through table rows?

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>&nbsp;</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>
like image 999
Ray Cheng Avatar asked Jun 06 '13 19:06

Ray Cheng


People also ask

What is the use of down arrow key in a table?

To navigate to the Table Row Actions: Use the keyboard keys to navigate through the page elements. Use the Down Arrow key to move focus to the table rows.

What is alternate key for down arrow?

press and hold down the Alt key, type the Alt Code value of the arrow you want, for example for an arrow down symbol, type 2 5 on the numeric pad , release the Alt key and you got a ↓ downwards arrow.

Why can't I use my arrow keys?

Turn off Scroll Lock on your keyboard Most of the time, if your arrow keys aren't moving the cursor from cell to cell, the fix is as simple as disabling the Scroll Lock key on your keyboard.


2 Answers

I think this is what you are looking for.

var myModel = new function () {
        var self = this;
        self.selectResult = function (item) {
            self.selectedResult(item);
        };
        self.selectedResult = ko.observable();
        self.searchResults = ko.observableArray([{
            data1: "1",
            data2: "2"
        }, {
            data1: "2",
            data2: "2"
        }, {
            data1: "3",
            data2: "2"
        }]);


        self.selectPrevious = function () {
            var index = self.searchResults().indexOf(self.selectedResult()) - 1;
            if (index < 0) index = 0;
            self.selectedResult(self.searchResults()[index]);
        };

        self.selectNext = function () {
            var index = self.searchResults().indexOf(self.selectedResult()) + 1;
            if (index >= self.searchResults().length) index = self.searchResults().length - 1;
            self.selectedResult(self.searchResults()[index]);
        };


    };
ko.applyBindings(myModel);

$(window).keyup(function (evt) {
    if (evt.keyCode == 38) {
        myModel.selectPrevious();
    } else if (evt.keyCode == 40) {
        myModel.selectNext();
    }
});

See Fiddle

I hope it helps.

like image 189
Damien Avatar answered Oct 15 '22 03:10

Damien


It's actually not that hard to find next and prev

For example

$(window).keyup(function (evt) {
    if (evt.keyCode == 38) { // up
       $('tbody tr:not(:first).selected').removeClass('selected').prev().addClass('selected')
    }
    if (evt.keyCode == 40) { // down
       $('tbody tr:not(:last).selected').removeClass('selected').next().addClass('selected')
    }
});

FIDDLE

like image 45
Spokey Avatar answered Oct 15 '22 04:10

Spokey