Skip to content
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

Closed
zelenij opened this issue Jun 14, 2017 · 12 comments · Fixed by Karaage-Cluster/karaage#310, mozilla/addons-server#5741, rlmv/doc-trips#90 or drummonds/bene#50

Comments

@zelenij
Copy link

zelenij commented Jun 14, 2017

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.

@zelenij
Copy link
Author

zelenij commented Jun 14, 2017

Upon further testing I can see that my hack doesn't actually work, so the problem is still open

@jieter
Copy link
Owner

jieter commented Jun 15, 2017

@zelenij thanks for reporting.
The suggestion in the stackoverflow answer was actually not that bad: you could do this with the extra_columns keyword argument:

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 style="color: red" html attribute to the <td> tags rendered for this column. Does this support your use case?

@zelenij
Copy link
Author

zelenij commented Jun 15, 2017

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

@jieter
Copy link
Owner

jieter commented Jun 15, 2017

Not really. May I ask, what is your exact usecase?

Issue #253 might be another step? Thoughts?

@zelenij
Copy link
Author

zelenij commented Jun 17, 2017

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.

@jieter
Copy link
Owner

jieter commented Jun 17, 2017

Interesting technique. Did you look into creating a custom template for the django-tables2 table?

@zelenij
Copy link
Author

zelenij commented Jun 17, 2017

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.

@jieter
Copy link
Owner

jieter commented Jun 17, 2017

Yes, I understand. And I also think it should be easier to override attrs for columns table-wide. We already have row_attrs, we could also just add column_attrs...
But adding more and more to the Table constructor has its limits. So maybe someone can think of another way...

@zelenij
Copy link
Author

zelenij commented Jun 17, 2017

Maybe have a method which would allow to inject it?

@jieter
Copy link
Owner

jieter commented Jun 21, 2017

@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
pip install -U https://github.com/bradleyayers/django-tables2/archive/td-attrs.zip
and this syntax:

class Table(tables.Table):
    class Meta:
        attrs = {
           'td': {'data-name': lambda column: column.name}
        }

@zelenij
Copy link
Author

zelenij commented Jun 22, 2017

Yes, this seems to do the trick, thanks!

@jieter
Copy link
Owner

jieter commented Jun 22, 2017

released 1.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment