Search code examples
primefacesprimefaces-datatable

How move between row and page in Primefaces Datatable


I want a code that move between row up and down in datatable with put two buttons and in last row go to next page.

I can write a code that move between row but i can't go to next page in last row. my code is here and it has two function for up and down between rows (tableWidgetVar is datatable widgetvar):

    downRow = function (tableWidgetVar) {
        if (PF(tableWidgetVar).selection.length === 0) {
             PF(tableWidgetVar).selectRow(0);
             return;
        }
        var index = PF(tableWidgetVar).originRowIndex;
        var rows = PF(tableWidgetVar).tbody[0].childElementCount;
        index++;

        PF(tableWidgetVar).unselectAllRows();
        PF(tableWidgetVar).selectRow(index);
        PF(tableWidgetVar).originRowIndex = index;
    },

    upRow = function (tableWidgetVar) {
        var rows = PF(tableWidgetVar).tbody[0].childElementCount;
        var index = PF(tableWidgetVar).originRowIndex;
            index--;
        PF(tableWidgetVar).unselectAllRows();
        PF(tableWidgetVar).selectRow(index);
        PF(tableWidgetVar).originRowIndex = index;
    },

have anyone idea for go to next datatable page in last row?


Solution

  • You need two condition,if you are in first row or you are in last row in page, when you are in last row go to next page and when you are in first row go to previous page.for this you must use PF(tableWidgetVar).paginator. but when you are in first page or last page you have two additional condition because you don't have next or previous page. edit your code like this:

    downRow = function (tableWidgetVar) {
        if (PF(tableWidgetVar).selection.length === 0) {
             PF(tableWidgetVar).selectRow(0);
             return;
        }
        var index = PF(tableWidgetVar).originRowIndex;
        var rows = PF(tableWidgetVar).tbody[0].childElementCount;
        index++;
        if (index === rows) {
            if (PF(tableWidgetVar).paginator.getCurrentPage() === PF(tableWidgetVar).paginator.cfg.pageCount - 1) {
                return;
            }else {
                PF(tableWidgetVar).getPaginator().next();
                \\or PF(tableWidgetVar).paginator.setPage(PF(tableWidgetVar).paginator.getCurrentPage() + 1);
                index = 0;
            }
        }
        PF(tableWidgetVar).unselectAllRows();
        PF(tableWidgetVar).selectRow(index);
        PF(tableWidgetVar).originRowIndex = index;
    },
    
    upRow = function (tableWidgetVar) {
    
        var rows = PF(tableWidgetVar).tbody[0].childElementCount;
        var index = PF(tableWidgetVar).originRowIndex;
        if (index === 0) {
            if(PF(tableWidgetVar).paginator.getCurrentPage()=== 0){
                return;
            }else {
                PF(tableWidgetVar).paginator.setPage(PF(tableWidgetVar).paginator.getCurrentPage() - 1);
                    index = rows - 1;
            }
        } else {
            index--;
        }
        PF(tableWidgetVar).unselectAllRows();
        PF(tableWidgetVar).selectRow(index);
        PF(tableWidgetVar).originRowIndex = index;
    },