NAME

Alien::GvaScript::Grid - ChoiceList based Grid with Pagination

SYNOPSIS

  var my_url  = "/app/path/to/data/list";
  var grid_id = "my_unique_grid_id";
  
  var my_grid = new GvaScript.Grid(grid_id, my_url, {
        css: 'grid-class-name',
        grid_container: 'grid_will_display_here',
        toolbar_container: 'grid_toolbar_will_display_here',
        dto: {VUE: 'JSON'},
        columns: [
            {label: 'Full Name', value: function(e) { 
                return e.lname.toUpperCase() + ' ' + e.name;
            }},
            {label: 'Gender', value: 'gender', css: 'tcenter', default_value: 'unknown'}
        ],
        actions: [
            {
                label: 'Add', 
                condition: function() {return this.rights.can_create},
                callback: function()  {alert('Add action has been called')}
            }
        ],
        grabfocus: true,
        onShow: function() {
        
        },
        onEmpty: function() {
            $('grid_will_display_here').update('empty list was retrieved');
        },        
        onPing: function(target) {
            alert('record:\n'+$H(target).inspect() + '\nin grid has been pinged!')}
        }, 
        onCancel: function() {alert('ESC pressed')}
    });

DESCRIPTION

Displays a dataset in a grid format. Dataset is bound to a JSON datasource retrieved via an AJAX request.

JSON object has a predefined format that the Grid class understands and renders.

A Grid is made up of two components:

Grid Result

The grid (table) that would be the container to display the result dataset. Result is displayed with the help of GvaScript.ChoiceList.

Grid Toolbar

The grid's toolbar that would be the container to display grid's actions and pagination links. Toolbar is displayed with the help of GvaScript.CustomButtons and GvaScript.Paginator

GvaScript.Grids

interface that provides one useful method:

GvaScript.Grids.get(id)

finds and returns the Grid instance where id is the unique id of the grid.

BEHAVIOUR

Grid's display is based on a GvaScript.ChoiceList object and thus has the behaviour of that of a choice list.

Grid Navigation Behaviour

DOWN / UP

navigate by recordCustomButtons

HOME / END

start/end of current page CustomButtons

RETURN

fires onPing event handler

ESC

fires onCancel event handler

PAGEUP / LEFT and PAGEDOWN / RIGHT

navigate by page

[Ctrl-HOME] and [Ctrl-END]

navigate to first and last page correspondingly.

JSON Datasource

As mentioned before, the Grid expects the JSON datasource object to be of a predefined format.

  json = {
    'liste'  : [
        {'fname': 'John',    'lname': 'Stiles', 'gender': 'm'},
        {'fname': 'Mary',    'lname': 'Major',  'gender': 'f'},
        {'fname': 'Richard', 'lname': 'Miles',  'gender': 'm'}
    ], 
    'total'  : 15,
    'rights' : {
        can_create : 0,
        can_update : 1,
        can_delete : 0
    }
  };
liste (Array)

array of records.

mandatory.

total (numeric)

total number of records that are being paginated.

mandatory.

rights (JSON Object)

object containing the access rights to the records in the list. Rights values should be boolean expressions. this object will be stored and referenced later via grid.rights for grid actions conditions.

CONSTRUCTOR

  var grid = new GvaScript.Grid(id, url, options);

id

A unique id to identify a Grid. Might be later used in GvaScript.Grids.get(id) to retrieve the grid instance.

url

Url for AJAX request to retrieve current page dataset.

options

css (String)

A string value of space seperated list of class names to be applied to the table element.

optional - defaulted to ''

dto (JSON Object)

Data transfer Object to the AJAX request. Ex: {param1: 'value1', param2: 'value2'}

optional - defaulted to {VUE: 'JSON'}

method (String)

HTTP method to open in the AJAX request.

optional - defaulted to 'post'

columns (Array)

Array of objects that defines the grid columns.

Ex column object:

    {
        label: 'Gender',  
        value: 'gender', 
        css: 'tcenter', 
        default_value: 'unknown'
    }
label (string)

column label. can be an empty string

mandatory.

value (string|Function)

string representing the key of the json element that the column is bound to.

Function that takes the records as an argument and returns string value of the column. Ex: value: function(e) {return e.fname + ' ' + e.lname}

mandatory.

default_value (string)

string value representing the column's default value if no value was found in the JSON datasource.

optional.

