pyams-table 1.0.0__py3.5.egg
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EGG-INFO/PKG-INFO +55 -0
- EGG-INFO/SOURCES.txt +32 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/namespace_packages.txt +1 -0
- EGG-INFO/not-zip-safe +1 -0
- EGG-INFO/requires.txt +14 -0
- EGG-INFO/top_level.txt +1 -0
- pyams_table/__init__.py +29 -0
- pyams_table/__pycache__/__init__.cpython-35.pyc +0 -0
- pyams_table/__pycache__/batch.cpython-35.pyc +0 -0
- pyams_table/__pycache__/column.cpython-35.pyc +0 -0
- pyams_table/__pycache__/header.cpython-35.pyc +0 -0
- pyams_table/__pycache__/include.cpython-35.pyc +0 -0
- pyams_table/__pycache__/interfaces.cpython-35.pyc +0 -0
- pyams_table/__pycache__/table.cpython-35.pyc +0 -0
- pyams_table/__pycache__/testing.cpython-35.pyc +0 -0
- pyams_table/__pycache__/value.cpython-35.pyc +0 -0
- pyams_table/batch.py +159 -0
- pyams_table/column.py +495 -0
- pyams_table/doctests/README.rst +668 -0
- pyams_table/doctests/batch.rst +660 -0
- pyams_table/doctests/column.rst +1274 -0
- pyams_table/doctests/miscellaneous.rst +123 -0
- pyams_table/doctests/sequence.rst +267 -0
- pyams_table/doctests/sort.rst +388 -0
- pyams_table/header.py +102 -0
- pyams_table/include.py +27 -0
- pyams_table/interfaces.py +249 -0
- pyams_table/locales/fr/LC_MESSAGES/pyams_table.mo +0 -0
- pyams_table/locales/fr/LC_MESSAGES/pyams_table.po +40 -0
- pyams_table/locales/pyams_table.pot +41 -0
- pyams_table/table.py +383 -0
- pyams_table/testing.py +129 -0
- pyams_table/tests/__init__.py +29 -0
- pyams_table/tests/__pycache__/__init__.cpython-35.pyc +0 -0
- pyams_table/tests/__pycache__/test_utilsdocs.cpython-35.pyc +0 -0
- pyams_table/tests/__pycache__/test_utilsdocstrings.cpython-35.pyc +0 -0
- pyams_table/tests/test_utilsdocs.py +60 -0
- pyams_table/tests/test_utilsdocstrings.py +65 -0
- pyams_table/value.py +51 -0
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
from pyams_table.interfaces import IValues===================
|
|
2
|
+
PyAMS_table package
|
|
3
|
+
===================
|
|
4
|
+
|
|
5
|
+
Introduction
|
|
6
|
+
------------
|
|
7
|
+
|
|
8
|
+
This package is composed of a set of utility functions, usable into any Pyramid application.
|
|
9
|
+
|
|
10
|
+
The goal of this package is to offer a modular table rendering library. We use
|
|
11
|
+
the content provider pattern and the column are implemented as adapters which
|
|
12
|
+
will give us a powerful base concept.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
Some important concepts we use
|
|
16
|
+
------------------------------
|
|
17
|
+
|
|
18
|
+
- separate implementation in update render parts, This allows to manipulate
|
|
19
|
+
data after update call and before we render them.
|
|
20
|
+
|
|
21
|
+
- allow to use page templates if needed. By default all is done in python.
|
|
22
|
+
|
|
23
|
+
- allow to use the rendered batch outside the existing table HTML part.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
No skins
|
|
27
|
+
--------
|
|
28
|
+
|
|
29
|
+
This package does not provide any kind of template or skin support. Most the
|
|
30
|
+
time if you need to render a table, you will use your own skin concept. This means
|
|
31
|
+
you can render the table or batch within your own templates. This will ensure
|
|
32
|
+
that we have as few dependencies as possible in this package and the package
|
|
33
|
+
can get reused with any skin concept.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
Note
|
|
37
|
+
----
|
|
38
|
+
|
|
39
|
+
As you probably know, batching is only possible after sorting columns. This is
|
|
40
|
+
a nightmare if it comes to performance. The reason is, all data need to get
|
|
41
|
+
sorted before the batch can start at the given position. And sorting can most
|
|
42
|
+
of the time only be done by touching each object. This means you have to be careful
|
|
43
|
+
if you are using a large set of data, even if you use batching.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
Sample data setup
|
|
47
|
+
-----------------
|
|
48
|
+
|
|
49
|
+
>>> from pyramid.testing import setUp, tearDown
|
|
50
|
+
>>> from pyams_table.testing import setup_adapters
|
|
51
|
+
|
|
52
|
+
>>> config = setUp()
|
|
53
|
+
>>> setup_adapters(config.registry)
|
|
54
|
+
>>> root = {}
|
|
55
|
+
|
|
56
|
+
Let's create a sample container which we can use as our iterable context:
|
|
57
|
+
|
|
58
|
+
>>> from zope.interface import implementer, Interface
|
|
59
|
+
>>> from zope.component import provideAdapter
|
|
60
|
+
>>> from zope.container.interfaces import IContainer
|
|
61
|
+
>>> from pyramid.interfaces import IRequest
|
|
62
|
+
>>> from pyams_table.interfaces import ISequenceTable, ITable, IValues
|
|
63
|
+
>>> from pyams_table.value import ValuesForContainer, ValuesForSequence
|
|
64
|
+
|
|
65
|
+
>>> from zope.container import btree
|
|
66
|
+
>>> class Container(btree.BTreeContainer):
|
|
67
|
+
... """Sample container."""
|
|
68
|
+
... __name__ = 'container'
|
|
69
|
+
>>> container = Container()
|
|
70
|
+
|
|
71
|
+
and set a parent for the container:
|
|
72
|
+
|
|
73
|
+
>>> root['container'] = container
|
|
74
|
+
|
|
75
|
+
and create a sample content object which we use as container item:
|
|
76
|
+
|
|
77
|
+
>>> class Content(object):
|
|
78
|
+
... """Sample content."""
|
|
79
|
+
... def __init__(self, title, number):
|
|
80
|
+
... self.title = title
|
|
81
|
+
... self.number = number
|
|
82
|
+
|
|
83
|
+
Now setup some items:
|
|
84
|
+
|
|
85
|
+
>>> container['first'] = Content('First', 1)
|
|
86
|
+
>>> container['second'] = Content('Second', 2)
|
|
87
|
+
>>> container['third'] = Content('Third', 3)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Table
|
|
91
|
+
-----
|
|
92
|
+
|
|
93
|
+
Create a test request and represent the table:
|
|
94
|
+
|
|
95
|
+
>>> from pyramid.testing import DummyRequest
|
|
96
|
+
>>> from pyams_table import table
|
|
97
|
+
|
|
98
|
+
>>> request = DummyRequest()
|
|
99
|
+
>>> plain_table = table.Table(container, request)
|
|
100
|
+
>>> plain_table.css_class_sorted_on = None
|
|
101
|
+
|
|
102
|
+
Now we can update and render the table. As you can see with an empty container
|
|
103
|
+
we will not get anything that looks like a table. We just get an empty string:
|
|
104
|
+
|
|
105
|
+
>>> plain_table.update()
|
|
106
|
+
>>> plain_table.render()
|
|
107
|
+
''
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
Column Adapter
|
|
111
|
+
--------------
|
|
112
|
+
|
|
113
|
+
We can create a column for our table:
|
|
114
|
+
|
|
115
|
+
>>> from pyams_table import interfaces
|
|
116
|
+
>>> from pyams_table import column
|
|
117
|
+
|
|
118
|
+
>>> class TitleColumn(column.Column):
|
|
119
|
+
...
|
|
120
|
+
... weight = 10
|
|
121
|
+
... header = u'Title'
|
|
122
|
+
...
|
|
123
|
+
... def render_cell(self, item):
|
|
124
|
+
... return u'Title: %s' % item.title
|
|
125
|
+
|
|
126
|
+
Now we can register the column:
|
|
127
|
+
|
|
128
|
+
>>> config.registry.registerAdapter(TitleColumn,
|
|
129
|
+
... (None, None, interfaces.ITable), provided=interfaces.IColumn,
|
|
130
|
+
... name='first_column')
|
|
131
|
+
|
|
132
|
+
Now we can render the table again:
|
|
133
|
+
|
|
134
|
+
>>> plain_table.update()
|
|
135
|
+
>>> print(plain_table.render())
|
|
136
|
+
<table>
|
|
137
|
+
<thead>
|
|
138
|
+
<tr>
|
|
139
|
+
<th>Title</th>
|
|
140
|
+
</tr>
|
|
141
|
+
</thead>
|
|
142
|
+
<tbody>
|
|
143
|
+
<tr>
|
|
144
|
+
<td>Title: First</td>
|
|
145
|
+
</tr>
|
|
146
|
+
<tr>
|
|
147
|
+
<td>Title: Second</td>
|
|
148
|
+
</tr>
|
|
149
|
+
<tr>
|
|
150
|
+
<td>Title: Third</td>
|
|
151
|
+
</tr>
|
|
152
|
+
</tbody>
|
|
153
|
+
</table>
|
|
154
|
+
|
|
155
|
+
We can also use the predefined name column:
|
|
156
|
+
|
|
157
|
+
>>> config.registry.registerAdapter(column.NameColumn,
|
|
158
|
+
... (None, None, interfaces.ITable), provided=interfaces.IColumn,
|
|
159
|
+
... name='second_column')
|
|
160
|
+
|
|
161
|
+
Now we will get an additional column:
|
|
162
|
+
|
|
163
|
+
>>> plain_table.update()
|
|
164
|
+
>>> print(plain_table.render())
|
|
165
|
+
<table>
|
|
166
|
+
<thead>
|
|
167
|
+
<tr>
|
|
168
|
+
<th>Name</th>
|
|
169
|
+
<th>Title</th>
|
|
170
|
+
</tr>
|
|
171
|
+
</thead>
|
|
172
|
+
<tbody>
|
|
173
|
+
<tr>
|
|
174
|
+
<td>first</td>
|
|
175
|
+
<td>Title: First</td>
|
|
176
|
+
</tr>
|
|
177
|
+
<tr>
|
|
178
|
+
<td>second</td>
|
|
179
|
+
<td>Title: Second</td>
|
|
180
|
+
</tr>
|
|
181
|
+
<tr>
|
|
182
|
+
<td>third</td>
|
|
183
|
+
<td>Title: Third</td>
|
|
184
|
+
</tr>
|
|
185
|
+
</tbody>
|
|
186
|
+
</table>
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
Colspan
|
|
190
|
+
-------
|
|
191
|
+
|
|
192
|
+
Now let's show how we can define a colspan condition of 2 for a column:
|
|
193
|
+
|
|
194
|
+
>>> class ColspanColumn(column.NameColumn):
|
|
195
|
+
...
|
|
196
|
+
... weight = 999
|
|
197
|
+
...
|
|
198
|
+
... def get_colspan(self, item):
|
|
199
|
+
... # colspan condition
|
|
200
|
+
... if item.__name__ == 'first':
|
|
201
|
+
... return 2
|
|
202
|
+
... else:
|
|
203
|
+
... return 0
|
|
204
|
+
...
|
|
205
|
+
... def render_head_cell(self):
|
|
206
|
+
... return 'Colspan'
|
|
207
|
+
...
|
|
208
|
+
... def render_cell(self, item):
|
|
209
|
+
... return 'colspan: %s' % item.title
|
|
210
|
+
|
|
211
|
+
Now we register this column adapter as colspanColumn:
|
|
212
|
+
|
|
213
|
+
>>> config.registry.registerAdapter(ColspanColumn,
|
|
214
|
+
... (None, None, interfaces.ITable), provided=interfaces.IColumn,
|
|
215
|
+
... name='colspan_column')
|
|
216
|
+
|
|
217
|
+
Now you can see that the colspan of the ColspanAdapter is larger than the table.
|
|
218
|
+
This will raise a ValueError:
|
|
219
|
+
|
|
220
|
+
>>> plain_table.update()
|
|
221
|
+
Traceback (most recent call last):
|
|
222
|
+
...
|
|
223
|
+
ValueError: Colspan for column '<ColspanColumn 'colspan_column'>' is larger than the table.
|
|
224
|
+
|
|
225
|
+
But if we set the column as first row, it will render the colspan correctly:
|
|
226
|
+
|
|
227
|
+
>>> class CorrectColspanColumn(ColspanColumn):
|
|
228
|
+
... """Colspan with correct weight."""
|
|
229
|
+
...
|
|
230
|
+
... weight = -1 # NameColumn is 0
|
|
231
|
+
|
|
232
|
+
Register and render the table again:
|
|
233
|
+
|
|
234
|
+
>>> config.registry.registerAdapter(CorrectColspanColumn,
|
|
235
|
+
... (None, None, interfaces.ITable), provided=interfaces.IColumn,
|
|
236
|
+
... name='colspan_column')
|
|
237
|
+
|
|
238
|
+
>>> plain_table.update()
|
|
239
|
+
>>> print(plain_table.render())
|
|
240
|
+
<table>
|
|
241
|
+
<thead>
|
|
242
|
+
<tr>
|
|
243
|
+
<th>Colspan</th>
|
|
244
|
+
<th>Name</th>
|
|
245
|
+
<th>Title</th>
|
|
246
|
+
</tr>
|
|
247
|
+
</thead>
|
|
248
|
+
<tbody>
|
|
249
|
+
<tr>
|
|
250
|
+
<td colspan="2">colspan: First</td>
|
|
251
|
+
<td>Title: First</td>
|
|
252
|
+
</tr>
|
|
253
|
+
<tr>
|
|
254
|
+
<td>colspan: Second</td>
|
|
255
|
+
<td>second</td>
|
|
256
|
+
<td>Title: Second</td>
|
|
257
|
+
</tr>
|
|
258
|
+
<tr>
|
|
259
|
+
<td>colspan: Third</td>
|
|
260
|
+
<td>third</td>
|
|
261
|
+
<td>Title: Third</td>
|
|
262
|
+
</tr>
|
|
263
|
+
</tbody>
|
|
264
|
+
</table>
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
Setup columns
|
|
268
|
+
-------------
|
|
269
|
+
|
|
270
|
+
The existing implementation allows us to define a table in a class without
|
|
271
|
+
using the modular adapter pattern for columns.
|
|
272
|
+
|
|
273
|
+
First we need to define a column which can render a value for our items:
|
|
274
|
+
|
|
275
|
+
>>> class SimpleColumn(column.Column):
|
|
276
|
+
...
|
|
277
|
+
... weight = 0
|
|
278
|
+
...
|
|
279
|
+
... def render_cell(self, item):
|
|
280
|
+
... return item.title
|
|
281
|
+
|
|
282
|
+
Let's define our table which defines the columns explicitly. you can also see
|
|
283
|
+
that we do not return the columns in the correct order:
|
|
284
|
+
|
|
285
|
+
>>> class PrivateTable(table.Table):
|
|
286
|
+
... css_class_sorted_on = None
|
|
287
|
+
...
|
|
288
|
+
... def setup_columns(self):
|
|
289
|
+
... first_column = TitleColumn(self.context, self.request, self)
|
|
290
|
+
... first_column.__name__ = 'title'
|
|
291
|
+
... first_column.weight = 1
|
|
292
|
+
... second_column = SimpleColumn(self.context, self.request, self)
|
|
293
|
+
... second_column.__name__ = 'simple'
|
|
294
|
+
... second_column.weight = 2
|
|
295
|
+
... second_column.header = 'The second column'
|
|
296
|
+
... return [second_column, first_column]
|
|
297
|
+
|
|
298
|
+
Now we can create, update and render the table and see that this renders a nice
|
|
299
|
+
table too:
|
|
300
|
+
|
|
301
|
+
>>> private_table = PrivateTable(container, request)
|
|
302
|
+
>>> private_table.update()
|
|
303
|
+
>>> print(private_table.render())
|
|
304
|
+
<table>
|
|
305
|
+
<thead>
|
|
306
|
+
<tr>
|
|
307
|
+
<th>Title</th>
|
|
308
|
+
<th>The second column</th>
|
|
309
|
+
</tr>
|
|
310
|
+
</thead>
|
|
311
|
+
<tbody>
|
|
312
|
+
<tr>
|
|
313
|
+
<td>Title: First</td>
|
|
314
|
+
<td>First</td>
|
|
315
|
+
</tr>
|
|
316
|
+
<tr>
|
|
317
|
+
<td>Title: Second</td>
|
|
318
|
+
<td>Second</td>
|
|
319
|
+
</tr>
|
|
320
|
+
<tr>
|
|
321
|
+
<td>Title: Third</td>
|
|
322
|
+
<td>Third</td>
|
|
323
|
+
</tr>
|
|
324
|
+
</tbody>
|
|
325
|
+
</table>
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
Cascading Style Sheet
|
|
329
|
+
---------------------
|
|
330
|
+
|
|
331
|
+
Our table and column implementation supports CSS class assignment. Let's define
|
|
332
|
+
a table and columns with some css class values:
|
|
333
|
+
|
|
334
|
+
>>> class CSSTable(table.Table):
|
|
335
|
+
...
|
|
336
|
+
... css_classes = {'table': 'table',
|
|
337
|
+
... 'thead': 'thead',
|
|
338
|
+
... 'tbody': 'tbody',
|
|
339
|
+
... 'th': 'th',
|
|
340
|
+
... 'tr': 'tr',
|
|
341
|
+
... 'td': 'td'}
|
|
342
|
+
... css_class_sorted_on = None
|
|
343
|
+
...
|
|
344
|
+
... def setup_columns(self):
|
|
345
|
+
... first_column = TitleColumn(self.context, self.request, self)
|
|
346
|
+
... first_column.__name__ = 'title'
|
|
347
|
+
... first_column.__parent__ = self
|
|
348
|
+
... first_column.weight = 1
|
|
349
|
+
... first_column.css_classes = {'th':'thCol', 'td':'tdCol'}
|
|
350
|
+
... second_column = SimpleColumn(self.context, self.request, self)
|
|
351
|
+
... second_column.__name__ = 'simple'
|
|
352
|
+
... second_column.__parent__ = self
|
|
353
|
+
... second_column.weight = 2
|
|
354
|
+
... second_column.header = 'The second column'
|
|
355
|
+
... return [second_column, first_column]
|
|
356
|
+
|
|
357
|
+
Now let's see if we got the css class assigned which we defined in the table and
|
|
358
|
+
column. Note that the ``th`` and ``td`` got CSS declarations from the table and
|
|
359
|
+
from the column:
|
|
360
|
+
|
|
361
|
+
>>> css_table = CSSTable(container, request)
|
|
362
|
+
>>> css_table.update()
|
|
363
|
+
>>> print(css_table.render())
|
|
364
|
+
<table class="table">
|
|
365
|
+
<thead class="thead">
|
|
366
|
+
<tr class="tr">
|
|
367
|
+
<th class="thCol th">Title</th>
|
|
368
|
+
<th class="th">The second column</th>
|
|
369
|
+
</tr>
|
|
370
|
+
</thead>
|
|
371
|
+
<tbody class="tbody">
|
|
372
|
+
<tr class="tr">
|
|
373
|
+
<td class="tdCol td">Title: First</td>
|
|
374
|
+
<td class="td">First</td>
|
|
375
|
+
</tr>
|
|
376
|
+
<tr class="tr">
|
|
377
|
+
<td class="tdCol td">Title: Second</td>
|
|
378
|
+
<td class="td">Second</td>
|
|
379
|
+
</tr>
|
|
380
|
+
<tr class="tr">
|
|
381
|
+
<td class="tdCol td">Title: Third</td>
|
|
382
|
+
<td class="td">Third</td>
|
|
383
|
+
</tr>
|
|
384
|
+
</tbody>
|
|
385
|
+
</table>
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
Alternating table
|
|
389
|
+
-----------------
|
|
390
|
+
|
|
391
|
+
We offer built in support for alternating table rows based on even and odd CSS
|
|
392
|
+
classes. Let's define a table including other CSS classes. For even/odd support,
|
|
393
|
+
we only need to define the ``css_class_even`` and ``css_class_odd`` CSS classes:
|
|
394
|
+
|
|
395
|
+
>>> class AlternatingTable(table.Table):
|
|
396
|
+
...
|
|
397
|
+
... css_classes = {'table': 'table',
|
|
398
|
+
... 'thead': 'thead',
|
|
399
|
+
... 'tbody': 'tbody',
|
|
400
|
+
... 'th': 'th',
|
|
401
|
+
... 'tr': 'tr',
|
|
402
|
+
... 'td': 'td'}
|
|
403
|
+
...
|
|
404
|
+
... css_class_even = 'even'
|
|
405
|
+
... css_class_odd = 'odd'
|
|
406
|
+
... css_class_sorted_on = None
|
|
407
|
+
...
|
|
408
|
+
... def setup_columns(self):
|
|
409
|
+
... first_column = TitleColumn(self.context, self.request, self)
|
|
410
|
+
... first_column.__name__ = 'title'
|
|
411
|
+
... first_column.__parent__ = self
|
|
412
|
+
... first_column.weight = 1
|
|
413
|
+
... first_column.css_classes = {'th':'thCol', 'td':'tdCol'}
|
|
414
|
+
... second_column = SimpleColumn(self.context, self.request, self)
|
|
415
|
+
... second_column.__name__ = 'simple'
|
|
416
|
+
... second_column.__parent__ = self
|
|
417
|
+
... second_column.weight = 2
|
|
418
|
+
... second_column.header = 'The second column'
|
|
419
|
+
... return [second_column, first_column]
|
|
420
|
+
|
|
421
|
+
Now update and render the new table. As you can see the given ``tr`` class is
|
|
422
|
+
added to the even and odd classes:
|
|
423
|
+
|
|
424
|
+
>>> alternating_table = AlternatingTable(container, request)
|
|
425
|
+
>>> alternating_table.update()
|
|
426
|
+
>>> print(alternating_table.render())
|
|
427
|
+
<table class="table">
|
|
428
|
+
<thead class="thead">
|
|
429
|
+
<tr class="tr">
|
|
430
|
+
<th class="thCol th">Title</th>
|
|
431
|
+
<th class="th">The second column</th>
|
|
432
|
+
</tr>
|
|
433
|
+
</thead>
|
|
434
|
+
<tbody class="tbody">
|
|
435
|
+
<tr class="even tr">
|
|
436
|
+
<td class="tdCol td">Title: First</td>
|
|
437
|
+
<td class="td">First</td>
|
|
438
|
+
</tr>
|
|
439
|
+
<tr class="odd tr">
|
|
440
|
+
<td class="tdCol td">Title: Second</td>
|
|
441
|
+
<td class="td">Second</td>
|
|
442
|
+
</tr>
|
|
443
|
+
<tr class="even tr">
|
|
444
|
+
<td class="tdCol td">Title: Third</td>
|
|
445
|
+
<td class="td">Third</td>
|
|
446
|
+
</tr>
|
|
447
|
+
</tbody>
|
|
448
|
+
</table>
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
Class based Table setup
|
|
452
|
+
-----------------------
|
|
453
|
+
|
|
454
|
+
There is a more elegant way to define table rows at class level. We offer
|
|
455
|
+
a method which you can use if you need to define some columns called
|
|
456
|
+
``addColumn``. Before we define the table. let's define some cell renderer:
|
|
457
|
+
|
|
458
|
+
>>> def head_cell_renderer():
|
|
459
|
+
... return 'My items'
|
|
460
|
+
|
|
461
|
+
>>> def cell_renderer(item):
|
|
462
|
+
... return '%s item' % item.title
|
|
463
|
+
|
|
464
|
+
Now we can define our table and use the custom cell renderer:
|
|
465
|
+
|
|
466
|
+
>>> class AddColumnTable(table.Table):
|
|
467
|
+
...
|
|
468
|
+
... css_classes = {'table': 'table',
|
|
469
|
+
... 'thead': 'thead',
|
|
470
|
+
... 'tbody': 'tbody',
|
|
471
|
+
... 'th': 'th',
|
|
472
|
+
... 'tr': 'tr',
|
|
473
|
+
... 'td': 'td'}
|
|
474
|
+
...
|
|
475
|
+
... css_class_even = 'even'
|
|
476
|
+
... css_class_odd = 'odd'
|
|
477
|
+
... css_class_sorted_on = None
|
|
478
|
+
...
|
|
479
|
+
... def setup_columns(self):
|
|
480
|
+
... return [
|
|
481
|
+
... column.add_column(self, TitleColumn, 'title',
|
|
482
|
+
... cell_renderer=cell_renderer,
|
|
483
|
+
... head_cell_renderer=head_cell_renderer,
|
|
484
|
+
... weight=1, colspan=0),
|
|
485
|
+
... column.add_column(self, SimpleColumn, name='simple',
|
|
486
|
+
... weight=2, header='The second column',
|
|
487
|
+
... css_classes = {'th':'thCol', 'td':'tdCol'})
|
|
488
|
+
... ]
|
|
489
|
+
|
|
490
|
+
Add some more content::
|
|
491
|
+
|
|
492
|
+
>>> container[u'fourth'] = Content('Fourth', 4)
|
|
493
|
+
>>> container[u'zero'] = Content('Zero', 0)
|
|
494
|
+
|
|
495
|
+
>>> add_column_table = AddColumnTable(container, request)
|
|
496
|
+
>>> add_column_table.update()
|
|
497
|
+
>>> print(add_column_table.render())
|
|
498
|
+
<table class="table">
|
|
499
|
+
<thead class="thead">
|
|
500
|
+
<tr class="tr">
|
|
501
|
+
<th class="th">My items</th>
|
|
502
|
+
<th class="thCol th">The second column</th>
|
|
503
|
+
</tr>
|
|
504
|
+
</thead>
|
|
505
|
+
<tbody class="tbody">
|
|
506
|
+
<tr class="even tr">
|
|
507
|
+
<td class="td">First item</td>
|
|
508
|
+
<td class="tdCol td">First</td>
|
|
509
|
+
</tr>
|
|
510
|
+
<tr class="odd tr">
|
|
511
|
+
<td class="td">Fourth item</td>
|
|
512
|
+
<td class="tdCol td">Fourth</td>
|
|
513
|
+
</tr>
|
|
514
|
+
<tr class="even tr">
|
|
515
|
+
<td class="td">Second item</td>
|
|
516
|
+
<td class="tdCol td">Second</td>
|
|
517
|
+
</tr>
|
|
518
|
+
<tr class="odd tr">
|
|
519
|
+
<td class="td">Third item</td>
|
|
520
|
+
<td class="tdCol td">Third</td>
|
|
521
|
+
</tr>
|
|
522
|
+
<tr class="even tr">
|
|
523
|
+
<td class="td">Zero item</td>
|
|
524
|
+
<td class="tdCol td">Zero</td>
|
|
525
|
+
</tr>
|
|
526
|
+
</tbody>
|
|
527
|
+
</table>
|
|
528
|
+
|
|
529
|
+
As you can see the table columns provide all attributes we set in the addColumn
|
|
530
|
+
method:
|
|
531
|
+
|
|
532
|
+
>>> title_column = add_column_table.rows[0][0][1]
|
|
533
|
+
>>> title_column
|
|
534
|
+
<TitleColumn 'title'>
|
|
535
|
+
|
|
536
|
+
>>> title_column.__name__
|
|
537
|
+
'title'
|
|
538
|
+
|
|
539
|
+
>>> title_column.__parent__
|
|
540
|
+
<AddColumnTable None>
|
|
541
|
+
|
|
542
|
+
>>> title_column.colspan
|
|
543
|
+
0
|
|
544
|
+
|
|
545
|
+
>>> title_column.weight
|
|
546
|
+
1
|
|
547
|
+
|
|
548
|
+
>>> title_column.header
|
|
549
|
+
'Title'
|
|
550
|
+
|
|
551
|
+
>>> title_column.css_classes
|
|
552
|
+
{}
|
|
553
|
+
|
|
554
|
+
and the second column:
|
|
555
|
+
|
|
556
|
+
>>> simple_column = add_column_table.rows[0][1][1]
|
|
557
|
+
>>> simple_column
|
|
558
|
+
<SimpleColumn 'simple'>
|
|
559
|
+
|
|
560
|
+
>>> simple_column.__name__
|
|
561
|
+
'simple'
|
|
562
|
+
|
|
563
|
+
>>> simple_column.__parent__
|
|
564
|
+
<AddColumnTable None>
|
|
565
|
+
|
|
566
|
+
>>> simple_column.colspan
|
|
567
|
+
0
|
|
568
|
+
|
|
569
|
+
>>> simple_column.weight
|
|
570
|
+
2
|
|
571
|
+
|
|
572
|
+
>>> simple_column.header
|
|
573
|
+
'The second column'
|
|
574
|
+
|
|
575
|
+
>>> sorted(simple_column.css_classes.items())
|
|
576
|
+
[('td', 'tdCol'), ('th', 'thCol')]
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
Headers
|
|
580
|
+
-------
|
|
581
|
+
|
|
582
|
+
We can change the rendering of the header of, e.g, the Title column by
|
|
583
|
+
registering a IHeaderColumn adapter. This may be useful for adding links to
|
|
584
|
+
column headers for an existing table implementation.
|
|
585
|
+
|
|
586
|
+
We'll use a fresh almost empty container.:
|
|
587
|
+
|
|
588
|
+
>>> container = Container()
|
|
589
|
+
>>> root['container-1'] = container
|
|
590
|
+
>>> container['first'] = Content('First', 1)
|
|
591
|
+
>>> container['second'] = Content('Second', 2)
|
|
592
|
+
>>> container['third'] = Content('Third', 3)
|
|
593
|
+
|
|
594
|
+
>>> class myTableClass(table.Table):
|
|
595
|
+
... css_class_sorted_on = None
|
|
596
|
+
|
|
597
|
+
>>> my_table = myTableClass(container, request)
|
|
598
|
+
|
|
599
|
+
>>> class TitleColumn(column.Column):
|
|
600
|
+
...
|
|
601
|
+
... header = u'Title'
|
|
602
|
+
... weight = -2
|
|
603
|
+
...
|
|
604
|
+
... def render_cell(self, item):
|
|
605
|
+
... return item.title
|
|
606
|
+
|
|
607
|
+
Now we can register a column adapter directly to our table class:
|
|
608
|
+
|
|
609
|
+
>>> config.registry.registerAdapter(TitleColumn,
|
|
610
|
+
... (None, None, myTableClass), provided=interfaces.IColumn,
|
|
611
|
+
... name='title_column')
|
|
612
|
+
|
|
613
|
+
And add a registration for a column header - we'll use here the provided generic
|
|
614
|
+
sorting header implementation:
|
|
615
|
+
|
|
616
|
+
>>> from pyams_table.header import SortingColumnHeader
|
|
617
|
+
>>> config.registry.registerAdapter(SortingColumnHeader,
|
|
618
|
+
... (None, None, interfaces.ITable, interfaces.IColumn),
|
|
619
|
+
... provided=interfaces.IColumnHeader)
|
|
620
|
+
|
|
621
|
+
Now we can render the table and we shall see a link in the header. Note that it
|
|
622
|
+
is set to switch to descending as the table initially will display the first
|
|
623
|
+
column as ascending:
|
|
624
|
+
|
|
625
|
+
>>> my_table.update()
|
|
626
|
+
>>> print(my_table.render())
|
|
627
|
+
<table>
|
|
628
|
+
<thead>
|
|
629
|
+
<tr>
|
|
630
|
+
<th><a
|
|
631
|
+
href="?table-sort-on=table-title_column-0&table-sort-order=descending"
|
|
632
|
+
title="Sort">Title</a></th>
|
|
633
|
+
...
|
|
634
|
+
</table>
|
|
635
|
+
|
|
636
|
+
If the table is initially set to descending, the link should allow to switch to
|
|
637
|
+
ascending again:
|
|
638
|
+
|
|
639
|
+
>>> my_table.sort_order = 'descending'
|
|
640
|
+
>>> print(my_table.render())
|
|
641
|
+
<table>
|
|
642
|
+
<thead>
|
|
643
|
+
<tr>
|
|
644
|
+
<th><a
|
|
645
|
+
href="?table-sort-on=table-title_column-0&table-sort-order=ascending"
|
|
646
|
+
title="Sort">Title</a></th>
|
|
647
|
+
...
|
|
648
|
+
</table>
|
|
649
|
+
|
|
650
|
+
If the table is ascending but the request was descending,
|
|
651
|
+
the link should allow to switch again to ascending:
|
|
652
|
+
|
|
653
|
+
>>> descending_request = DummyRequest(params={'table-sort-on': 'table-title_column-0',
|
|
654
|
+
... 'table-sort-order':'descending'})
|
|
655
|
+
>>> my_table = myTableClass(container, descending_request)
|
|
656
|
+
>>> my_table.sortOrder = 'ascending'
|
|
657
|
+
>>> my_table.update()
|
|
658
|
+
>>> print(my_table.render())
|
|
659
|
+
<table>
|
|
660
|
+
<thead>
|
|
661
|
+
<tr>
|
|
662
|
+
<th><a
|
|
663
|
+
href="?table-sort-on=table-title_column-0&table-sort-order=ascending"
|
|
664
|
+
title="Sort">Title</a></th>
|
|
665
|
+
...
|
|
666
|
+
</table>
|
|
667
|
+
|
|
668
|
+
>>> tearDown()
|