GithubHelp home page GithubHelp logo

Table footer about django-tables2 HOT 13 CLOSED

jieter avatar jieter commented on August 29, 2024
Table footer

from django-tables2.

Comments (13)

bradleyayers avatar bradleyayers commented on August 29, 2024

What was your specific use-case? The first one that comes to mind for me, would be a totals row, however I think this would appear strange if the table was paginated. In the context of pagination, should the footer show data specific to the current data on the page, or specific to all data in the table?

Just a few problems that come to mind, thoughts? Any ideas on an API for this?

from django-tables2.

bradleyayers avatar bradleyayers commented on August 29, 2024

This can be achieved now in v0.8.0 by writing a custom template:

{% extends "django_tables2/table.html" %}

{% block table.tfoot %}
<tfoot>
    <tr>
        <td colspan="3">three column spanning footer</td>
    </tr>
</tfoot>    
{% endblock %}

It can be hooked up to the table via:

class FootedTable(tables.Table):
    # ... columns

    class Meta:
        template = "path/to/footed_template.html"

from django-tables2.

blite avatar blite commented on August 29, 2024

I think this is still needed, but as a special type of row that totals up the whole dataset

from django-tables2.

bradleyayers avatar bradleyayers commented on August 29, 2024

Could you propose an API for this

from django-tables2.

blite avatar blite commented on August 29, 2024

just a quick look over this it seems like you'd need different column types to be able to automatically calculate it. You could however quite easily insert a hook into the iter method of BoundRows to return a total if a certain function existed

def render_total(self, columns):
    #columns is a dict of all the columns with initial value set to None
    #render None as N/A
    return columns   

from django-tables2.

bradleyayers avatar bradleyayers commented on August 29, 2024

Perhaps a render_footer method on Column that would return HTML for a footer cell, and allow subclasses to extend it. However this raises a few questions:

  • How does render_footer access data to use to calculate an output value.
  • In the context of pagination, should the footer be for the current page, or all pages.
  • Should (and if so how) multiple footer rows be supported.

from django-tables2.

blite avatar blite commented on August 29, 2024

that's definitely better because you could re-use some of the logic to actually render the values. I don't think multiple footer rows would be that common, but if you wanted to build it in a way to support them you could just consider the footer to be similar to rows. I don't personally use the pagination, but I think it would be more intuitive to total the rows currently displayed.

{% block table.tfoot %}
<tfoot>

    {% for row in table.footer %}
      <tr>  
      {% for column, cell in row.items %}
          <th>{{ cell }}</th>
      {% endfor %}
      </tr>    
    {% endfor %}

</tfoot>    
{% endblock %}  

from django-tables2.

blite avatar blite commented on August 29, 2024

for now I ended up doing the above plus this:

    def __init__(self, form, request, *args, **kwargs):
        rf_values = form.get_values()
        rf_annotations = form.get_annotations()
        rf_filter = form.get_filter()
        rf_calculations = form.get_calculations()                  
        data = list(HourlyStat.objects.filter(**rf_filter
                    ).values(*rf_values
                    ).annotate(**rf_annotations).order_by('-clicks')) 
        super(StatsTable, self).__init__(data, *args, **kwargs)      
        columns = rf_values + rf_annotations.keys() + rf_calculations
        for col in columns:
            self.base_columns[col] = tables.Column()
        f_data = dict.fromkeys(columns, 0)
        for k in f_data.keys():
            if k in rf_annotations.keys():
                f_data[k] = sum([x[k] for x in data])
        self.footer_data = self.TableDataClass(data=[f_data], table=self)
        self.footer = BoundRows(self.footer_data)    

from django-tables2.

thebookworm101 avatar thebookworm101 commented on August 29, 2024

I'm in need of a way to create a totals column row in m my table, is there a way to do that yet? im unable to follow the code snippets by blite above

from django-tables2.

bradleyayers avatar bradleyayers commented on August 29, 2024

I assume you mean a totals row, and if so, what should its behavior be in regards to pagination?

from django-tables2.

thebookworm101 avatar thebookworm101 commented on August 29, 2024

um, yes that's what meant to say, thanks for the correction, about pagination im thinking the totals should occur at the very end of the table (im not expecting anything else to make sense for my use-case since its a time delta column with float values) , or but since i have different tables, i could have them on a per table basis, something like that.
EDIT:
Is there a way of accessing the column data so i can add it up or is there already a way of doing that?

from django-tables2.

bradleyayers avatar bradleyayers commented on August 29, 2024

So you mean you'd only see the totals row on the last page?

from django-tables2.

thebookworm101 avatar thebookworm101 commented on August 29, 2024

yes, only on the last page

from django-tables2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.