简体   繁体   English

只允许选择一个Django_tables2

[英]Only allow Select One Django_tables2

I have a table that populates on using django_tables2, with two columns: 我有一个使用django_tables2填充的表,其中有两列:

tables.py table.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'}

Columns update and vehid. 列更新和提示。

What I need is to restrict the user to only select one checkbox, if they select another checkbox, it unselects the first and selects the new choice. 我需要限制用户仅选择一个复选框,如果他们选择另一个复选框,它将取消选择第一个复选框并选择新的选择。

Can anyone advise how to do this? 谁能建议该怎么做?

Here's the source of a special-purpose tables2 Column which I use in pop-up tables. 这是我在弹出表中使用的特殊用途table2列的来源。 The Javascript sends the result back to the parent window which invoked the pop-up and closes the window. Javascript将结果发送回父窗口,该父窗口调用弹出窗口并关闭该窗口。 How to do that is a Javascript question rather than a Python/Django one, and I'm pretty sure that the way I am doing it is not the best, since JS is not one of my strengths. 如何做到这一点是一个Javascript问题,而不是Python / Django问题,而且我很确定我的做法不是最好的方法,因为JS并不是我的强项之一。 Anyway, the Python part: 无论如何,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 )

For what you ask, I'd suggest that the JS "returnResultAndClose" should populate a hidden input box (overwriting its previous content if any), so when the form is sumbitted, only the last item to be selected is returned. 对于您的要求,我建议JS“ returnResultAndClose”应该填充一个隐藏的输入框(如果有的话,覆盖其先前的内容),因此在对表格进行汇总时,仅返回要选择的最后一项。

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

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