The theme_table() documentation reads as follows (emphasis mine):
$header An array containing the table headers. Each element of the array can be either a localized string or an associative array with the following keys:
- "data": The localized title of the table column.
- "field": The database field represented in the table column (required if user is to be able to sort on this column).
- "sort": A default sort order for this column ("asc" or "desc").
- Any HTML attributes, such as "colspan", to apply to the column header cell.
The documentation neglects to mention that the implementation of the 'sort' field requires another function, tablesort_sql(), to be called and appended to the query from which the table has been generated.
I was able to find reference in the forum (http://drupal.org/node/144371#comment-232344) which illustrates the use of tablesort_sql(), and indeed tablesort_sql()'s docs do backreference theme_table, but I believe mention should be made in the documentation for the theme_table function itself for completeness.
Cheers,
Andy
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | tablesort_doc_v2.patch | 976 bytes | aspilicious |
| #10 | tablesort_doc.patch | 1.02 KB | theflowimmemorial |
| #4 | drupal-theme_table_sort_docs-310144-4.patch | 1002 bytes | webchick |
| #2 | theme_table_sort_docs.6.patch | 1017 bytes | dvessel |
| #2 | theme_table_sort_docs.7.patch | 1002 bytes | dvessel |
Comments
Comment #1
kingandyOh man, I screwed up the list code there huh.
Comment #2
dvessel commentedHow's this?
Comment #3
Anonymous (not verified) commentedReads correct to me.
Comment #4
webchickCommitted to 7.x, with the minor adjustment of wrapping at 80 characters. Thanks!
Moving down to 6.x. Here's the patch I committed. There are other lines in that PHPDoc that should be shortened too but I wanted to protect the kittens. :)
Comment #5
gábor hojtsyI don't think this is clear at all.
Sort "data"? Appending "sort data" to what? This depends on tablesort_sql() appending? That function does not do any appending! The query is passed to theme_table()? It should not be passed there.... Ooooh. I don't think any of the details of this sentence are clear, and helpful, except the reference to the function, with which developers might be able to run with. I think we should work on improving this, and I am not going to commit this as-is. A suggestion which you can pick up and improve.
This will only work if the output of tablesort_sql() is appended to the SQL query which selects data for this table.
Or something.
Comment #6
dvessel commentedGábor, I wish I could have been more clear.
The $header parameter since it's an element of "sort". The rest isn't as clear as it could have been looking at it again. I did a quick scan and that description came out. :)
Comment #7
gábor hojtsyWell, we should somehow express that the original SQL query needs to be appended with the output of tablesort_sql() and $header should be passed into that as well. $header is not modified at all in that function, so there is nothing appended to $header as you describe.
Comment #8
webchickHuh. For some reason this made perfect sense when I had a skull-splitting migraine the other night. ;) But I agree, it could use some clarification.
Rolled back in D7, but I agree this needs fixing. Care to have another stab?
Comment #9
KingMoore commentedjust spent an hour trying to figure out how to get my headers to sort... knew it could be done but the extra step was no where in any documentation I could find.
Comment #10
theflowimmemorial commentedPatch attached which adds the work Gabor used in #5. Read it over and it seems to read pretty clearly.
Comment #12
jhodgdon#10: tablesort_doc.patch queued for re-testing.
Comment #14
aspilicious commentedLets try this
Comment #15
aspilicious commentedComment #16
KingMoore commentedI'm not totally sure here, but it seems like the behavior of 'sort' => 'asc' is not as I understood it.
I assumed that you could set a default sort order for each column, so the first time you click a column header it would sort either as asc or desc depending on the parameter.
What seems to happen is that this is the default sort column and order for the table when is first rendered and NO sort column is specified. It *seems* like adding a 'sort' parameter to the $header_row array only works as expected if I specify one and only one field with a 'sort' parameter.
This is not what I expected, but seems to make sense... although it would be nice to be able to specify which way each column sorts when first clicked.
Comment #17
jhodgdongrammar nitpick:
" query which selects data for this table." which => that
Also, I think maybe something should be added to the description part of this documentation to address the confusion in #16.
Comment #18
jhodgdonThis issue has been superceded by
#839634: Fix up documentation for table sorting
Comment #19
abhishek@098 commentedTable sort by header Drupal 9
if(empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc'))
{
$default = $header;
}
$header = [
'field_name' => [
'data' => $this->t('Name'),
'field' => 'field_name',
],
];
$build['tablesort_table'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $data,
];