Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django tables 2, how do you cut off a long text field with an ellipsis?

I am using django-tables2 and I have a field that has really long text.

django-table2 long text

Which I would like to truncate like this:

django-table2 shortened text

It is a bootstrap table:

class EntryRecordTable(tables.Table):
    ...
    comment = tables.Column(accessor=A('Comment'))

    class Meta:
        template_name = 'django_tables2/bootstrap-responsive.html'
        attrs = {'class': 'table table-bordered'}
        orderable = False
        empty_text = 'No entries for selected dates'

I have tried this answer about truncating text in a bootstrap table, but it makes all rows a single line and truncates a bit too much. How can I do this in code?

like image 337
tread Avatar asked Dec 13 '22 17:12

tread


1 Answers

I am using truncatewords combined with tooltip:

comment = tables.TemplateColumn('<data-toggle="tooltip" title="{{record.comment}}">{{record.comment|truncatewords:5}}')
like image 128
flopezlira Avatar answered Dec 16 '22 07:12

flopezlira