2

I have a table format view with multiple columns/fields:

  • Date
  • Title
  • Room

And data that looks like this table (as an example):

| Date | Title | Room |

| 4 June 2016 | Yellow-Green | Apple |

| 4 June 2016 | Sky Blue | Orange |

| 1 June 2016 | Magenta | Apple |

| 4 June 2016 | Yellow-Green | Grape |

I want to sort the data in the table at multiple levels:

  1. First by Date in descending order
  2. Second by Title in ascending order
  3. Third by Room in ascending order

So from the data given, the table would sort in this order:

| Date | Title | Room |

| 4 June 2016 | Sky Blue | Orange |

| 4 June 2016 | Yellow-Green | Apple |

| 4 June 2016 | Yellow-Green | Grape |

| 1 June 2016 | Magenta | Apple |

Please note that the data all still needs to be in a single table, rather than splitting into multiple tables, thank you.

The effect I'm trying to achieve in how it's like in Excel -- wherein you can sort the cells by levels -- for instance, first sort by Column B, then sort by Column D, etc.

How can I achieve this in Drupal 7 views without writing/editing any code?

Thanks very much in advance for any help.

(I've edited this question to hopefully clear things up.)

2 Answers 2

2
  1. Add all sorting cck fields (date/title/room in this case) to the 'sort criteria' section in Views UI
  2. Go write a very easy custom module for yourself. Using only one hook, which is hook_views_query_alter(&$views, &$query)

the .module file looks something like this:

function [MODULE_NAME]_views_query_alter(&$view, &$query) {
  if ($view->name == '[views_name]' && $view->current_display == '[display_name]') {
    $query->orderby = array(
      array(
        'field' => '[first_sort_cck_field_value]',
        'direction' => 'AESC',
      ),
      array(
        'field' => '[second_sort_cck_field_value]',
        'direction' => 'DESC',
      ),
      array(
        'field' => '[third_sort_cck_field_value]',
        'direction' => 'ASC',
      )
   );

//use dsm($query->orderby) to debug if you use Devel module;

  }
}

As an alternative, you can also try follow this post

Good luck :)

2
  • Thanks for this info. Unfortunately, we need to be able to achieve this without touching the code, but thank you. :)
    – cchy
    Jun 14, 2016 at 19:49
  • @cchy It is not possible to do what you ask without modifying or adding code.
    – mbomb007
    Mar 8, 2019 at 22:24
1

In Views, under the "Format" heading you format is already "Table", Click the Settings link, Scroll down until you see "Grouping field Nr.1" and choose date. Then use "Grouping field Nr.2" for the next one.

1
  • Ah, thanks although when I tried that, it's splitting the data into different tables, which is actually not what I was looking for. My apologies if I was unclear about that, but it didn't cross my mind that it would do that. I'd like for the data to be in a single table. The effect I'm looking for is like in Excel, where we're able to sort the data in the columns by levels -- first by, say, Column B, then Column D, etc. I hope that helps. I'm going to edit my question now.
    – cchy
    Jun 9, 2016 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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