首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django:如何更改django-tables2 2中的列宽度

Django:如何更改django-tables2 2中的列宽度
EN

Stack Overflow用户
提问于 2013-11-07 21:48:49
回答 2查看 7.5K关注 0票数 2

我使用django-tables2{% render_table table %}来显示一个有表的网页。这个表有多个行和多个列。其中一些列有跨多行的文本。这会创建具有不同高度的行。

我尝试过使用CSS,但它似乎不影响宽度:

代码语言:javascript
复制
.my_col_2 {
    width: 100px;
}

我的views.py:

代码语言:javascript
复制
def index(request):
    table = OrderTable(Order.objects.all(), order_by="-order_date")
    RequestConfig(request, paginate={"per_page": 10}).configure(table)
    return render(request, 'orders_app/index.html', {'table': table})

当使用django-tables2 2时,如何手动指定列的宽度和行的高度?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-13 06:40:05

您必须选择宽度和高度作为列的限制因素。假设不能指定内容长度,则可以选择截断所显示的内容。

Set the table column width constant regardless of the amount of text in its cells?Using CSS to create table cells of a specific width with no word wrapping所示,在不修改表布局和宽度设置的情况下,很难或不可能直接设置表列宽度;或者,您可以将所有内容包装在div中,然后将格式应用于这些div。

为了在tables2中实现这一点,我重写了table.Column:

代码语言:javascript
复制
class DivWrappedColumn(tables.Column):

    def __init__(self, classname=None, *args, **kwargs):
        self.classname=classname
        super(DivWrappedColumn, self).__init__(*args, **kwargs)

    def render(self, value):
        return mark_safe("<div class='" + self.classname + "' >" +value+"</div>")

在表中创建列:

代码语言:javascript
复制
    custom_column = DivWrappedColumn(classname='custom_column')

然后应用css:

代码语言:javascript
复制
div.custom_column {
  white-space: normal;
  width: 200px;
  height: 45px;
}

这将导致一个固定宽度、固定高度的单元格,直到没有更多行,然后截断为止。

或者,使用“空格: nowrap",省略高度;然后单元格就会被截断(但是用户可以滚动)。

票数 4
EN

Stack Overflow用户

发布于 2021-12-22 22:48:10

代码语言:javascript
复制
# assume columns c1 and c2 that should be only as wide as needed
#   to fit their content
# and a column c3 which takes up the rest of the table width 

class MyTable(tables.Table):

    def __init__(self, *args, **kwargs):
        self.columns['c1'].column.attrs = {"td":{"style" : "width:1%;" }}
        self.columns['c2'].column.attrs = {"td":{"style" : "width:1%;" }}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19847371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档