css (string|Array)

a single css class or and array of css classes to be given to the column header/cells.

optional.

actions (Array)

Array that defines the grids list of actions (to be rendered in the grid's tool bar).

Action object here is identical to GvaScript.CustomButtons.Button.options

grabfocus (boolean)

boolean indicating whether the grid container is to grab focus right after the grid has been initialized.

optional - defaulted to true

pagesize (numeric|'auto')

a numeric specifying number of records to show on a page. To keep this number variable and dependant on grid's container available space, set it to 'auto'.

optional - defaulted to 'auto'

gridheight (numeric|'auto')

height to set to the grid. To keep this number variable and dependant on the grid's container available space, set it to 'auto'.

optional - defaulted to 'auto'

recordheight (numeric)

height to set to the grid record.

optional - defaulted to 21

requestTimeout (numeric)

time in seconds to wait before aborting the AJAX request.

optional - defaulted to 15

errorMsg (string)

message to display in the grid container area when the request times out.

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

PROPERTIES

Here's a list of useful properties of the Grid Object.

grid_container

  grid.grid_container

HTMLElement container of the grid list

toolbar_container

  grid.toolbar_container

HTMLElement container of the toolbar (pagination links + action buttons)

paginatorbar_container

  grid.paginatorbar_container 

HTMLElement container of the pagination links

actionsbar_container

 grid.actionsbar_container 

HTMLElement container of the action buttons

METHODS

clearResult

  grid.clearResult(msg)

This method clears the grid result container and displays msg instead.

clearToolbar

  grid.clearToolbar()

This methods clears the grid toolbar container.

clearActionButtons

  grid.clearActionButtons()

This method clears the action buttons container and thus removes the buttons

clear

  grid.clear(msg)

This method is equivalent to grid.clearResult(msg) and grid.clearToolbar()

addActionButtons

  grid.addActionButtons()

This method renders the action buttons based on the grids actions. NOTE that the actions conditions will be re-evaluated.

destroy

  my_grid.destroy();

This method will unregister the Grid in the GvaScript.Grids namespace and will iteratively call the destructor on all the grid's depedencies this removing all handlers attached. Call this method when the grid is removed from the DOM.

EVENTS

onShow

This event is triggered of the table is rendered and displayed. Useful for attaching custom events on table records/cells.

onHighlight

This event is triggered when a choice in the list is highlighted. The event handler may use event.index to know the index of the highlighted choice.

onPing

This event is triggered when a choice in the list is "ping-ed", i.e. either by dblclicking, clicking on selected row or by pressing the RETURN key. The event handler will recieve 1 arguement target which is the record object that has been "Pinged".

Ex: {'fname': 'Mary', 'lname': 'Major', 'gender': 'f'}

onCancel

This event is triggered when the user presses the ESCAPE key.

onEmpty

This event is triggered when the list is empty.

EXAMPLE

JAVASCRIPT

  var my_url  = "/app/path/to/data/list";
  var my_grid = new GvaScript.Grid("ex_grid", my_url, {
    grid_container: 'ex_grid_list',
    toolbar_container: 'ex_grid_toolbar',
    dto: {VUE: 'JSON'},
    pagesize: 3,
    columns: [
        {label: 'Full Name', value: function(e) { 
            return e.fname + ' ' + e.lname;
        }},
        {label: 'Gender', value: 'gender', css: 'tcenter', default_value: 'unknown'}
    ],
    actions: [
        {
            label: 'Add', 
            condition: function() {return this.rights.can_create},
            callback: function()  {alert('Add action has been called')}
        }
    ]
  });

  // my_url response : Content-type "application/json"
  {
    'liste'  : [
        {'fname': 'John',    'lname': 'Stiles', 'gender': 'm'},
        {'fname': 'Mary',    'lname': 'Major',  'gender': 'f'},
        {'fname': 'Richard', 'lname': 'Miles',  'gender': 'm'}
    ], 
    'total'  : 15,
    'rights' : {
        can_create : 0,
        can_update : 1,
        can_delete : 0
    }
  }

HTML

  <div id="ex_container">
     
   <div tabindex="0" id="ex_grid_list">
     <table class="dmweb-grid">
       <thead>
         <tr>
           <th class="grid-marker"> </th>
           <th class="grid-header">Full Name</th>
           <th class="grid-header">Gender</th>
        </tr>
       </thead>
       <tbody>
        <tr id="ex_grid_list.CL_choice.0" class="CL_choiceItem liste_highlight">
           <td class="grid-marker"> </td>
           <td valign="top" class="grid-cell index_0">John Stiles</td>
           <td valign="top" class="grid-cell index_0 tcenter">m</td>
        </tr>
        <tr id="ex_grid_list.CL_choice.1" class="CL_choiceItem">
           <td class="grid-marker"> </td>
           <td valign="top" class="grid-cell index_1">Mary Major</td>
           <td valign="top" class="grid-cell index_1 tcenter">f</td>
        </tr>
         <tr id="ex_grid_list.CL_choice.3" class="CL_choiceItem">
           <td class="grid-marker"> </td>
           <td valign="top" class="grid-cell index_0">Richard Miles</td>
           <td valign="top" class="grid-cell index_0 tcenter">m</td>
        </tr>
      </tbody>
    </table>
   </div>
  
   <div id="ex_grid_toolbar" class="dmweb-grid-toolbar">
    <div class="dmweb-paginatorbar">
      <div title="Dernière page" class="last"></div>
      <div title="Page suivante" class="forward"></div>
      <div class="text">1 à 3 de 15</div>
      <div title="Page précédente" class="back inactive"></div>
      <div title="Première page" class="first inactive"></div>
    </div>
    <div class="dmweb-grid-actionsbar">
      <span id="grid_id_btn_0" class="dmweb-btn-container">
        <span class="left"/>
        <span class="center">
          <button class="btn" style="width: auto;" type="button">Add</button>
        </span>
        <span class="right"/>
      </span>
    </div>
   </div>
 
  </div>

CSS

  /* class given to grid list container when an AJAX request is being affected */
  .dmweb-loading {
    background:url(ajax_loading.gif) no-repeat center center;
    position:absolute;
    width:30px;height:30px;
    top:50%;left:50%;
  }

  /* grid toolbar */
  .dmweb-grid-toolbar {
    background:#C5CCE8 url(glass-bg-n.gif) repeat-x scroll left top;
    border:1px solid #A3BAD9;
    height:28px;
  }
  .dmweb-grid-actionsbar {float:left;}
  .dmweb-paginatorbar {float:right;width:250px;}
  .dmweb-paginatorbar div {width:16px;height:16px;cursor:pointer;float:right;}
  .dmweb-paginatorbar div.first {background:url(page-first.gif) no-repeat top center;}
  .dmweb-paginatorbar div.last {background:url(page-last.gif) no-repeat top center;}
  .dmweb-paginatorbar div.back {background:url(page-prev.gif) no-repeat top center;}
  .dmweb-paginatorbar div.forward {background:url(page-next.gif) no-repeat top center;}
  .dmweb-paginatorbar div.inactive {cursor:default;opacity:0.25;filter:alpha(opacity=25);}
  .dmweb-paginatorbar div.text {text-align:center;width:140px;color:#4b34c5;font-size:10pt;}
  .dmweb-paginatorbar span.dmweb-btn-container {display:block;margin-right:4px;}
  
  /* grid table */
  .dmweb-grid {width:100%;}
  .dmweb-grid th.grid-header {
    text-align:center;
    padding:2px;
    font-size:75%;
    color: #183E6C;
    background-color: #D0D6ED;
    border:1px solid #6F82A5;
  }
  .dmweb-grid td {font-size:75%;color:#183E6C;}
  .dmweb-grid .grid-marker {width:15px;background-color: #D0D6ED;border:1px solid #6F82A5;}
  .dmweb-grid tr.liste_highlight td.grid-marker {
    background: #D0D6ED url(selector.gif) no-repeat center center;
  }
  .dmweb-grid tr.liste_highlight td.grid-cell {background-color: #6F82A5 !important;color:#f5f5f5 !important;}
  .dmweb-grid td.grid-cell {padding:2px !important;border:1px solid #e8e8e8;}
  .dmweb-grid td.grid-cell.index_1 {background-color:#EFEFEF;}
  .dmweb-grid td.grid-cell.center {text-align:center;float:none;}
  .dmweb-grid td.grid-cell.right {text-align:right;float:none;}
  .dmweb-grid td.grid-cell.red {color:red;}

DEPENDENCIES

This class depends on the following GvaScript classes:

GvaScript.ChoiceList
GvaScript.Paginator
GvaScript.CustomButtons