簡體   English   中英

只允許選擇一個Django_tables2

[英]Only allow Select One Django_tables2

我有一個使用django_tables2填充的表,其中有兩列:

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'}

列更新和提示。

我需要限制用戶僅選擇一個復選框,如果他們選擇另一個復選框,它將取消選擇第一個復選框並選擇新的選擇。

誰能建議該怎么做?

這是我在彈出表中使用的特殊用途table2列的來源。 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”應該填充一個隱藏的輸入框(如果有的話,覆蓋其先前的內容),因此在對表格進行匯總時,僅返回要選擇的最后一項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM