Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
How can one add a class to the rendered table when a view is outputted as table?
In html-list format for example one can add a class to a wrapper OR/AND to the list. Is this possible to the table as well?
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#55 | views-n1689472-55.patch | 2.61 KB | DamienMcKenna |
| |||
#55 | views-n1689472-55.interdiff.txt | 1.25 KB | DamienMcKenna |
Comments
Comment #1
eridolfi CreditAttribution: eridolfi commentedI have done it before using views templates. Are you familiar with using views templates? If not, there is a link under "Advanced" that is titled, "Theme Information" or something like that. You can click that link and look for style output.
There are a number of different filename suggestions. You can get very specific with the naming. Once you have created the file in your theme's folder, simply click the style output link. Copy the code and then paste it into the empty file in your theme's folder.
If you have questions, please let me know.
Comment #2
bartvdputte CreditAttribution: bartvdputte commentedThanks for your reply.
Eventually I did it this way, but in my opinion it makes no sense creating different "view templates" for each necessary view that has a table output, only to add a class to the table.
Besides that, it's not consistent with how view formatting works in other formats. As I said in my original post, in the html list format you can easily add classes in the UI. Why not in the table format?
Comment #3
adammaloneI'm also looking for this. Views has the ability to give a class to a row as well as give a class to an entire view, so why not allow classes to be given to tables?
Taking a look in theme/views-view-table.tpl.php, it looks like the functionality to add a class to a table is there, just not in the Views UI interface.
<table <?php if ($classes) { print 'class="'. $classes . '" '; } ?><?php print $attributes; ?>>
Comment #4
jhuon CreditAttribution: jhuon commentedSame problem here. Looking in the style output:
$classes: A class or classes to apply to the table, based on settings.
But I can't find this setting in Views UI neither.
Comment #5
danharper CreditAttribution: danharper commentedI'm looking for this also.
Comment #6
moniuch CreditAttribution: moniuch commentedI second this request, I totally agree with #3 and while I'm sure everybody found a workaround to do that in template, I think it's leading us to a disease called tpl'itis.
Comment #7
agerson CreditAttribution: agerson commentedYes, this implies a setting is available in the GUI when its not.
Comment #8
dawehnerComment #9
Anybody+1
Comment #10
zmove CreditAttribution: zmove commented+1 for this.
Comment #11
codesmith+1
Comment #12
schifazl CreditAttribution: schifazl commented+1
Comment #13
alex.87 CreditAttribution: alex.87 commented+1
Comment #14
AntonLargiader CreditAttribution: AntonLargiader commentedActually the class specified in the "Advanced" area does work, but it acts on a DIV rather than on the table, so the CSS needs to read:
.yourclass table {}
rather than
table.yourclass {}
This is view-independent so you can have a favorite table style and select it for any view(s) you want. I was happy to find that I did not have to make a style for each view table or create a style for all view tables, which the other documentation led me to believe.
Comment #15
Nikolay Shapovalov+1
Comment #16
schifazl CreditAttribution: schifazl commentedI think that this could be closed, since the solution in #14 works perfectly!
Comment #17
moniuch CreditAttribution: moniuch commentedI would vote that we close all requests and resort to workarounds, just because there's no will to address users' feedback.
Comment #18
schifazl CreditAttribution: schifazl commentedI thinked for a while about this issue. Adding a class to the table while it was already added to the entire view seems redundant to me, so I don't think that #14 is a workaround, but everyone has his own opinion ;)
Comment #19
zmove CreditAttribution: zmove commentedIt can be great to have the control over table class even if you can have a custom class in a parent div because more and more framework like bootstrap require to precisely give classes to your elements.
Comment #20
cybercampbell CreditAttribution: cybercampbell commentedEasiest way is to put the following in your template.php:
and if you want striped tables:
Hope that helps.
Comment #21
zmove CreditAttribution: zmove commentedAnd if you want to have some table stripped and some tables not :)
Definitely it should be configurable in views.
Comment #22
mooru CreditAttribution: mooru commentedThe code in #20 is not working even after clearing cache
Comment #23
mark@redhorseinteractive.com CreditAttribution: mark@redhorseinteractive.com commentedMaybe just add a line of jquery to a custom js file?
$( "table" ).addClass( "table table-striped" );
Comment #24
minorOffense CreditAttribution: minorOffense commentedI've created a patch which adds the option in the UI and the necessary changes to the preprocess function.
Comment #25
minorOffense CreditAttribution: minorOffense commentedLast patch introduced a PHP warning. This removes the warning.
Comment #26
alex.skrypnyk#25 works great
Comment #27
jedsaet CreditAttribution: jedsaet commented+1 RTBC to #25
Comment #28
nmillin CreditAttribution: nmillin commented#25 works great
Comment #29
big_smile CreditAttribution: big_smile commented#25 works for me too.
Comment #30
Cameron Tod CreditAttribution: Cameron Tod commented#25 works great here too. +1 :)
Comment #31
milos.kroulik CreditAttribution: milos.kroulik commented#25 works great and would be great addition to Views.
Comment #32
Anybody+1 I agree. Any maintainer feedback? Can you include this into the next dev release and stable release afterwards?
Comment #33
mxwright CreditAttribution: mxwright as a volunteer commentedComment #34
colanWe've recently switched our testing from the old qa.drupal.org to DrupalCI. Because of a bug in the new system, #2623840: Views (D7) patches not being tested, older patches must be re-uploaded. On re-uploading the patch, please set the status to "Needs Review" so that the test bot will add it to its queue.
If all tests pass, change the Status back to "Reviewed & tested by the community". We'll most likely commit the patch immediately without having to go through another round of peer review.
We apologize for the trouble, and appreciate your patience.
Comment #35
AnybodyRe-uploaded #25 as of #34
Comment #36
AnybodyComment #37
knalstaaf CreditAttribution: knalstaaf commentedSupport for field tokens would top it off though…
Comment #38
Sneakyvv CreditAttribution: Sneakyvv at Calibrate for Government of Flanders commented#35 works great, thanks!
However... I renamed the patch according to the patch naming convention as it is a real hassle to update contrib modules if the patch names don't start with the module name.
Comment #39
Sneakyvv CreditAttribution: Sneakyvv at Calibrate for Government of Flanders commentedback to RTBC as new patch passes tests and only changes a typo (and patch name).
Comment #40
darrenwh CreditAttribution: darrenwh as a volunteer and at Investis Digital commentedSame here why not just use isset($class)
This comment is missing a period (.) at the end
Could the value be more descriptive what is c$?
Comment #41
minorOffense CreditAttribution: minorOffense at Coldfront Labs Inc. commentedI made some of the changes. Fixed the comment. I added an isset but left most as !empty since I actually want to check if something isn't blank or 0. The variable will most always actually be set but I want to prevent blank values from getting into the classes array.
Comment #42
interdruper CreditAttribution: interdruper at Interdruper commented#41 works fine.
Note that #20 approach also works fine, if you want to easily add a class to every table from views in your theme.
Comment #43
dreftymac CreditAttribution: dreftymac as a volunteer commentedFor those who are not averse to a javascript solution, here is some sample jQuery code.
Comment #44
DamienMcKennaWhat's the rationale to use views_clean_css_identifier() vs drupal_clean_css_identifier()?
Comment #45
minorOffense CreditAttribution: minorOffense at Coldfront Labs Inc. commentedIt mirrors how the list classes are sanitized. Well at least when I originally wrote the patch years ago. If that's changed then just use whatever the current function should be.
Comment #46
nironan CreditAttribution: nironan commented#41 did the trick for me, thanks!
Comment #47
NWOM CreditAttribution: NWOM commented#41 worked for me as well. Thanks so much!
Comment #48
AnybodyI can confirm RTBC.
Can we have this in the next views 7.x release?
Furthermore how do we go on porting this to Drupal 8? New issue for core? Drupal 8 views table also only have rows classes.
Comment #49
Prometheus2704 CreditAttribution: Prometheus2704 commentedCan this be incorporated into the next dev release or an updated patch?
The latest patch in this thread doesn't work on the current stable or dev versions
Comment #50
NWOM CreditAttribution: NWOM commentedAccording to #49, it needs a re-roll most likely against the newest dev.
Comment #51
nmillin CreditAttribution: nmillin commentedRe-roll against dev after confirming patch didn't apply cleanly.
Comment #52
Ken Hawkins CreditAttribution: Ken Hawkins commentedThis is really helpful.
Applying cleanly and works as expected against latest dev for me.
Comment #53
Ken Hawkins CreditAttribution: Ken Hawkins commentedBeing bold — this one's clearly been around for a good bit of testing and peer review over the last several years.
Comment #54
RunOfTheMill CreditAttribution: RunOfTheMill as a volunteer commentedI ended up here when trying to figure out how to add bootstrap classes to the
table
element. Trying to keep the site light and not use jQuery, so I was looking for a plain JS solution to #23 and #43. The simplest.classList
function only seems to work ongetElementByID
, but this table doesn't have an ID.After some digging, I came across this post which had a work-around that allows javascript's
.classList
function to be applied to multiple elements. I adapted it to this situation below, providing a plain vanilla javascript solution to adding bootstrap classes to tables generated by Views.Comment #55
DamienMcKennaMinor tweaks.
Comment #56
jedsaet CreditAttribution: jedsaet commentedThanks! #55 patches against current dev and does the trick.
Comment #57
DamienMcKennaComment #58
AnybodyWhao this is great news :)
Should we create a separate issue for Drupal 8 afterwards or handle the follow-up here?
Comment #59
DamienMcKennaYes, please create a separate issue for D8, if there isn't one already, and list it as a "related issue". Thanks.
Comment #61
DamienMcKennaCommitted. Thanks everyone.
Comment #62
AnybodyDrupal 8 Follow-up: #3045871: Add "Table class" option to views table formatter UI
(I couldn't find a Drupal 8 issue for that yet)