StartVader StartVader - 4 months ago 38
PHP Question

Moodle flexible_table sorting

I am using

Moodle 2.6
and already rewrote a table from
html_table
to
flexible_table
. I could not figure out, how the
sorting
is working.
I set everything as follows:

$table = new flexible_table('Car Bookings');
$table->define_baseurl(new moodle_url("/blocks/cars/view.php"));
$table->define_columns(array(
'carname',
'platenumber',
'pickupdate',
'tankdate',
'city',
'actions',
));
$table->define_headers(array(
'car name',
'plate number',
'pick-up date',
'tank date',
'city',
'actions',
));
$table->sortable(true, 'carname');
$table->collapsible(false);


I then add for the sorting:

if ($table->get_sql_sort()) {
$sort = ' ORDER BY '.$table->get_sql_sort();
} else {
$sort = '';
}


The table is rendering, but the sorting is not working, i.e the headers are "sorting"-I can click on them, but nothing happens. What I am missing?

Answer

You need to do the sorting manually. Something like this:

if ($orderby = $table->get_sql_sort()) {
    $sql .= ' ORDER BY ' . $orderby . ' ';
}

Have a look at the code in /user/index.php

Comments