Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.utils.safestring import mark_safe
4from .base import Element
6# Exports
8__all__ = (
9 "Column",
10 "Row",
11 "Table",
12)
14# Classes
17class Column(Element):
18 """A column (header) in an HTML table."""
20 def __init__(self, name, help_text=None, label=None, **kwargs):
21 """Initialize the column.
23 :param name: The programmatic name of the column. For example, the field name.
24 :type name: str
26 :param help_text: Help text for the column.
27 :type help_text: str
29 :param label: The column label. If omitted, the name is converted into a label.
30 :type label: str
32 """
33 self.help_text = help_text
34 self.label = label or name.replace("_", " ").title()
35 self.name = name
37 super().__init__("th", content=str(self.label), **kwargs)
39 @property
40 def title(self):
41 """Get the column label. Alias for ``label``."""
42 return self.label
45class Row(Element):
46 """A row in an HTML table."""
48 def __init__(self, data, **kwargs):
49 """Initialize the row.
51 :param data: The data to be included in the row.
52 :type data: list | tuple
54 """
55 super().__init__("tr", **kwargs)
57 self._data = data
59 @property
60 def data(self):
61 """Alias for ``_data``."""
62 return self._data
64 @mark_safe
65 def to_html(self):
66 """Export the row to HTML."""
67 a = list()
68 a.append(self.get_open_tag())
69 for d in self._data:
70 a.append("<td>%s</td>" % d)
72 a.append(self.get_close_tag())
74 return "\n".join(a)
77class Table(Element):
78 """An HTML table."""
80 def __init__(self, caption=None, columns=None, rows=None, **kwargs):
81 """Initialize the table.
83 :param caption: Optional caption for the table.
84 :type caption: str
86 :param columns: The columns to be included in the table.
87 :type columns: list[superdjango.html.library.Column]
89 :param rows: The rows to be included in the table.
90 :type rows: list[superdjango.html.library.Row]
92 """
93 super().__init__("table", **kwargs)
95 self.caption = caption
96 self.columns = columns or list()
97 self.rows = rows or list()
99 def __iter__(self):
100 return iter(self.rows)
102 def column(self, name, help_text=None, label=None, **kwargs):
103 """Add a column to the table.
105 :param name: The programmatic name of the column. For example, the field name.
106 :type name: str
108 :param help_text: Help text for the column.
109 :type help_text: str
111 :param label: The column label. If omitted, the name is converted into a label.
112 :type label: str
114 :rtype: superdjango.html.library.Column
116 """
117 column = Column(name, help_text=help_text, label=label, **kwargs)
118 self.columns.append(column)
120 return column
122 def row(self, data, **kwargs):
123 """Add a row to the table.
125 :param data: The data to be included in the row.
126 :type data: list | tuple
128 :rtype: superdjango.html.library.Row
130 """
131 row = Row(data, **kwargs)
132 self.rows.append(row)
134 return row
136 @mark_safe
137 def to_html(self):
138 """Export the table as HTML.
140 :rtype: str
142 """
143 a = list()
144 a.append(self.get_open_tag())
146 if self.caption is not None:
147 caption = Element("caption", content=self.caption)
148 a.append(caption.to_html())
150 a.append("<thead><tr>")
151 for column in self.columns:
152 a.append(column.to_html())
153 a.append("</tr></thead>")
155 a.append("<tbody>")
157 for row in self.rows:
158 a.append(row.to_html())
160 a.append("</tbody>")
162 a.append(self.get_close_tag())
164 return "\n".join(a)