简体   繁体   中英

Only allow Select One Django_tables2

I have a table that populates on using django_tables2, with two columns:

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

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. The Javascript sends the result back to the parent window which invoked the pop-up and closes the window. 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. Anyway, the Python part:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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