Closed (fixed)
Project:
Views (for Drupal 7)
Version:
6.x-2.3
Component:
table style
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
1 Apr 2009 at 14:42 UTC
Updated:
16 May 2012 at 20:47 UTC
In first, sorry for my poor english ...
I use views to print statistics with table style. I'd like user can choose on the fly column displayed on table.
I have searched in forum, issues, google ... I found nothing !
I create one test function who do that with hook_views_pre_render. The function insert column select form in view header (with full html), it use javascript to change css display and it's work perfectly.
I'm not sure it's the best way. I maybe use hook_form_alter on views_exposed_form ? i need help to start in good condition ...
The function (just a test ...) :
function mymodule_views_pre_render($view) {
//get column list
$field_list = array();
foreach($view->display['default']->display_options['fields'] as $key=>$value) {
$field_list[$value['label']] = str_replace(array('_', ' '), '-', $key);
}
//create checkbox foreach column name
foreach($field_list as $label=>$field){
$header .= "<label><input onChange='hide_show_column(this)' type='checkbox' value='views-field-$field' checked/>$label ";
}
$header = "<fieldset><legend>Choose column</legend>$header</fieldset>";
//javascript function to hide/show column
$header .= "<script type='text/javascript'>
function hide_show_column(op){
if($('.'+op.value)[0].style.display == 'none') {
$('.'+op.value).css(\"display\",\"table-cell\");
}else{
$('.'+op.value).css(\"display\",\"none\");
}
}
</script>";
// 2 is my full html format
$view->display['default']->handler->options['header_format'] = 2;
//add form to header
$view->display['default']->handler->options['header'] .= $header;
}
Comments
Comment #1
dawehnerIf it works its fine.
But i wouldn't use inline js. I would move out the js to an extra js style, and by happy
Comment #2
esmerel commentedSolution suggested, no response.
Comment #3
dan.mantyla commentedSolution for D7
I needed the same thing, using Drupal 7.
Here's how I did it. It's very ugly, but gets the job done. I started with the code above, but a lot of it didn't work for me and I also needed to pre-select a few columns and hide the rest. I added this code to my custom module: