django-tables2为不同的行指定不同的属性 [英] django-tables2 specify different properties for different rows

查看:542
本文介绍了django-tables2为不同的行指定不同的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 django-tables2 创建一个表,使不同的行具有不同的属性。



默认情况下, / p>

 < tr class =odd> 

 < tr class =even> 

如何为某些行指定自己的类?



同样,如果我有一个CheckBoxColumn,并为此列指定一些数据,它会进入

 < input type =checkboxname =colvalue =123/> 

这非常适用于勾选哪个复选框。但是,如何在创建表时将一些复选框设置为选中?



我的方案:用户从大表中选择一些行。例如,表格包含




  • 橙色1

  • 橙色2

  • 苹果5

  • 橙色3

  • 苹果4

  • 黄瓜7
  • b $ b
  • aaple 1



用户选择 aaple 5 / em>。



然后我要显示所有苹果和所有黄瓜,因为用户选择了至少一个苹果和至少一个黄瓜。这允许用户查看其他相关条目:




  • apple 5

  • apple 4

  • cucumber 7



但是,我想突出显示用户实际选择的条目和/或显示选中的复选框:




  • apple 5

  • apple 4

  • cucumber 7


解决方案

好,让我发布自己的解决方案。



我已经复制了标准模板 table.html 我只更改了一行:

 < tbody& 
{%for table.page_object_list | default:table.rows%} {#support pagination#}
{%block table.tbody.row%}
< tr class ={{row.tr_class}}> <! - 每个行的类 - >

而不是

 < tr class ={%cycleoddeven%}> 

这样,您可以为表中的每一行设置不同的类。它仍然是添加一个不可见的列到您的表类:

  class MyTable(tables.Table):
tr_class = tables.Column(visible = False)
...#其他列

无论何时创建表,都可以为任何特定的行设置任何CSS类。请务必使用修改后的模板:

  {%render_table div_tablemodifiedtable.html%} 

当然,您也可以更改原始的 table.html



任何人都可以建议更优雅的解决方案?



总的来说,我有一种感觉django_tables2仍然缺少很多



定义tr_class



p>要使用此功能,您必须使用自定义呈现 。例如:

  class MyTable(tables.Table):
tr_class = tables.Column(visible = False,empty_values =())
def render_tr_class(s​​elf,value):
if value.chosen == True:
return'highlight'

并且 tr 会给予类 highlight


I would like to create a table with django-tables2 such that different rows have different properties.

By default I get either

<tr class="odd">

or

<tr class="even">

How can I specify my own class for some of the rows?

Similarly, if I have a CheckBoxColumn and I specify some data for this column, it goes into the value:

<input type="checkbox" name="col" value="123"/>

This is great for figuring out which checkbox was checked. However, how can I set some checkboxes as checked when the table is created?

My scenario: the user picks some rows from a large table. For example, the table has

  • orange 1
  • orange 2
  • apple 5
  • orange 3
  • apple 4
  • cucumber 7
  • aaple 1

The user picks aaple 5 and cucumber 7.

Then I would like to display all apples and all cucumbers, since the user picked at least one apple and at least one cucumber. This allows the user to see other relevant entries:

  • apple 5
  • apple 4
  • cucumber 7

However, I would like to highlight the entries actually picked by the user by using css and/or by displaying a checked checkbox:

  • apple 5
  • apple 4
  • cucumber 7

解决方案

Well, let me post my own solution.

I have copied the standard template table.html and edited it. I only changed one line:

<tbody>
    {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
    {% block table.tbody.row %}
    <tr class="{{ row.tr_class }}">  <!-- CLASS FOR EACH ROW -->

instead of

    <tr class="{% cycle "odd" "even" %}">

This way you can set a different class for each row in the table. It remains to add an invisible column to your table class:

class MyTable(tables.Table):
  tr_class=tables.Column(visible=False)
  ... # other columns

After that, whenever you are creating a table, you can set any CSS classes for any particular rows. Remember to use the modified template:

{% render_table div_table "modifiedtable.html" %}  

Of course, you can also change the original table.html.

Can anybody suggest a more graceful solution?

Overall, I have a feeling that django_tables2 is still missing many important features, so I have to reinvent the wheel every time I am trying to do something nontrivial.

Define tr_class

To use this, you must use custom rendering. For example:

class MyTable(tables.Table):
 tr_class=tables.Column(visible=False, empty_values=())
 def render_tr_class(self, value):
   if value.chosen == True:
     return 'highlight'

And the tr would be given the class highlight.

这篇关于django-tables2为不同的行指定不同的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