-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add additional attributes to td (and other elements) after the table was created #451
Add additional attributes to td (and other elements) after the table was created #451
Comments
Upon further testing I can see that my hack doesn't actually work, so the problem is still open |
@zelenij thanks for reporting. class MyTable(tables.Table):
class Meta:
model = Person
fields = ('first_name', 'last_name')
table = MyTable(queryset, extra_columns=[
('first_name', tables.Column(attrs={'td': {'style': 'color: red;'}}))
]) will add the |
It's a step in the right direction. Is there an easy way to do the same generically on all the columns of an existing table instance? It's a given that these columns can be some unknown derived classes of tablesColumn |
Not really. May I ask, what is your exact usecase? Issue #253 might be another step? Thoughts? |
What I'm trying to do is to stamp on each td (cell) the name of it's header, in order to then use CSS tricks for nice rendering on mobile. One description of the CSS trick is here: https://elvery.net/demo/responsive-tables/ (the last section on the page). There are alternatives, for example I could use jQuery to achieve the same, but I prefer to restrict the usage of JavaScript as much as realistically possible. |
Interesting technique. Did you look into creating a custom template for the django-tables2 table? |
Yes, it's an option. However, from that moment onwards I'll have to keep my custom template in sync with the "official" one, which over the long term might prove annoying. |
Yes, I understand. And I also think it should be easier to override attrs for columns table-wide. We already have |
Maybe have a method which would allow to inject it? |
@zelenij while I was fixing this, it turned out to be kind of supported already, see #457. Please test this by installing the branch using class Table(tables.Table):
class Meta:
attrs = {
'td': {'data-name': lambda column: column.name}
} |
Yes, this seems to do the trick, thanks! |
released 1.9.0 |
Please take a look at this discussion on SO: https://stackoverflow.com/questions/44525828/django-tables2-updating-attrs-has-no-effect/44546087
In essence, I want to be able to inject some additional attributes into the td element after the table was created. The table in question is based on a model, and therefore the columns are generated automatically, rather than being crafted one by one, providing a chance to set the parameters there. There is a way to hack it at the moment, but it feels it's using an undocumented API and such is brittle.
The text was updated successfully, but these errors were encountered: