The default sort order of a datatable's column is not sourced from the dropdown menu shown in the Views options popup, rather it's currently being sourced from a static value ("asc") in the parent plugin "views_plugin_style_table".
Steps to reproduce:
- Create a new view that displays some data using the datatables style
- In the datatables settings, enable 'Sortable' for a column, and select 'Descending' in the dropdown that appears to the right of that checkbox.
- Apply, save the view, and browse to the page on which it is displayed.
- When the page loads, the table will be sorted in 'Ascending' order for the chosen column, rather than the specified 'Descending'.
- Optional: In Firefox/Chrome, open the inspector console, and view the value of Drupal.settings.datatables - the 'order' property for your datatable will be 'asc' not 'desc'.
Why this happens
In datatables.module line 305, the DataTables aaSorting option is set on the correct column, but the order is taken from the general $options['sort'] rather than the sort order specific to that column which is $options['info'][$field]['default_sort_order'] (which is set by the Views UI form).
Furthermore, the $options['sort'] is not set by the datatables view style plugin - it's set by the parent plugin views_plugin_style_table as a default value of 'asc'. This can be seen here (line 35): http://cgit.drupalcode.org/views/tree/plugins/views_plugin_style_table.i...
Proposed solution
Sets the DataTables aaSorting option to the value of $options['info'][$field]['default_sort_order'] for the column being added to the table. This corrects the sort order of the table upon initial load.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | datatables-default_sort_order_source-2429213-4.patch | 506 bytes | justinstandring |
Comments
Comment #1
justinstandring commentedComment #2
justinstandring commentedComment #3
justinstandring commentedComment #4
justinstandring commentedApologies, this patch references the correct variable for 'default_sort_order'.
Comment #5
arkener commentedThis issue has been fixed in issue #1507778