Table of Contents

Table class

Table is a PHP wrapper for the Tabulator javascript library, based of the Component class.

A basic table

The most basic table is created like:

$table = new Table();
// The setTabulatorOption function is used to pass options to the javascript tabulator function.
$table->setTabulatorOption('ajaxUrl', '/url-to-fetch-data');
$table->setTabulatorOption('columns', array(some column data));
// This renders the necessary html 
$table->render();

Datarecord convenience

There are three main ways to integrate Datarecord objects into a table. The easiest and most convenient is using the DatarecordEditComplex class which automatically both creates a table of data, and adds the options to create, edit and delete such data.

Stepping down a notch is the Table::getTableFromClass($table_id, $class_name) which get a table configured for displaying all data from the class given and which is ready to render. See below on how to filter this data further.

The most manual way to get data from a Datarecord is as follows, where you also need to construct and provide your own data url.

index.php
$table = new Table();
$table->setTabulatorOption('ajaxUrl', 'data_source.php');
$table->buildColumnsFromDatarecord(DATARECORD CLASS NAME);
$table->render();
data.php
// Get all data
$filter = new Filter(DATARECORD CLASS NAME);
$collection = $filter->execute();
 
$result = Table::getDataFromCollection($collection);
 
header('Content-type: text/json');
echo json_encode($result);

Filtering data

For both the DatarecordEditComplex and the getTableForClass configuration above, there is an easy built-in way to filter data, and that is by attaching a filter to the table like this:

$filter = new Filter(DATARECORD CLASS NAME);
$filter->addCondition(new ConditionMatch('fieldname', 'value'));
 
$table->setFilter($filter);

This will apply the filter to the table when displaying.

If you provide your own data url, you can also easily modify it to comply to such a filter. Just decode it from the POST like this:

$filter = Filter::getFilterFromJSON($_POST['filter']);

which will yield the same filter as passed into the table.

Actionbuttons

As a convenience one can easily add an actionbutton on each row. An actionbutton is an icon from Fontawesome, which will trigger a javascript function with the ID of the row as the parameter.

$table->addActionButton('fa-envelope-open-o', 'mailme');

The following will add an icon of an open envelope on each row, and each time it is clicked, the javascript function mailme(id) will be called, where id is the id of the data row.

Selecting items

In order to display checkboxes next to each data row, one can call the showSelector() function on the table.

$table->showSelector();

This will render checkboxes next to each table row.

These checked values can be retrieved from javascript using the getSelectedTableIds(tableid) function, where tableid is the HTML id of the table.

Providing a search form

It is possible to integrate a search form into the table, if one also writes the data url. A form can easily be added to the table by doing this:

$form = new Form('searchform', 'search.form');
 
$table->setDataURL('data.php');
$table->attachForm($form);

This will prevent the table from displaying data before the form is submitted and the content of the form will be posted to the data URL, so one can perform the search server side:

data.php
...
$filter = new Filter(DATARECORD CLASS NAME);
$form = new Form('searchform', 'search.form');
if ($form->isSubmitted()) {
  $values = $form->getValues();
  $filter->addCondition(new ConditionLike('field_to_search', $values['value_to_find']));
}
$collection = $filter->execute();
 
$result = Table::getDataFromCollection($collection);

Searching results

It is possible to use a form field for searching through the results already displayed in the table. In order to do this attach the form field using addTabulatorOption('filter_field', 'ID_OF_FORM_FIELD').

Tabulator options

Native tabulator options can be passed to the table from PHP, by using the addTabulatorOption($option, $value) function.