【问题标题】:Only allow Select One Django_tables2只允许选择一个 Django_tables2
【发布时间】:2016-12-07 11:24:16
【问题描述】:

我有一个使用 django_tables2 填充的表,有两列:

tables.py

class SummaryTable(tables.Table):

    update = CheckBoxColumnWithName(verbose_name = "Select",accessor="pk", 
                                   orderable=False)

    class Meta:
        model = Vehicle
        fields = ('update', 'vehid')

        # Add class="paleblue" to <table> tag
        attrs = {'class':'paleblue'}

列更新和vehid。

我需要的是限制用户只选择一个复选框,如果他们选择另一个复选框,它会取消选择第一个并选择新的选择。

谁能建议如何做到这一点?

【问题讨论】:

  • 什么是 CheckBoxColumnWithName?我在django-tables2.readthedocs.io/en/latest/pages/…的tables2 文档中看不到它
  • @nigel222 这是一个自定义的CheckBoxColumn,允许标题
  • 我猜你想要的是一个自定义列,它将生成一列单选按钮,或者一列链接到 JavaScript 的按钮,它将最新的选择复制到一个(可能隐藏) 输入框。不过,我不是自愿写的!

标签: python django checkbox django-tables2


【解决方案1】:

这是我在弹出表中使用的特殊用途的 tables2 列的来源。 Javascript 将结果发送回调用弹出窗口并关闭窗口的父窗口。如何做到这一点是一个 Javascript 问题,而不是 Python/Django 问题,而且我很确定我这样做的方式不是最好的,因为 JS 不是我的强项之一。无论如何,Python部分:

class SelectorColumn( tables.Column):
    """ a special column that will normally be used only by selectPopupFactory """ 
    def __init__( self, *a, **kw):
        clickme = kw.pop('clickme', None) # html or by default, the column value as link
        super().__init__( *a, **kw)
        self.clickme = mark_safe(clickme)

    def render( self, value):
        return format_html( "<a href='#' onclick='return returnResultAndClose(\"{}\");'>{}</a>", value, self.clickme or value )

对于您的要求,我建议 JS“returnResultAndClose”应该填充一个隐藏的输入框(如果有的话,覆盖它以前的内容),所以当表单被提交时,只返回最后一个要选择的项目。

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2014-10-17
    • 1970-01-01
    相关资源
    最近更新 更多