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:

  1. Create a new view that displays some data using the datatables style
  2. In the datatables settings, enable 'Sortable' for a column, and select 'Descending' in the dropdown that appears to the right of that checkbox.
  3. Apply, save the view, and browse to the page on which it is displayed.
  4. When the page loads, the table will be sorted in 'Ascending' order for the chosen column, rather than the specified 'Descending'.
  5. 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.

Comments

justinstandring’s picture

justinstandring’s picture

Status: Active » Needs review
justinstandring’s picture

Issue summary: View changes
justinstandring’s picture

StatusFileSize
new506 bytes

Apologies, this patch references the correct variable for 'default_sort_order'.

arkener’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Needs review » Fixed
Related issues: +#1507778: Default Sort Descending

This issue has been fixed in issue #1507778

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.