Skip to content

Commit

Permalink
Merge cb9abee into 2e54325
Browse files Browse the repository at this point in the history
  • Loading branch information
plumdog committed Feb 7, 2017
2 parents 2e54325 + cb9abee commit 99dbb6f
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ Extra things:

* Adding border to your table by just setting attribute `border=True` while creating a table.

* You can pass attributes to the `td` and `th` elements by passing a
dict of attributes as `td_html_attrs` or `th_html_attrs` when creating a
Col. Or as `column_html_attrs` to apply the attributes to both the `th`s
and the `td`s. (Any that you pass in `th_html_attrs` or `td_html_attrs` will
overwrite any that you also pass with `column_html_attrs`.) See
examples/column_html_attrs.py for more.

* There are also LinkCol and ButtonCol that allow links and buttons,
which is where the Flask-specific-ness comes in.

Expand Down
96 changes: 96 additions & 0 deletions examples/column_html_attrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from flask_table import Table, Col


"""What if we need to apply some classes (or any other attribute) to
the td and th HTML attributes? Maybe you want this so you can apply
some styling or attach some javascript.
NB: This example just handles the adding of some fixed attributes to a
column. If you want to do something dynamic (eg, only applying an
attribute to certain rows, see the rows.py example).
This example is not very "real world" but should show how the setting
of elements works and what things you can do.
"""


class Item(object):
def __init__(self, name, description):
self.name = name
self.description = description


class ItemTable(Table):
name = Col(
'Name',
# Apply this class to both the th and all tds in this column
column_html_attrs={'class': 'my-name-class'},
)
description = Col(
'Description',
# Apply these to both
column_html_attrs={
'data-something': 'my-data',
'class': 'my-description-class'},
# Apply this to just the th
th_html_attrs={'data-something-else': 'my-description-th-class'},
# Apply this to just the td - note that this will things from
# overwrite column_html_attrs.
td_html_attrs={'data-something': 'my-td-only-data'},
)


def main():
items = [Item('Name1', 'Description1'),
Item('Name2', 'Description2'),
Item('Name3', 'Description3')]

table = ItemTable(items)

# or {{ table }} in jinja
print(table.__html__())

"""Outputs:
<table>
<thead>
<tr>
<th class="my-name-class">Name</th>
<th class="my-description-class"
data-something="my-data"
data-something-else="my-description-th-class">
Description
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="my-name-class">Name1</td>
<td class="my-description-class"
data-something="my-td-only-data">
Description1
</td>
</tr>
<tr>
<td class="my-name-class">Name2</td>
<td class="my-description-class"
data-something="my-td-only-data">
Description2
</td>
</tr>
<tr>
<td class="my-name-class">Name3</td>
<td class="my-description-class"
data-something="my-td-only-data">
Description3
</td>
</tr>
</tbody>
</table>
Except it doesn't bother to prettify the output.
"""

if __name__ == '__main__':
main()
16 changes: 14 additions & 2 deletions flask_table/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class Col(object):
_counter = 0

def __init__(self, name, attr=None, attr_list=None,
allow_sort=True, show=True):
allow_sort=True, show=True,
th_html_attrs=None, td_html_attrs=None,
column_html_attrs=None):
self.name = name
self.allow_sort = allow_sort
self._counter_val = Col._counter
Expand All @@ -66,6 +68,12 @@ def __init__(self, name, attr=None, attr_list=None,
self.attr_list = attr.split('.')
self.show = show

column_html_attrs = column_html_attrs or {}
self.td_html_attrs = column_html_attrs.copy()
self.td_html_attrs.update(td_html_attrs or {})
self.th_html_attrs = column_html_attrs.copy()
self.th_html_attrs.update(th_html_attrs or {})

Col._counter += 1

def get_attr_list(self, attr):
Expand All @@ -85,7 +93,11 @@ def from_attr_list(self, item, attr_list):

def td(self, item, attr):
content = self.td_contents(item, self.get_attr_list(attr))
return element('td', content=content, escape_content=False)
return element(
'td',
content=content,
escape_content=False,
attrs=self.td_html_attrs)

def td_contents(self, item, attr_list):
"""Given an item and an attr, return the contents of the td.
Expand Down
1 change: 1 addition & 0 deletions flask_table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def th(self, col_key, col):
'th',
content=self.th_contents(col_key, col),
escape_content=False,
attrs=col.th_html_attrs,
)

def sort_url(self, col_id, reverse=False):
Expand Down
62 changes: 62 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,65 @@ def test_one(self):
Item(b='r2bsc1', c='r2bsc2')])]
self.assert_html_equivalent_from_file(
'nestedcol_test', 'test_one', items)


class ColumnAttrsTest(TableTest):

class MyTable(Table):
name = Col('Name Heading', column_html_attrs={'class': 'myclass'})

def test_column_html_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file(
'column_html_attrs_test', 'test_column_html_attrs', items)


class TDAttrsTest(TableTest):

class MyTable(Table):
name = Col('Name Heading', td_html_attrs={'class': 'myclass'})

def test_td_html_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file(
'column_html_attrs_test', 'test_td_html_attrs', items)


class THAttrsTest(TableTest):

class MyTable(Table):
name = Col('Name Heading', th_html_attrs={'class': 'myclass'})

def test_th_html_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file(
'column_html_attrs_test', 'test_th_html_attrs', items)


class BothAttrsTest(TableTest):

class MyTable(Table):
name = Col(
'Name Heading',
th_html_attrs={'class': 'my-th-class'},
td_html_attrs={'class': 'my-td-class'})

def test_both_html_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file(
'column_html_attrs_test', 'test_both_html_attrs', items)


class AttrsOverwriteTest(TableTest):

class MyTable(Table):
name = Col(
'Name Heading',
column_html_attrs={'data-other': 'mydata', 'class': 'myclass'},
th_html_attrs={'class': 'my-th-class'},
td_html_attrs={'class': 'my-td-class'})

def test_overwrite_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file(
'column_html_attrs_test', 'test_overwrite_attrs', items)
10 changes: 10 additions & 0 deletions tests/html/column_html_attrs_test/test_both_html_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table>
<thead>
<tr><th class="my-th-class">Name Heading</th></tr>
</thead>
<tbody>
<tr>
<td class="my-td-class">one</td>
</tr>
</tbody>
</table>
10 changes: 10 additions & 0 deletions tests/html/column_html_attrs_test/test_column_html_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table>
<thead>
<tr><th class="myclass">Name Heading</th></tr>
</thead>
<tbody>
<tr>
<td class="myclass">one</td>
</tr>
</tbody>
</table>
10 changes: 10 additions & 0 deletions tests/html/column_html_attrs_test/test_overwrite_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table>
<thead>
<tr><th class="my-th-class" data-other="mydata">Name Heading</th></tr>
</thead>
<tbody>
<tr>
<td class="my-td-class" data-other="mydata">one</td>
</tr>
</tbody>
</table>
10 changes: 10 additions & 0 deletions tests/html/column_html_attrs_test/test_td_html_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table>
<thead>
<tr><th>Name Heading</th></tr>
</thead>
<tbody>
<tr>
<td class="myclass">one</td>
</tr>
</tbody>
</table>
10 changes: 10 additions & 0 deletions tests/html/column_html_attrs_test/test_th_html_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table>
<thead>
<tr><th class="myclass">Name Heading</th></tr>
</thead>
<tbody>
<tr>
<td>one</td>
</tr>
</tbody>
</table>

0 comments on commit 99dbb6f

Please sign in to comment.