I once implemented something similar where I had to add records to a
DataTable.
What I ended up doing is twickying the DataProvider to allow adding rows to
the current page.

ie: I would add new rows to the current page and flush them to the DB on
Save.
I did not have to care about the page number nor the sort order as the rows
were always added to the current page, top row and the table would grow in
size with each additonal record. Once pagination happen, the rows would be
sorted anyhow and displayed in the proper place.

Below are the changes to my parent DataTable that I had to implement to
make this possible (feel free to use as you see fit):
    /**
     * Obtain the index of the first row on the given page number.
     *
     * @param pageNum The page number for which to retrieve the index
within the List model.
     * @return The first row index on the given page number if that page
were to be rendered.
     */
    public int getIndexOfFirstRowOnPage(int pageNum) {
        return (pageNum > 0) ? pageNum * getItemsPerPage() : 0;
    }

    /**
     * Obtain the index of the last row on the given page number.
     *
     * @param pageNum The page number for which to retrieve the index
within the List model.
     * @return The last row index on the given page number if that page
were to be rendered.
     */
    public int getIndexOfLastRowOnPage(int pageNum) {
        int rowsPerPage = getItemsPerPage();
        int totalRows   = getRowCount();
        int lastRow    = (pageNum > 0) ? (pageNum * rowsPerPage) +
rowsPerPage -1 : rowsPerPage - 1;
        if(lastRow >= totalRows) {
            lastRow = totalRows - 1;
        }
        return lastRow;
    }

    /**
     * Retrieve collection of the row models for the given page
number.<br><br>
     * <i><b>NOTE:</b> Is more efficient to build your own collection of
elements on
     * a page by adding them during one of your columns populateItem()
call.</i>
     *
     * @param pageNum The page number for which to retrieve the collection
of rows.
     * @return  A list of models found on the given page number, null if
the page
     *          number is invalid.
     */
    @SuppressWarnings("unchecked")
    public List<T> getRowsOnPage(int pageNum) {
        int rowsPerPage = getItemsPerPage();
        int totalRows   = getRowCount();
        int firstRow    = (pageNum > 0) ? pageNum * rowsPerPage : 0;
        int count       = (rowsPerPage > totalRows) ? totalRows :
rowsPerPage;
        int lastRow     = firstRow + count;
        if( lastRow > totalRows) {
            count = rowsPerPage - (lastRow - totalRows);
        }

        if((pageNum > totalRows/rowsPerPage) || (pageNum < 0) ) {
            return null;
        }

        List<T> elements = new ArrayList<T>();
        Iterator<T> iter = (Iterator<T>) provider.iterator(firstRow, count);
        while(iter.hasNext()) {
            elements.add(iter.next());
        }
        return elements;
    }

Noroc si bafta la treaba!

~ Thank you,
   Paul Bors

On Sun, Feb 24, 2013 at 9:02 AM, Ciocoiu Elvis
<elvis.cioc...@synthesys.ro>wrote:

> Hi,
>
> I'm trying to select a item of inmethod grid after adding it in an ajax
> call. Based on the current sort properties and filters, after I effectively
> add the element in the database but in the same ajax call I'm determining
> the element's page number and try to set it as current page in the grid. My
> code works except in the case when the created element is in the last page
> (the current grid page page count is cached internally in
> AbstractPageableView.cachedPageCount). In this case the cachedPageCount is
> equal to the create element page and when calling
> grid.setCurrentPage(newPage) { ... if (page < 0 || page >= pageCount &&
> pageCount > 0) ... } throws IndexOutOfBounds exception. I want to clear the
> cachedPageCount somehow ... or maybe there is another solution? I want to
> select the new page in the same ajax call. For the moment if I encounter
> this situation I select the previous page ... but the my newly created
> element is on the next one :(
>
> Can somebody help me with some hints?
>
> Thank you
>
> --
> _____________________________________________________
> Elvis Ciocoiu
> Senior Consultant
>
> Synthesys Consulting ROMANIA
>
> address: http://www.synthesys.ro
> e-mail: elvis.cioc...@synthesys.ro
> mobile : (40) 0745 13 75 85
>
> This message and any attachments contain information, which may be
> confidential or privileged.
> If you are not the intended recipient, please refrain from any
> disclosure, copying, distribution or use of this information.
> Please be aware that such actions are prohibited. If you have received
> this transmission in error, kindly notify us by email to
> off...@synthesys.ro. We appreciate your cooperation.
>

Reply via email to