Drupal Answers Asked by Hiranya Sarma on November 18, 2020
I created a views
page using foo
plugin.When a user visit the page the city and Clinic/Hospital columns by default are being rendered alphabetically (ASC) as in below screenshot.
EDIT: Let’s say for example. City is Bangalore and only for Bangalore Clinic/Hospital(C/H) column is alphabetically ordered(say Hospitals A,B,C,D…).Then user clicks on City head,then for Bangalore C/H column is alphabetically disordered(ex B,D,C,H…) but records are still correct to Bangalore city means those clinic/hospitals are located in Bangalore .Same goes for other cities too.What I want is C/H col should be ordered as earlier (alphabetical) City by City but no matter how city is ordered.
When user clicks on City head (which is sortable),the order of Clinic/Hospital goes heywire (no more alphabetical order) like below image:
Problem: How can I make my Clinic/Hospital order intact even after user clicks on City head.
Below is my views
configuration:
I am using Drupal 7.41
and views 3.0
Tried so far:
function mymodule_views_query_alter(&$view, &$query) {
if($view->name == 'list_of_doctors' && $view->current_display == 'page_1' && count($query->orderby) == 1)
{
$query->orderby[1] = array(
'field' => 'field_data_field_clinic_hospital_field_clinic_hospital_value',
'direction' => 'ASC',
);
}
}
}
But after clicking City head,whole table vanishes.
As I understand you want to be able to use a secondary sort after overriding the default sort by clicking on a column header.
This can be achieved from the front-end using jQuery plugin TableSorter as described here: https://drupal.stackexchange.com/a/125188/22915
Note this won't work for views with pagination.
It can also be achieved with some custom PHP code. Add the following to a custom module and change the CAPITALIZED words accordingly.
/**
* Implements hook_views_query_alter().
*/
function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {
if ($view->name === 'YOUR_VIEWS_NAME' && $view->current_display === 'YOUR_VIEWS_DISPLAY_NAME' && count($query->orderby) == 1) {
$query->orderby[1] = array(
'field' => 'YOUR_SECONDARY_SORT_FIELD',
'direction' => 'ASC',
);
}
}
This code snippet will make sure that, no matter what column you use as main sorting, you always use a certain field as secondary sort. In your case this would be the Clinic/Hospital field (use system name in code).
Note: if the secondary sort field is not a sortable column, you can't use the query alias. So YOUR_SECONDARY_SORT_FIELD needs to be something like: 'field_data_field_name.field_name_value' instead of 'field_data_field_name_field_name_value'.
Answered by dmsmidt on November 18, 2020
1 Asked on December 4, 2020 by miststudent2011
1 Asked on December 3, 2020 by vaibhav-rana
1 Asked on November 30, 2020 by rick1
1 Asked on November 29, 2020 by alaa-haddad
1 Asked on November 18, 2020 by hiranya-sarma
1 Asked on November 13, 2020 by paul-h
2 Asked on October 21, 2020 by wbeasley
1 Asked on October 9, 2020 by sivaji
1 Asked on October 4, 2020 by anders-wallenquist
1 Asked on October 1, 2020 by travis-miller
1 Asked on October 1, 2020 by user3686276
0 Asked on September 13, 2020 by hendrik-kaiser
Get help from others!
Recent Answers
Recent Questions
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP