Dzięki Berdirowi działam . Tak to działa bardziej szczegółowo.
Tablesort jest uruchamiany „automatycznie”, jeśli tablice (kolumnowe) w tablicy $ headers zawierają klucze „data”, „field” i opcjonalnie „sort”. Spowoduje to utworzenie linków z „sort” i „uporządkowanie” w nagłówkach kolumn oraz wyświetlenie małej strzałki i tym podobnych.
Aby wykonać własne sortowanie, uzyskaj bieżące ustawienia sortowania za pomocą tableort_get_order i tablesort_get_sort i użyj tych wartości do własnej funkcji sortowania. Klucz „sql” w tablicy zwrócony przez tableort_get_order zawiera nazwę pola, która ma być używana do sortowania.
Kawałek (nieprzetestowanego) przykładowego kodu z tablicą $ users zawierającą pewne szczegóły dla każdego użytkownika:
// setup the table data that we want to show
$tableData = array();
foreach ($users as $userDetails) {
$tableData[] = array(
'name' => $userDetails['name'],
'visits' => $userDetails['visits'],
'views' => $userDetails['views'],
'comments' => $userDetails['comments']
);
}
// headers array, sorting by default on comments
$headers = array(
array('data' => t('Name'), 'field' => 'name'),
array('data' => t('Visits'), 'field' => 'visits'),
array('data' => t('Views'), 'field' => 'views'),
array('data' => t('Comments'), 'field' => 'comments', 'sort' => 'desc')
);
// getting the current sort and order parameters from the url
$order = tablesort_get_order($headers);
$sort = tablesort_get_sort($headers);
// sort the table data accordingly (write your own sort function)
$tableData = my_array_sort($tableData, $order['sql'], $sort);
// create the array with rows for theme table
$rows = array();
foreach ($tableData as $entry) {
$rows[] = array(
array('data' => $entry['name']),
array('data' => $entry['visits']),
array('data' => $entry['views']),
array('data' => $entry['comments']),
);
}
// add any attributes and sent everything to theme table
$attributes = array('class' => array('my_class'));
$table = array('header' => $headers, 'attributes' => $attributes, 'rows' => $rows);
$html = theme('table', $table);