Skip to content

Commit

Permalink
Merge pull request #582 from iommirocks/thead-conf-improvement
Browse files Browse the repository at this point in the history
Make it possible to target the `<thead>` of a table with configuration
  • Loading branch information
jlubcke authored Sep 30, 2024
2 parents 10fd9b9 + 66af2cd commit 8ed87ec
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
2 changes: 2 additions & 0 deletions iommi/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def validate_styles(*, additional_classes: List[Type] = None, default_classes=No
from iommi.form import FieldGroup
from iommi.fragment import Container, Header
from iommi.live_edit import LiveEditPage
from iommi.table import HeaderConfig
from iommi.menu import (
MenuBase,
get_debug_menu,
Expand All @@ -250,6 +251,7 @@ def validate_styles(*, additional_classes: List[Type] = None, default_classes=No
Filter,
Form,
Header,
HeaderConfig,
LiveEditPage,
Menu,
MenuBase,
Expand Down
3 changes: 3 additions & 0 deletions iommi/style_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
),
header__attrs__class__iommi_sort_header=lambda header, **_: header.url is not None,
),
HeaderConfig=dict(
tag='thead',
),
Paginator=dict(
show_always=False,
template='iommi/table/paginator.html',
Expand Down
42 changes: 38 additions & 4 deletions iommi/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,15 +1208,46 @@ class TemplateConfig(RefinableObject):
template: str = Refinable()


class HeaderConfig(Traversable):
class HeaderConfig(Traversable, Tag):
tag: str = EvaluatedRefinable()
attrs: Attrs = SpecialEvaluatedRefinable()
template: Union[str, Template] = EvaluatedRefinable()
extra: Dict[str, Any] = Refinable()
extra_evaluated: Dict[str, Any] = Refinable()
url = EvaluatedRefinable()

def __html__(self):
return render_template(self.get_request(), self.template, self.iommi_parent().iommi_evaluate_parameters())
if self.template:
return render_template(self.iommi_parent().get_request(), self.template, self.iommi_evaluate_parameters())

return self.render()

def render(self):
children = {}
header_levels = self.iommi_parent().header_levels
assert len(header_levels) in (1, 2)

def header_list_to_transient_fragment(header_list):
return TransientFragment(
tag='tr',
attrs={},
children={f'header_{i}': v for i, v in enumerate(header_list)},
parent=self,
)

if len(header_levels) > 1:
children['superheader'] = header_list_to_transient_fragment(header_levels[0])

children['subheader'] = header_list_to_transient_fragment(header_levels[-1])

return (
TransientFragment(
tag=self.tag,
attrs=self.attrs,
children=children,
parent=self,
)
.__html__()
)

def __str__(self):
return self.__html__()
Expand Down Expand Up @@ -1272,6 +1303,9 @@ def __init__(
self._name = 'header'
self.attrs = evaluate_attrs(self, table=table, column=column, header=self)

def __html__(self):
return self.rendered

@property
def rendered(self):
return render_template(self.table.get_request(), self.template, dict(header=self))
Expand Down Expand Up @@ -1806,7 +1840,7 @@ def post_bulk_edit(table, queryset, updates, **_):
row__attrs={'data-pk': lambda row, **_: getattr(row, 'pk', None)},
row__template=None,
cell__tag='td',
header__template='iommi/table/table_header_rows.html',
header=EMPTY,
h_tag__call_target=Header,
actions_template='iommi/form/actions.html',
actions_below=False,
Expand Down
24 changes: 1 addition & 23 deletions iommi/table__tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1835,28 +1835,6 @@ class TestTable(NoSortTable):
)


def test_no_header_template(NoSortTable): # noqa: N803
class TestTable(NoSortTable):
class Meta:
header__template = None

foo = Column()

rows = [Struct(foo="bar")]

verify_table_html(
table=TestTable(rows=rows),
# language=html
expected_html="""
<table class="table" >
<tbody>
<tr> <td> bar </td> </tr>
</tbody>
</table>
""",
)


def test_row_template(NoSortTable): # noqa: N803
class TestTable(NoSortTable):
foo = Column()
Expand Down Expand Up @@ -2828,7 +2806,7 @@ class FooTable(Table):
# language=html
expected_html="""
<table class="table" data-iommi-path="" data-iommi-type="FooTable">
<thead>
<thead data-iommi-path="header" data-iommi-type="HeaderConfig">
<tr>
<th class="superheader" colspan="1" data-iommi-type="ColumnHeader"> foo </th>
</tr>
Expand Down
9 changes: 0 additions & 9 deletions iommi/templates/iommi/table/table_header_rows.html

This file was deleted.

0 comments on commit 8ed87ec

Please sign in to comment.