简体   繁体   English

django-tables2自定义表格可视化

[英]django-tables2 customize table visualization

I am working on a web app with Django (first time using this) and I have successfully rendered a table with django-tables2 and it looks like this: 我正在使用Django开发一个Web应用程序(第一次使用此功能),并且已经使用django-tables2成功渲染了一个表,它看起来像这样:

Sequence     Epitope     Score

sequence1    name1       0.5    
sequence1    name2       0.7
sequence2    name1       0.4
sequence2    name2       0.2
...          ...         ...

But I would like to switch the columns and rows to get it to look like: 但我想切换列和行,使其看起来像:

Sequence     name1     name2     ...
sequence1    0.5       0.7       ...
sequence2    0.4       0.2       ...
...          ...       ...

Is there a way to change this without changing my models? 有没有一种方法可以在更改我的模型的情况下进行更改? I have been searching for a while now but I can't find a way to change this. 我已经搜索了一段时间,但找不到改变它的方法。 Can anyone help me with this? 谁能帮我这个?

Here is my table from tables.py 这是来自tables.py的表

class CSVTables(tables.Table):

class Meta:
    model = CSV_Results
    attrs = {
        'class': 'output_table',
        'th': {'class': 'output_table_header'}
        }
    template_name = 'django_tables2/bootstrap.html'
    fields = ('TCRsequence', 'Epitope', 'Score')#,"Submission_ID")

The model is linked to a form, depending on the input from the user, there could be 10 names in 'Epitope', 50 or just 2,... . 该模型链接到一个表单,具体取决于用户的输入,“表位”中可能有10个名称,50个或仅2个...。 My model: 我的模特:

class CSV_Results(models.Model):
TCRsequence = models.CharField(max_length=100)
Epitope = models.CharField(max_length=100)
Score = models.FloatField()
Submission_ID = models.ForeignKey('Info_Submission',on_delete=models.CASCADE)

class Meta:
    verbose_name_plural = "CSV_results"

My views.py: 我的views.py:

table = CSVTables(CSV_Results.objects.filter(Submission_ID__Submission_ID__exact=submission_id))
    RequestConfig(request, paginate={'per_page': 50}).configure(table)

And in my html I just rendered the table with: 在我的html中,我使用以下命令呈现了表格:

{% render_table table %} {%render_table表%}

Thanks! 谢谢!

For fixed set of columns which are not generated dynamically, define table as below and do not associate it with model 对于不是动态生成的固定列集,请按以下方式定义表,并且不要将其与模型关联

class CSVTables(Table):
   sequence = Column(verbose_name='Sequence')
   name1 = Column(verbose_name='Name1')
   name2 = Column(verbose_name='Name2')

Then iterate over queryset objects to generate list of dictionary objects matching to table, which will be passed as argument during table initialization. 然后遍历queryset对象以生成与表匹配的字典对象列表,该列表将在表初始化期间作为参数传递。 Each dictionary object should be of format 每个字典对象应采用以下格式

{ 'sequence' : 'xxxx',
   'name1' : 'yyy',
   'name2' : 'zzz'}

Update: 更新:

django-tables2 will not suite for dynamic columns generation. django-tables2将不适合动态列生成。 For Dynamic columns client side rendering is a better option. 对于动态列,客户端渲染是一个更好的选择。 There are couple of JS libraries like datatables might help at client side. 有几个JS库像数据表可能有助于在客户端。 A django view app for datatables is django-datatable-view , but I have not tried the dynamic tables in this app. 数据表的django视图应用程序是django-datatable-view ,但是我没有在此应用程序中尝试过动态表。 If not, a simple view which can serialize the JSON data as per datatables requirement, which is quite simple, is all what is needed. 如果不是这样,那么只需要一个简单的视图,就可以按照数据表的要求序列化JSON数据,这非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM