Is there any way, when creating a sortable table, that I can specify which field the table should initially be sorted on? I can easily do it like this:
$query->orderBy('efficiency', 'desc');
However if I am making a sortable table (by adding clickable headers) I have to order it like this:
$query = $query
->extend('TableSort')
->orderByHeader($header_row);
And it default sorts on the first table column, which is not how I want my table to sort by default. Also, I would love to be able to set the default direction of sorting per column, but this seems to be impossible.
Here is all my code:
$table_rows = array();
$header_row = array(
array('data' => 'Player', 'field' => 's.last_name'),
array('data' => 'Team', 'field' => 's.team_id'),
array('data' => 'Pos', 'field' => 's.position'),
array('data' => 'MP', 'field' => 's.minutes'),
array('data' => 'Pts', 'field' => 's.points'),
array('data' => 'Reb', 'field' => 's.rebounds'),
array('data' => 'Ast', 'field' => 's.ast'),
array('data' => 'Stl', 'field' => 's.stl'),
array('data' => 'Blk', 'field' => 's.blk'),
array('data' => 'TO', 'field' => 's.tov'),
array('data' => 'PF', 'field' => 's.pf'),
array('data' => 'Eff', 'field' => 'efficiency', 'sort' => 'DESC'),
);
$query = db_select('nba_stats_daily', 's');
$query->condition('s.game_date', $date_text, '=');
$query->fields('s', array('name', 'team_id', 'position', 'minutes', 'points', 'rebounds', 'ast', 'stl', 'blk', 'tov', 'pf', 'efficiency'));
$query = $query
->extend('TableSort')
->orderByHeader($header_row);
$result = $query->execute();
foreach($result as $stat_line) {
$table_rows[] = array(
array('data' => $stat_line->name),
array('data' => $stat_line->team_id),
array('data' => $stat_line->position),
array('data' => $stat_line->minutes),
array('data' => $stat_line->points),
array('data' => $stat_line->rebounds),
array('data' => $stat_line->ast),
array('data' => $stat_line->stl),
array('data' => $stat_line->blk),
array('data' => $stat_line->tov),
array('data' => $stat_line->pf),
array('data' => $stat_line->efficiency),
);
}
//return a themed schedule table
return theme_table(array('header' => $header_row,
'rows' => $table_rows,
'attributes' => array('class' => 'table_dfs', 'width' => '100%'),
'caption' => '',
'colgroups' => array(),
'sticky' => FALSE,
'empty' => 'No data is available',
));
In the example above, when the table is initially rendered it sorts on the Player field. I want it to sort on Efficiency by default. Am I doing something wrong? This seems to be a lot more confusing than the documentation had me first believe.
Thanks!
Comments
Try lowercase?
I just used
'sort' => 'desc'on one of my header and it works.
Name your columns
You have to put in keys for your columns....
Must be "desc", Not "DESC"
via includes/tablesort.inc:211
Thanks!
Nice catch mate. Thanks a lot!
What's new and changing in PHP 8.4
Nice catch, was pulling my
Nice catch, was pulling my hair out for about an hour in trying to figure out why it wasn't working!
dodged that bullet
Saved me tons of time. Thx.
I just found an issue
I just found an issue preventing the default sort from working. In my header, I was forcing an empty header, as I have a column that does not need a title. I had this:
For some reason, the empty data attribute was causing the default sort to not work. The solution was to do the following instead. Drop the empty column, and use a colspan instead:
Contact me to contract me for D7 -> D10/11 migrations.
Default sort breaks with empty column
Brilliant- Thanks (again!) @Jaypan. MVP 2 u.
-Bronius
--
http://drupaltees.com
80s themed Drupal T-Shirts