Sortable Table

From Dokeos

Jump to: navigation, search

There is a library available to build sortable tables in Dokeos. Using this possibility you can easily:

  • create a HTML-table with clickable headers to sort the data
  • split data into several pages
  • add a checkbox on every row to perform actions on a set of items

This pages gives a short manual on creating sortable tables in Dokeos. More advanced features are explained in the phpdoc of the files mentioned below.

Contents

Including the necessary libraries

The code of sortable tables is mainly in following files:

  • claroline/inc/lib/sortabletable.class.php This file contains the actual classes to build the sortable table
  • claroline/inc/lib/tablesort.lib.php This file contains some static functions to sort tabular data

All necessary files are included in the Display-library, which is included by default on every Dokeos-page.

Creating a sortable table

There are 2 possibilities:

  • build a table from data in the database
  • build a table from data available in a 2D-array

Build a table from data in the database

In your script, you should at least define 2 functions

A function to get data from the database.

As parameters this function takes

    • $from The index of the first item to return
    • $number_of_items The number of items to return
    • $column The column on which the data should be sorted
    • $direction The sorting direction ('ASC' or 'DESC')

Example:

function get_table_data($from, $number_of_items, $column, $direction)
{
 $sql = "SELECT id AS col0, title AS col1, description AS col2 FROM my_table
         ORDER BY col$column $direction
         LIMIT $from,$number_of_items";
 $res = api_sql_query($sql,__FILE__,__LINE__);
 $data = array();
 while($item = mysql_fetch_row($res)
 {
  $data[] = $item;
 }
 return $data;
}

A function to get the total number of items.

Example:

function get_number_of_items()
{
 $sql = "SELECT COUNT(*) AS number_of_items FROM my_table";
 $res = api_sql_query($sql,__FILE__,__LINE__);
 $obj = mysql_fetch_object($res);
 return $obj->number_of_items;
}

One you've defined these 2 functions, you can create your SortableTablen, define the table headers and show the table:

$table = new SortableTable('table_name','get_number_of_items','get_table_data');
$table->set_header(0,'Title');
...
$table->display();

build a table from data available in an 2D-array

If you allready have an array available with the data to show in your table, you can use following code to create a sortable table:

$table = new SortableTableFromArray($my_data);
$table->set_header(0,'title');
...
$table->display();

Advanced features

Checkboxes in first column

In a lot of cases, you want to give the user the possibility to perform a single action on several items in the table. SortableTable has a function which

  • changes the first column of your table into checkboxes
  • adds a "select all" & "deselect all" link below the table
  • adds a dropdown list with the defined actions.

See function set_form_actions

Additional URL-parameters

SortableTable creates a lot of links (to sort the table, to go to another page,...). Some URL-parameters are automatically defined for this, but you can define a set of optional URL-parameters which SortableTable should use in all created URLs. See function set_additional_parameters($parameters)

Several SortableTables on one page

In some cases you want several sortable tables on a single page in Dokeos. By default the other tables on the page are restored in their default state when you sort a table. It is possible to keep the state of all tables on a page, but then you have to let every table know which other tables are displayed on that page. Every table has a name given in the constructor. You can use these table-names and the function

function set_other_tables($tablenames)

The parameter $tablesnames is a simple array of the table names.

Examples

Please see following files to see the sortable table in action

  • claroline/admin/user_list.php
  • claroline/admin/course_list.php
  • claroline/admin/languages.php
Personal tools