5

I work on drupal 7, and create a page from views used 4 exposed filter. I want to display exposed selected values.enter image description here, and also close button in exposed selected values

I try to selected data in field and excluded it and used in header Global: Unfiltered text. But not work properly.

  • 1
    Although it is possible to do it using views hooks and preprocessors personally I would simply use JavaScript! – Alexar May 31 '15 at 15:34
  • I would use a search page with facets rather than a view with exposed filters. – Darvanen Jun 1 '15 at 0:06
2

It is not the best practice to include PHP in views. Instead override views tpl file to include the code as needed. Consider view page named "Exposed Filter data". "Display output" file can be found under "advanced"->"Theme: information" setting of view. Select last name from "Display Output" as the tpl file name. For this example file name is "views-view--exposed-filter-data--page.tpl.php". Clicking on "Display Output", you will get current code of the file. Include following code after $exposed is printed & its condition ends. Here is the code that needs to be done for displaying exposed selected values of all four dropdowns. Considering Lamp, Color Temperature, Luminous Flex & Wattage as Taxonomy Term Reference fields, current view is loaded & current selected values are displayed with a cross anchor link.

Note that here "data-field" value on anchor field are same as id of the select list.

<?php 
  $lamp = '';
  $color_temperature = '';
  $luminous_flex = '';
  $wattage = '';
  $view = views_get_current_view();
  if (isset ($view->exposed_input['field_lamp_tid'])) {        
    $tid = $view->exposed_input['field_lamp_tid'];
    if (is_numeric($tid)) {
      $term_obj = taxonomy_term_load($tid);
      $lamp = '<a href="#" class="reset" data-field="edit-field-lamp-tid">X</a>' . $term_obj->name; 
    }
  }
  if (isset ($view->exposed_input['field_color_temperature_tid'])) {        
    $tid = $view->exposed_input['field_color_temperature_tid'];
    if (is_numeric($tid)) {
      $term_obj = taxonomy_term_load($tid);
      $color_temperature = '<a href="#" class="reset" data-field="edit-field-color-temperature-tid">X</a>' . $term_obj->name; 
    }
  }
  if (isset ($view->exposed_input['field_luminous_flex_tid'])) {        
    $tid = $view->exposed_input['field_luminous_flex_tid'];
    if (is_numeric($tid)) {
      $term_obj = taxonomy_term_load($tid);
      $luminous_flex = '<a href="#" class="reset" data-field="edit-field-luminous-flex-tid">X</a>' . $term_obj->name; 
    }
  }
  if (isset ($view->exposed_input['field_wattage_tid'])) {        
    $tid = $view->exposed_input['field_wattage_tid'];
    if (is_numeric($tid)) {
      $term_obj = taxonomy_term_load($tid);
      $wattage = '<a href="#" class="reset" data-field="edit-field-wattage-tid">X</a>' . $term_obj->name;
    }
  }
  echo $lamp . ' ' . $color_temperature . ' ' . $luminous_flex . ' ' . $wattage;
?>

Jquery is used for reseting the value of dropdown & submitting the form, when cross link is clicked. Replace "VIEW_EXPOSED_FORM_ID" with your form id by inspecting element.

jQuery(document).ready(function(){
  jQuery('a.reset').live( 'click', function() {
    var datafield = jQuery(this).attr('data-field');
    jQuery('#'+datafield).val('All');
    jQuery( 'VIEW_EXPOSED_FORM_ID' ).submit();
  });
});

Hope this helps.

Thanks!

|improve this answer|||||
  • Good amount of detail here, well done. Just a note, I didn't recommend using PHP in views, but instead in a template. I realize the first example I posted had someone adding PHP to their view header but the same code applies to the appropriate template. Thanks for clarifying for new folks. – Jon Reid Jul 6 '15 at 15:14
  • Can we remove the selected filter using ajax. Means without refreshing page.? – jsh Sep 6 '15 at 7:35
1
+25

You have a couple of options here Rituraj.

You'd need to use PHP to output the selected values in the header. See the related example here to see how to output view filter values to a template.

<?php
$view = views_get_current_view();

if(isset($view->exposed_input['field_release_date_value']['value']['year'])) {
  echo "<h2>Press Releases for ", $view->exposed_input['field_release_date_value']['value']['year']; "</h2>";
}
else {
  echo  "<h2>Press Releases for ", date("Y"); "</h2>";
}
?>

This post has an example for using an exposed filter value to override the page title as well.

My preferred method would be to use JQuery to read updates from the exposed filter and inject them into the DOM. That would have the added bonus of making it easy for you to wire up the X links to remove both the value both from the display and from the exposed filter.

|improve this answer|||||

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