User Tools

Site Tools


table_class

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
table_class [2019/12/21 21:43] – created sahltable_class [2021/03/18 21:20] (current) sahl
Line 1: Line 1:
 ====== Table class ====== ====== Table class ======
  
-This is a PHP wrapper for the tabulator javascript library and provides an easy way to have data tables.+Table is a PHP wrapper for the Tabulator javascript library, based of the [[Component class]].
  
 ===== A basic table ===== ===== A basic table =====
Line 9: Line 9:
 <code php> <code php>
 $table = new Table(); $table = new Table();
-// The setOption function is used to pass options to the javascript tabulator function. +// The setTabulatorOption function is used to pass options to the javascript tabulator function. 
-$table->setOption('ajaxUrl', '/url-to-fetch-data'); +$table->setTabulatorOption('ajaxUrl', '/url-to-fetch-data'); 
-$table->setOption('columns', array(some column data));+$table->setTabulatorOption('columns', array(some column data));
 // This renders the necessary html  // This renders the necessary html 
 $table->render(); $table->render();
Line 18: Line 18:
 ===== Datarecord convenience ===== ===== Datarecord convenience =====
  
-One can quickly build column definition from a datarecordusing the ''setDefinitionFromDatarecord($classname)'' function, but the easiest way to interact with Datarecord objects is through Daterecords own ''renderEditComplex()'' function.+There are three main ways to integrate [[Datarecord class|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. 
 + 
 +<code php index.php> 
 +$table = new Table(); 
 +$table->setTabulatorOption('ajaxUrl', 'data_source.php'); 
 +$table->buildColumnsFromDatarecord(DATARECORD CLASS NAME); 
 +$table->render(); 
 +</code> 
 + 
 +<code php 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); 
 +</code> 
 + 
 +===== 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: 
 + 
 +<code php> 
 +$filter = new Filter(DATARECORD CLASS NAME); 
 +$filter->addCondition(new ConditionMatch('fieldname', 'value')); 
 + 
 +$table->setFilter($filter); 
 +</code> 
 + 
 +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 filter. Just decode it from the POST like this: 
 + 
 +<code php> 
 +$filter = Filter::getFilterFromJSON($_POST['filter']); 
 +</code> 
 + 
 +which will yield the same filter as passed into the table. 
 + 
 +===== Actionbuttons ===== 
 + 
 +As convenience one can easily add an //actionbutton// on each row. An actionbutton is an icon from Fontawesomewhich will trigger a javascript function with the ID of the row as the parameter. 
 + 
 +<code> 
 +$table->addActionButton('fa-envelope-open-o', 'mailme'); 
 +</code> 
 + 
 +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. 
 + 
 +<code php> 
 +$table->showSelector(); 
 +</code> 
 + 
 +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: 
 + 
 +<code php> 
 +$form = new Form('searchform', 'search.form'); 
 + 
 +$table->setDataURL('data.php'); 
 +$table->attachForm($form); 
 +</code> 
 + 
 +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: 
 + 
 +<code php 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); 
 +</code> 
 + 
 +===== 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.
table_class.1576964623.txt.gz · Last modified: 2019/12/21 21:43 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki