# GvaScript.Paginator

API for data pagination

### Table of contents

NAME
SYNOPSIS
DESCRIPTION
DESTRUCTOR
Pagination Parameters
Navigation Links
HTML
CSS
Programming Interface
Methods
new GvaScript.Paginator
Flipping the Pages

## NAME

GvaScript.Paginator - API for data pagination

## SYNOPSIS

      var url = '/app/my/paginated/data';
      var mypaginator = new GvaScript.Paginator(url, {
        list_container  : 'list_result_container',
        links_container : 'pagination_links_container'

        onSuccess   : onSuccessHandler,
        parameters  : {param1: 'val1', param2: 'val2'},
        step        : 20
      }

## DESCRIPTION

Paginated data is a set of data that can be divided into differnent pages. It is best described as a list of items that overflow one page and best presented in several pages. The Paginator Object is used in this case to manage retrieval (through AJAX Requests) and handling (through an onSuccess handler). It also will take care of creating the navigation links (first, prev, next, last) and rendering them into provided container. Navigation links are used for an on demand page flipping.

Data will be requested using an HTTP call to a provided URL. Data Index and Size are decided using `INDEX` and `STEP` parameters respectively. It is up to you to make use of these parameters to decide on the items to display.

### DESTRUCTOR

      mypaginator.destroy();

This method will remove all handlers assigned to pagination buttons. Call this method when the paginator element is removed from the DOM.

### Pagination Parameters

The Paginator Object will manage the 2 request parameters

- `INDEX` start index of items to retrieve
- `STEP` number if items to include per page

  These two parameters would help you in deciding which part of the dataset to return.

## Programming Interface

### Methods

#### new GvaScript.Paginator

      var paginator = new GvaScript.Paginator(
            url,
            {opt1:"val1", opt2:"val2", ...}
      );

Creates the object that controls pages navigation. Arguments are

- `url`

  URL used to retrieve pages content using HTTP request.

- `options`

Options used to define and customize the Paginator object.

The Paginator object is returned as it might be useful for a programatic navigation of pages.

Available options are:

- links_container (String|HTMLElement)

  Element or an id of an HTML Element to render the navigation links into.

  required.

- list_container (String|HTMLElement)

  Element of an id of an HTML Element that is used to render the result of the pagination. A 'gva-loading' classname is set to this container in the duration of page request. Also ajax timeout and error messages will be displayed in this container.

  required.

- step (numeric)

  Number of items to display per page. This number would be the value of `STEP` (HTTP Request Parameter) that is transferred with pagination calls.

  optional - defaulted to 20

- method (String)

  String indicating the HTTP method to use in the AJAX request for retrieving paginated data.

  optional - defaulted to 'post'

- parameters (String|Hash)

  String in the syntax of key1=val1&key2=val2, or hash {key1: 'val1', key2: 'val2'} of key/value pairs of parameters to be sent with the HTTP request

  optional - defaulted to {}

- onSuccess (function)

  Function handler of the onSuccess of the AJAX HTTP request to retrieve paginated data.

  optional - defaulted to Prototype.emptyFunction

- lazy (boolean)

  Boolean indicating whether to load first page with the initialization of the Paginator Object. `true` implies lazy loading of the first page - will wait for an explicit call to `loadContent` method.

  optional - defaulted to false

- timeoutAjax (Numeric)

  Number of seconds to wait before aborting the Ajax HTTP Request. Upon abort, an error message will display in the `list_container`.

  optional - defaulted to 15

- errorMsg (String)

  Error message to display on timeoutAjax.

  optional - defaulted to "Problème de connexion. Réessayer et si le problème persiste, contacter un administrateur."

#### Flipping the Pages

- hasNext()

  Returns boolean indicating whether the paginator has more data to display.

- hasPrevious()

  Returns boolean indicating whether the paginator has previous data to display.

- getFirstPage()

  Returns boolean if successful.

- getPrevPage()

  Returns boolean if successful.

- getNextPage()

  Returns boolean if successful.

- getLastPage()

  Returns boolean if successful

- loadContent()

  Core method that initializes the AJAX HTTP Request to retrieve data and calls the onSuccess handler to dipplay the result.