2016-12-07 158 views
0

我有填充使用django_tables2,有兩列的表:只允許選擇一個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。

我需要的是限制用戶只選擇一個複選框,如果他們選擇另一個複選框,它會取消選擇第一個複選框並選擇新選項。

任何人都可以建議如何做到這一點?

+0

什麼是CheckBoxColumnWithName?我無法在http://django-tables2.readthedocs.io/en/latest/pages/api-reference.html#column – nigel222

+0

@ nigel222的tables2文檔中看到它,它是一個自定義的「CheckBoxColumn」,它允許標題 – Jim

+0

我猜你想要的是一個自定義列,它將生成一列單選按鈕,或者一列鏈接到JavaScript的按鈕,它將最新的選擇複製到(可能是隱藏的)輸入框中。不過,我不是志願寫的! – nigel222

回答

0

這是我在彈出式表格中使用的特殊用途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」應填入一個隱藏的輸入框(如果有的話覆蓋它以前的內容),所以當窗體sumbitted,只返回要選擇的最後一個項目。

相關問題