2

I am using the DataTables module (https://www.drupal.org/project/datatables). The sorting option does not work for all columns, and I am not sure why. I'd like the ability to have all columns sortable as this is just in the DOM, shouldn't that be a possibility?

enter image description here

For example:

  • Columns that do sort: Content Text, Terms, Dates...
  • Columns that do not sort: View Conditional (if this then that results)

Here is 2 of the table columns html output:

<th class="views-field views-field-title sorting_asc" tabindex="0" aria-controls="datatable-1" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Title        : activate to sort column ascending">Title</th>

There is a checkbox in the views datatable settings next the "TITLE" column above...

There is not checkbox enabling sorting on the column below (views conditional):

<th class="views-field views-field-views-conditional sorting_disabled" rowspan="1" colspan="1" aria-label="BLM">BLM</th>

EDIT: This might be due to the sType (https://datatables.net/development/sorting)

In the DataTables module, changing line 298 from FALSE to TRUE allows all sorting... I am sure there is a reason not to allow this, but not sure why. Investigating sType more to apply another elseif on string or whatever type is being produced by the views conditional...

Line 298:

if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
        $column_options['bSortable'] = TRUE; //changed from FALSE
      }

Line 301: //checks what sType the data is:

else {
        // Attempt to autodetect the type of field in order to handle sorting
        // correctly.
        if (drupal_strlen($fields[$field]->last_render) != drupal_strlen(strip_tags($fields[$field]->last_render))) {
          $column_options['sType'] = 'html';
        }
        elseif (is_numeric($fields[$field]->last_render)) {
          $column_options['sType'] = 'numeric';
        }
        elseif ($fields[$field] instanceof views_handler_field_date) {
          $column_options['sType'] = 'date';
        }
        $column_options['bSortable'] = TRUE;

STRING is not one of the options in here. Though the documentation says it could be: http://legacy.datatables.net/usage/columns#sType

I attempted to add it like so real quick with no results:

elseif (drupal_strlen($fields[$field]->last_render) != drupal_strlen(strip_tags($fields[$field]->last_render))) {
      $column_options['sType'] = 'string';
    }
  • Can you clarify your question a bit please, to explain what you mean by "all" columns. E.g. by adding a sample of what works and what does not work. – Pierre.Vriens Aug 24 '15 at 16:15
  • Please don't post answers inside questions. If you found a way, great! By all means, share it. But share it where it belongs, as an answer. – Mołot Aug 24 '15 at 20:53
  • @molot, still looking for an answer. This hacks the module, I'd like to know how to address this properly. – jasonflaherty Aug 24 '15 at 20:58
1

I used a hook_views_data_alter() to add 'click sortable' to the field that didn't have that option.

/**
 * Implements hook_views_data_alter().
 */
function utagta_helper_views_data_alter(&$data) {
  // Allow the views fields to be sortable.
  $data['views']['view']['field']['click sortable'] = TRUE;
}
0

If I am reading your question right, you are wanting to sort a Views Conditional field on the database level. The issue is, the value that would be sorted by is only available after the database work is done and the view starts processing the conditions. Therefore, the database sorting does not work even if you force the ui to allow it.

What you can do is use a hook_views_pre_render to alter the ordering of the results, effectively sorting it yourself. I have done this when I was making a site usage report and the client wanted multiple layers of aggregation with filters. If you have the ajax setting on No, and force the ui to permit sorting on the fields, then you can listen for the sort in the url and use the pre_render to perform it.

Other options are treat this like a rewritten field, and do one of these: sort-view-on-a-rewritten

  • Sorry, not in the database, using data tables module. It is dom manipulation – jasonflaherty Aug 24 '15 at 18:12
  • 1
    Ah, then it is out of my realm of knowledge. Sorry. – DeveloperWeeks Aug 24 '15 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.