select all rows in django_tables2
Asked Answered
L

2

8

I have tried to select all rows in a table by clicking on the upper check box in a CheckBoxColumn with the following definition:

selection = tables.CheckBoxColumn(accessor="pk", orderable=False)

However the rows are not selected, is there anything else I need to do? I am using django 1.4.1 and django_tables2 0.13.0.

Lamond answered 17/10, 2012 at 17:3 Comment(0)
L
23

It is possible to select all checkboxes in a CheckBoxColumn by replacing the input attribute in the header cell:

selection = tables.CheckBoxColumn(accessor="pk", attrs = { "th__input": 
                                        {"onclick": "toggle(this)"}},
                                        orderable=False)

Then this JavaScript construct in the template file should do the work.

<script language="JavaScript">
function toggle(source) {
    checkboxes = document.getElementsByName('selection');
    for(var i in checkboxes)
        checkboxes[i].checked = source.checked;
}
</script>
Lamond answered 24/10, 2012 at 16:32 Comment(0)
T
3

Unfortunately CheckBoxColumn is very bare-bones. The header checkbox does not do anything, and it's up to you to write JavaScript to make it work. There are plans to make this more seamless, but aren't like to happen soon.

Tyranny answered 17/10, 2012 at 22:54 Comment(2)
Thanks, could you point me to an example of JavaScript that can be used to select all rows in the table by selecting the header box?Lamond
I don't know anything that'll be easy to integrate off the top of my head, but I know this functionality exists within the django.contrib.admin web UI. Perhaps have a look there?Tyranny

© 2022 - 2024 — McMap. All rights reserved.