Conquering columns in a Drupal standard View
Mon, Apr 13, 2009 by jonathan
I was trying to figure out how to show/hide Drupal table columns in a View using checkboxes to select which columns are visible. Most of my research (aka asking around) slash googling was pointing towards jquery but I wanted to stay away from the whole .js route. So after some trial and error I was able to come up with this solution... Step 1: I used hook_form_alter to add checkboxes to the View's exposed filter form. Each checkbox represents a header/column in the view table. Step 2: I preprocessed that table in the template.php file and set an array that contains the values of those checkboxes that were checked. (This takes only 2 lines of code) Step 3: Lastly I created my own tpl file that would override the default tpl file. The default tpl file renders all of the columns that were created with the View, this is done by iterating through an array of all the column headers and displaying each. What I did was instead of using that default array, I passed it the array I created in step 2, an array that contains only the headers that I want, and voila, column show/hide in a Druapal View. Of course JQuery may or may not have been much simpler to implement but hey, at least now I know there is a different approach... now all that is left is to theme the table... I'll leave that for a later post. Cheers
Jim posted on April 15, 2009 10:12 am
Added to DrupalSightings.com
Post new comment