如何使用django-tables2汇总选中的列

如何使用django-tables2汇总选中的列,django,django-tables2,Django,Django Tables2,我有一个有一些列的表。一列是checkboxcolumn。 那么,如何总结checkboxcolumn选中的“数量”列? 如果有人能帮助我,我将不胜感激。 您可能希望可以在呈现的表中选择多个复选框,然后对其进行处理。此功能未实现。如果你想让事情真正发生,你需要自己去实现 所以django-tables2没有内置的东西,我们必须自己编写一些东西。这使得这更像是一个JavaScript/HTML问题,但无论如何,让我们看看是否可以为此模型创建一个表: class Country(models.Mod

我有一个有一些列的表。一列是checkboxcolumn。 那么,如何总结checkboxcolumn选中的“数量”列? 如果有人能帮助我,我将不胜感激。

您可能希望可以在呈现的表中选择多个复选框,然后对其进行处理。此功能未实现。如果你想让事情真正发生,你需要自己去实现

所以django-tables2没有内置的东西,我们必须自己编写一些东西。这使得这更像是一个JavaScript/HTML问题,但无论如何,让我们看看是否可以为此模型创建一个表:

class Country(models.Model):
    name = models.CharField(max_length=100)
    population = models.PositiveIntegerField()
这是一个基本表,通过向其中一列添加页脚参数向表中添加空页脚。稍后我们将使用此页脚将计算出的人口总数输入

class CheckboxTable(tables.Table):
    select = tables.CheckBoxColumn(empty_values=(), footer='')

    population = tables.Column(attrs={'cell': {'class': 'population'}})

    class Meta:
        model = Country
        template_name = 'django_tables2/bootstrap.html'
        fields = ('select', 'name', 'population')
现在,模板。我使用jQuery快速创建一个函数来计算总和。每次对其中一个复选框和页面加载触发
change
事件时,都会执行此函数

{% load django_tables2 %}
{% render_table table %}
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
$(function () {

    // calculate the sum of the population column for the selected rows.
    function update_sum() {
        var sum = 0;
        // loop over each checkbox within <tbody>
        $('tbody input[type=checkbox]').each(function () {
            // if the checkbox is checked
            if ($(this).is(':checked')) {
                // find the contents of the population column, convert
                // it to integer and add it to the total.
                sum += parseInt($(this).parents('tr').find('.population').html());
            }
        });
        // add the sum to the footer cell in the population column.
        $('tfoot .population').html(sum);
    }

    // update the sum after each change
    $('tbody input[type=checkbox]').on('change', update_sum);

    // update the sum initially.
    update_sum();
});
</script>
{%load django_tables 2%}
{%render_table%}
$(函数(){
//计算所选行的填充列的总和。
函数更新_sum(){
var总和=0;
//在中的每个复选框上循环
$('tbody input[type=checkbox]')。每个(函数(){
//如果选中该复选框
如果($(this).is(':checked')){
//查找“填充”列的内容,然后进行转换
//将其转换为整数,并将其添加到总数中。
sum+=parseInt($(this.parents('tr').find('.population').html());
}
});
//将总和添加到“填充”列的页脚单元格中。
$('tfoot.population').html(总和);
}
//每次更改后更新总和
$('tbody input[type=checkbox]')。在('change',update_sum')上;
//最初更新总和。
更新_sum();
});
我将调用django-tables2示例应用程序。

您可能希望可以在呈现的表中选择多个复选框,然后对其进行处理。此功能未实现。如果你想让事情真正发生,你需要自己去实现

所以django-tables2没有内置的东西,我们必须自己编写一些东西。这使得这更像是一个JavaScript/HTML问题,但无论如何,让我们看看是否可以为此模型创建一个表:

class Country(models.Model):
    name = models.CharField(max_length=100)
    population = models.PositiveIntegerField()
这是一个基本表,通过向其中一列添加页脚参数向表中添加空页脚。稍后我们将使用此页脚将计算出的人口总数输入

class CheckboxTable(tables.Table):
    select = tables.CheckBoxColumn(empty_values=(), footer='')

    population = tables.Column(attrs={'cell': {'class': 'population'}})

    class Meta:
        model = Country
        template_name = 'django_tables2/bootstrap.html'
        fields = ('select', 'name', 'population')
现在,模板。我使用jQuery快速创建一个函数来计算总和。每次对其中一个复选框和页面加载触发
change
事件时,都会执行此函数

{% load django_tables2 %}
{% render_table table %}
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
$(function () {

    // calculate the sum of the population column for the selected rows.
    function update_sum() {
        var sum = 0;
        // loop over each checkbox within <tbody>
        $('tbody input[type=checkbox]').each(function () {
            // if the checkbox is checked
            if ($(this).is(':checked')) {
                // find the contents of the population column, convert
                // it to integer and add it to the total.
                sum += parseInt($(this).parents('tr').find('.population').html());
            }
        });
        // add the sum to the footer cell in the population column.
        $('tfoot .population').html(sum);
    }

    // update the sum after each change
    $('tbody input[type=checkbox]').on('change', update_sum);

    // update the sum initially.
    update_sum();
});
</script>
{%load django_tables 2%}
{%render_table%}
$(函数(){
//计算所选行的填充列的总和。
函数更新_sum(){
var总和=0;
//在中的每个复选框上循环
$('tbody input[type=checkbox]')。每个(函数(){
//如果选中该复选框
如果($(this).is(':checked')){
//查找“填充”列的内容,然后进行转换
//将其转换为整数,并将其添加到总数中。
sum+=parseInt($(this.parents('tr').find('.population').html());
}
});
//将总和添加到“填充”列的页脚单元格中。
$('tfoot.population').html(总和);
}
//每次更改后更新总和
$('tbody input[type=checkbox]')。在('change',update_sum')上;
//最初更新总和。
更新_sum();
});

我向django-tables2示例应用程序发送了一封电子邮件。

您的意思是想知道选中行的数量吗?我有3列:“id、value和select”。第三列是检查列。当我检查这一列时,我想对值列进行汇总。您是想在浏览器中汇总,还是想在python中汇总?现在我需要了解python中的汇总。但是知道这两种情况会很好,所以,我需要在同一屏幕上显示总数。我不知道怎样做才是最好的。你的意思是想知道选中行的数量吗?我有3列:“id、value和select”。第三列是检查列。当我检查这一列时,我想对值列进行汇总。您是想在浏览器中汇总,还是想在python中汇总?现在我需要了解python中的汇总。但是知道这两种情况会很好,所以,我需要在同一屏幕上显示总数。我不知道怎么做才是最好的。我明白了!I包括valorfloat=parseFloat(valor.replace(“,”,”);谢谢我得到了它!I包括valorfloat=parseFloat(valor.replace(“,”,”);谢谢