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

Comments

kingandy’s picture

Oh man, I screwed up the list code there huh.

dvessel’s picture

Project: Documentation » Drupal core
Version: » 7.x-dev
Component: Correction/Clarification » documentation
Status: Active » Needs review
StatusFileSize
new1002 bytes
new1017 bytes

How's this?

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

Reads correct to me.

webchick’s picture

Version: 7.x-dev » 6.x-dev
StatusFileSize
new1002 bytes

Committed 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. :)

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs work

I don't think this is clear at all.

+ *   - "sort": A default sort order for this column ("asc" or "desc"). This depends
+ *     on tablesort_sql() appending sort data when forming queries before being
+ *     passed to theme_table().

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.

dvessel’s picture

Gábor, I wish I could have been more clear.

/**
 * Return a themed table.
 *
 * @param $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"). This depends
 *     on tablesort_sql() appending sort data when forming queries before being
 *     passed to theme_table().
 * ..snip..
 */
Sort "data"? Appending "sort data" to what?

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. :)

gábor hojtsy’s picture

Well, 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.

webchick’s picture

Version: 6.x-dev » 7.x-dev

Huh. 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?

KingMoore’s picture

just 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.

theflowimmemorial’s picture

Status: Needs work » Needs review
Issue tags: +tcdocsprint09
StatusFileSize
new1.02 KB

Patch attached which adds the work Gabor used in #5. Read it over and it seems to read pretty clearly.

Status: Needs review » Needs work
Issue tags: -tcdocsprint09

The last submitted patch failed testing.

jhodgdon’s picture

Status: Needs work » Needs review

#10: tablesort_doc.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +tcdocsprint09

The last submitted patch, tablesort_doc.patch, failed testing.

aspilicious’s picture

StatusFileSize
new976 bytes

Lets try this

aspilicious’s picture

Status: Needs work » Needs review
KingMoore’s picture

I'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.

jhodgdon’s picture

Status: Needs review » Needs work

grammar 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.

jhodgdon’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs work » Closed (duplicate)

This issue has been superceded by
#839634: Fix up documentation for table sorting

abhishek@098’s picture

Issue summary: View changes

Table 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,
];