"[SOLVED] p:dataTable row selection with arrow keys?

UI Components for JSF
Post Reply
JeeDev_User
Posts: 100
Joined: 26 Sep 2019, 11:02

29 Sep 2019, 16:23

Hi all,
There is various ways to select row in dataTable, as described here:
https://www.primefaces.org/showcase/ui/ ... tion.xhtml

However, I would like to know is there a way to select row by scrolling in the table, by using up and down arrow key?
Last edited by JeeDev_User on 01 Oct 2019, 18:25, edited 1 time in total.

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

30 Sep 2019, 16:11

Yes go here: https://www.primefaces.org/showcase/ui/ ... tion.xhtml

Look at the "Single with Row Click" example which is the 2nd table down.

1. Click on a row to select it.

2. Now press Up and Down arrow keys to move rows.

3. Press SPACEBAR to select a new row.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

JeeDev_User
Posts: 100
Joined: 26 Sep 2019, 11:02

01 Oct 2019, 08:47

Thanks for response.
I saw this.
But this is not what I want.
I want for row to become current _without pressing SPACEBAR.
In other words, the row should become current immediately after the pressing Up (or Down ) arrow, without need to press SPACEBAR , or anything else.

Is it possible?

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

01 Oct 2019, 13:09

Yep just have to override the javascript to perform a rowClick selection event when you press Arrow Up or Down. Just copy and paste this script to your page should do it.

Code: Select all

PrimeFaces.widget.DataTable.prototype.bindSelectionKeyEvents = function() {
        var $this = this;

        this.getFocusableTbody().on('focus', function(e) {
            //ignore mouse click on row
            if(!$this.mousedownOnRow) {
                $this.focusedRow = $this.tbody.children('tr.ui-widget-content.ui-datatable-selectable.ui-state-highlight').eq(0);
                if ($this.focusedRow.length == 0) {
                    $this.focusedRow = $this.tbody.children('tr.ui-widget-content.ui-datatable-selectable').eq(0);
                }

                $this.highlightFocusedRow();

                if($this.cfg.scrollable) {
                    PrimeFaces.scrollInView($this.scrollBody, $this.focusedRow);
                }
            }
        })
        .on('blur', function() {
            if($this.focusedRow) {
                $this.unhighlightFocusedRow();
                $this.focusedRow = null;
            }
        })
        .on('keydown', function(e) {
            var keyCode = $.ui.keyCode,
            key = e.which;

            if($(e.target).is(':input') && $this.cfg.editable) {
                return;
            }

            if($this.focusedRow) {
                switch(key) {
                    case keyCode.UP:
                    case keyCode.DOWN:
                        var rowSelector = 'tr.ui-widget-content.ui-datatable-selectable',
                        row = key === keyCode.UP ? $this.focusedRow.prev(rowSelector) : $this.focusedRow.next(rowSelector);

                        if(row.length) {
                            $this.unhighlightFocusedRow();

                            if($this.isCheckboxSelectionEnabled()) {
                                row.find('> td.ui-selection-column .ui-chkbox input').focus();
                            }
                            else {
                                $this.focusedRow = row;
                            }

                            $this.highlightFocusedRow();

                            if($this.cfg.scrollable) {
                                PrimeFaces.scrollInView($this.scrollBody, $this.focusedRow);
                            }

                            e.target = $this.focusedRow.children().eq(0).get(0);
                            $this.onRowClick(e,$this.focusedRow.get(0));
                        }
                        e.preventDefault();
                    break;

                    case keyCode.ENTER:
                    case keyCode.SPACE:
                        if($this.focusedRowWithCheckbox) {
                            $this.focusedRow.find('> td.ui-selection-column .ui-chkbox .ui-chkbox-box').trigger('click.dataTable');
                        }
                        else {
                            e.target = $this.focusedRow.children().eq(0).get(0);
                            $this.onRowClick(e,$this.focusedRow.get(0));
                        }

                        e.preventDefault();
                    break;

                    default:
                    break;
                };
            }
        });

    };
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

JeeDev_User
Posts: 100
Joined: 26 Sep 2019, 11:02

01 Oct 2019, 15:20

SuperB!
Thanks a lot.
Is there a way to mark answer as correct?

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

01 Oct 2019, 16:09

Change the title of the post to "[SOLVED] p:dataTable row selection with arrow keys?"
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

pokala
Posts: 3
Joined: 30 Mar 2021, 15:06

14 Jun 2021, 16:54

Hello

Is there any example to work with p-table with single selection & navigate by selecting row with keyboard up & down arrow keys ?

Thanks in advance.

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

14 Jun 2021, 16:58

You are in the wrong forum this is PrimeFaces forum you want PrimeNG forum.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 24 guests