question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

additional header <tr> and summarized values in <tfood>

See original GitHub issue

Hi Andrew,

In my target table, I use a seperate <tr> inside the header (above the column description) to specify a factor for the SUM values later in the <tfoot> tag.

  1. Is there a way to specify an additional <tr> tag inside the header as coded below?
  2. how do I specify a <tfood> tag that summarises the values of the tables for each column, using also the “factor” given from the header <input> field?

would like to get a result something like this:

image

<table>
  <thead>
    <tr>
      <th>factor SUM</th>
      <th><input type="number" name="factor_savings" value=2></th>
    </tr>
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Sum</td>
      <td>$360</td>
    </tr>
  </tfoot>
</table>

thanks Michael

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
plumdogcommented, Jan 17, 2018

Both of these are possible, but you have to do much of the work yourself - they aren’t built into flask table.

Firstly, the extra th:

Something like:

from __future__ import unicode_literals
from flask_table import Table, Col
from flask_table.html import element


class ExtraTHTable(Table):

    def thead(self):
        normal_ths = ''.join(
            self.th(col_key, col)
            for col_key, col in self._cols.items()
            if col.show)
        extra_ths = ''.join(
            self.extra_th(col_key, col)
            for col_key, col in self._cols.items()
            if col.show)
        content = '\n'.join([
            element('tr', content=normal_ths, escape_content=False),
            element('tr', content=extra_ths, escape_content=False),
        ])
        return element(
            'thead',
            attrs=self.get_thead_attrs(),
            content=content,
            escape_content=False,
        )

    def extra_th(self, key, col):
        # With the key and column, return the additional th for that
        # column
        return element('th', content='Extra th for {}'.format(key))


class MyExtraTHTable(ExtraTHTable):
    name = Col('Name')
    desc = Col('Desc')


data = [
    dict(name='name1', desc='desc1'),
]
print MyExtraTHTable(data).__html__()

Secondly, the tfoot:

from __future__ import unicode_literals
from flask_table import Table, Col
from flask_table.html import element


class WithTFootTable(Table):

    def __html__(self):
        tbody = self.tbody()
        if tbody or self.allow_empty:
            content = '\n{thead}\n{tbody}\n{tfoot}\n'.format(
                thead=self.thead(),
                tbody=tbody,
                tfoot=self.tfoot(),
            )
            return element(
                'table',
                attrs=self.get_html_attrs(),
                content=content,
                escape_content=False)
        else:
            return element('p', content=self.no_items)

    def tfoot(self):
        tr_content = ''.join(self.tfoot_td(col_key, col) for col_key, col in self._cols.items() if col.show)
        content = element('tr', content=tr_content, escape_content=False)
        return element('tfoot', content=content, escape_content=False)

    def tfoot_td(self, key, col):
        # note that here you have access to the items with self.items
        return element('td', content='tfoot td for {}'.format(key))


class MyTFootTable(WithTFootTable):
    name = Col('Name')
    desc = Col('Desc')


data = [
    dict(name='name1', desc='desc1'),
]
print MyTFootTable(data).__html__()

Hopefully, you should be able to run those and work out what is going on. For both, I poked into the code for the Table class in https://github.com/plumdog/flask_table/blob/master/flask_table/table.py and have overridden the relevant methods. (The current state of the code doesn’t make this sort of thing super easy, but it is possible).

Also, you should be able to use those two together without a problem (though I haven’t actually tested it!)

Hope that’s useful!

0reactions
giuliohomecommented, Jan 27, 2021

@plumdog Thank yuo for the footer implementation! I’ve re-used it today in my project to work with DataTables, to filter columns with multiple values (where a footer like that is required in fact)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tables in HTML documents
Table cells may either contain "header" information (see the TH element) or "data" (see the TD element). Cells may span multiple rows and...
Read more >
<tfoot>: The Table Foot element - HTML - MDN Web Docs
The HTML element defines a set of rows summarizing the columns of the table. ... Permitted content, Zero or more <tr> elements.
Read more >
Types of Therapeutic Diets
A therapeutic diet is a meal plan that controls the intake of certain foods or nutrients. It is part of the treatment of...
Read more >
Dietary Guidelines for Americans | health.gov
The Dietary Guidelines for Americans (Dietary Guidelines) provides advice on what to eat and drink to meet nutrient needs, promote health, and prevent ......
Read more >
<table rules=""> » - HTML
7 Values of the rules Attribute; 8 All Attributes of table Element ... RULES and FRAME have an annoying way of changing each...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found