Swaraj
Swaraj

Reputation: 589

GWT SimplePager - Add History for every next and previous

I need to implement a service like whenever a next or previous is clicked in pagination the browser should save the state so that while doing browser back it goes back to last accessed page. Currently it is happening like when I enter any url in new tab page and in pagination if I click on next button, further I click browser back button it is going to new tab page.

Anybody please suggest how to achieve this.

I have tried following

    public class ShortenLastPagePager extends SimplePager implements ValueChangeHandler<String>

{

private int pageSize;


public ShortenLastPagePager( TextLocation location, Resources resources, boolean showFastForwardButton,
    int fastForwardRows, boolean showLastPageButton, int pageSize )
{
    super( location, resources, showFastForwardButton, fastForwardRows, showLastPageButton );
    this.pageSize = pageSize;
}


@Override
public void setPageSize( int pageSize )
{
    this.pageSize = pageSize;
}


@Override
public void setPageStart( int index )
{
    if ( getDisplay() != null ) {
        Range range = getDisplay().getVisibleRange();
        int myPageSize = 0;

        index = Math.min( index, getDisplay().getRowCount() );
        index = Math.max( 0, index );
        myPageSize = Math.min( pageSize, getDisplay().getRowCount() - index );

        if ( index != range.getStart() || myPageSize != range.getLength() ) {
            getDisplay().setVisibleRange( index, myPageSize );
        }
    }
}


@Override
public void nextPage()
{
    if ( getDisplay() != null ) {
        Range range = getDisplay().getVisibleRange();
        setPageStart( range.getStart() + range.getLength() );
    }
}


@Override
public void previousPage()
{
    if ( getDisplay() != null ) {
        Range range = getDisplay().getVisibleRange();
        setPageStart( range.getStart() - pageSize );
    }
}


@Override
public void lastPage()
{
    int remainder = getDisplay().getRowCount() % pageSize;
    if ( remainder > 0 ) {
        setPageStart( getDisplay().getRowCount() - remainder );
    } else {
        if ( getDisplay().getRowCount() / pageSize > 0 ) {
            setPageStart( getDisplay().getRowCount() - pageSize );
        } else {
            setPageStart( 0 );
        }
    }
}


@Override
public void firstPage()
{
    setPageStart( 0 );
}


@Override
public boolean hasPreviousPage()
{
    return getDisplay() == null ? false : getDisplay().getVisibleRange().getStart() > 0 && getDisplay().getRowCount() > 0;
}


@Override
public boolean hasNextPage()
{

    return getDisplay() == null ? false
        : getDisplay().getRowCount() > ( getDisplay().getVisibleRange().getStart() + pageSize );
}


@Override
public void onValueChange( ValueChangeEvent<String> event )
{
    event.getValue();
    System.out.println( event.getSource() );
}

}

It is not hitting onValueChange it is coming to nextPage

Upvotes: 2

Views: 445

Answers (1)

pillingworth
pillingworth

Reputation: 3238

Instead of trying to add a value change listener to the pager you could put the code in the data provider (assuming you have one).

In the data provider that is providing the content (again I am assuming a table or list) then simply update the browser history as necessary.

Something like

@Override
protected void onRangeChanged(final HasData<YourObject> display) {

  // update browser URL without navigating
  History.newItem(historyMapper.getToken(place), false);

  ...
}

then add support into your place token for storing the paging data, start index, page length, that sort of thing.

You will need corresponding code at the start of your activity to read the paging data and handle it as necessary.

Ideally if you are using activities and places you want to avoid using the browser back button as generally it will cause a new place and new activity to be created which is a bit heavy. I would imagine you can code your way around this.

Upvotes: 1

Related Questions