Skip to content

Commit

Permalink
Merge f5b3ed3 into 8b9f31c
Browse files Browse the repository at this point in the history
  • Loading branch information
plumdog committed Jul 13, 2017
2 parents 8b9f31c + f5b3ed3 commit a731abf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ Extra things:

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

* You can set attributes on the `table` element by setting
`html_attrs` as a dict on the class element, or by passing it as a
kwarg to the table. Some common attributes have shortcuts, namely
`table_id` (a string), `classes` (a list of strings), and `border`
(a boolean).

* 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
Expand Down
14 changes: 11 additions & 3 deletions flask_table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ class Table(with_metaclass(TableMeta)):
"""

# For setting attributes on the <table> element.
html_attrs = None
classes = []
table_id = None
border = False

thead_attrs = None
thead_classes = []
allow_sort = False
Expand All @@ -61,7 +65,7 @@ class Table(with_metaclass(TableMeta)):

def __init__(self, items, classes=None, thead_classes=None,
sort_by=None, sort_reverse=False, no_items=None,
table_id=None, border=False):
table_id=None, border=None, html_attrs=None):
self.items = items
self.sort_by = sort_by
self.sort_reverse = sort_reverse
Expand All @@ -71,8 +75,12 @@ def __init__(self, items, classes=None, thead_classes=None,
self.thead_classes = thead_classes
if no_items is not None:
self.no_items = no_items
self.table_id = table_id
self.border = border
if table_id is not None:
self.table_id = table_id
if html_attrs is not None:
self.html_attrs = html_attrs
if border is not None:
self.border = border

def get_html_attrs(self):
attrs = dict(self.html_attrs) if self.html_attrs else {}
Expand Down
45 changes: 44 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ def table_cls(self, table_cls):
def assert_html_equivalent_from_file(self, d, name, items=[], **kwargs):
table_id = kwargs.get('table_id', None)
border = kwargs.get('border', False)
html_attrs = kwargs.get('html_attrs', None)
tab = kwargs.get('tab', self.table_cls(
items, table_id=table_id, border=border))
items, table_id=table_id, border=border, html_attrs=html_attrs))
if kwargs.get('print_html'):
print(tab.__html__())
html = self.get_html(d, name)
Expand Down Expand Up @@ -121,6 +122,18 @@ def test_one(self):
'tableid_test', 'test_one', items, table_id='Test table ID')


class TableIDOnClassTest(TableTest):

class MyTable(Table):
table_id = 'Test table ID'
name = Col('Name Heading')

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


class BorderTest(TableTest):

class MyTable(Table):
Expand Down Expand Up @@ -915,3 +928,33 @@ def test_overwrite_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file(
'column_html_attrs_test', 'test_overwrite_attrs', items)


class TableHTMLAttrsTest(TableTest):

class MyTable(Table):
name = Col('Name Heading')

def test_html_attrs(self):
items = [Item(name='one')]
html_attrs = {
'data-myattr': 'myval',
}
self.assert_html_equivalent_from_file(
'html_attrs_test',
'test_html_attrs',
items,
html_attrs=html_attrs)


class TableHTMLAttrsOnClassTest(TableTest):

class MyTable(Table):
html_attrs = {
'data-myattr': 'myval',
}
name = Col('Name Heading')

def test_html_attrs(self):
items = [Item(name='one')]
self.assert_html_equivalent_from_file('html_attrs_test', 'test_html_attrs', items)
10 changes: 10 additions & 0 deletions tests/html/html_attrs_test/test_html_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table data-myattr="myval">
<thead>
<tr><th>Name Heading</th></tr>
</thead>
<tbody>
<tr>
<td>one</td>
</tr>
</tbody>
</table>

0 comments on commit a731abf

Please sign in to comment.