1 | waeup.sirp.widgets |
---|
2 | ****************** |
---|
3 | |
---|
4 | Widgets for the WAeUP SRP. |
---|
5 | |
---|
6 | |
---|
7 | .. :doctest: |
---|
8 | .. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer |
---|
9 | |
---|
10 | Tables |
---|
11 | ====== |
---|
12 | |
---|
13 | >>> from waeup.sirp.widgets.table import Col, Table |
---|
14 | >>> id = Col( |
---|
15 | ... header='Id', width=100, sortable=True, type='int', |
---|
16 | ... data = (1, 2, 3)) |
---|
17 | >>> mycol1 = Col( |
---|
18 | ... header='Column 1', width=100, sortable=True, |
---|
19 | ... data = ('bli', 'bla', 'blub')) |
---|
20 | |
---|
21 | >>> mycol2 = Col( |
---|
22 | ... header='Column 2', width=100, sortable=True, |
---|
23 | ... data = ('schwi', 'schwa', 'schwafel')) |
---|
24 | |
---|
25 | >>> table = Table('My Table', cols=(id, mycol1, mycol2)) |
---|
26 | >>> print table.renderHTML() |
---|
27 | <div id="mytable-tablecontainer"><table id="mytable-table"><thead><tr><th>Id</th><th>Column 1</th><th>Column 2</th></tr></thead> |
---|
28 | ...<tbody><tr> |
---|
29 | ...<td>1</td><td>bli</td><td>schwi</td> |
---|
30 | ...</tr><tr> |
---|
31 | ...<td>2</td><td>bla</td><td>schwa</td> |
---|
32 | ...</tr><tr> |
---|
33 | ...<td>3</td><td>blub</td><td>schwafel</td> |
---|
34 | ...</tr></tbody></table></div> |
---|
35 | |
---|
36 | >>> print table.internal_title |
---|
37 | mytable |
---|
38 | |
---|
39 | YUITable |
---|
40 | -------- |
---|
41 | |
---|
42 | A YUI table extends a regular table by adding some JavaScript |
---|
43 | code-generation facilities suitable for rendering YUI based |
---|
44 | datatables. Futhermore they provide a `need` method, which includes |
---|
45 | all needed YUI scripts and stylesheets. |
---|
46 | |
---|
47 | |
---|
48 | >>> from waeup.sirp.widgets.table import YUITable |
---|
49 | >>> table = YUITable('My YUI Table', cols=(id, mycol1, mycol2)) |
---|
50 | >>> print table.getJSTableCode() |
---|
51 | <BLANKLINE> |
---|
52 | YAHOO.util.Event.addListener(window, "load", function() {... |
---|
53 | ... |
---|
54 | draggableColumns:true }); |
---|
55 | }); |
---|
56 | |
---|
57 | Naturally, YUITable objects are suited for use by views. |
---|
58 | |
---|
59 | |
---|
60 | YUIStaticTableView |
---|
61 | ================== |
---|
62 | |
---|
63 | This is a view named ``yuistatictables.js`` which from a users' |
---|
64 | perspective can be used like a static file. It is, however, not |
---|
65 | static, but returns the JavaScript code suitable for a given table. |
---|
66 | |
---|
67 | We define some content object that contains a table for which we want |
---|
68 | the JavaScript to be generated: |
---|
69 | |
---|
70 | >>> class SomeContent(object): |
---|
71 | ... mytable = table |
---|
72 | |
---|
73 | >>> obj = SomeContent() |
---|
74 | |
---|
75 | Now we need an adapter, that knows how to get a table from this kind |
---|
76 | of object. The YUIStaticTableView looks up such an adapter for a given |
---|
77 | context to find a table to manage. The adapter has to implement |
---|
78 | `ITableProvider` from `waeup.sirp.widgets.interfaces` and should provide a |
---|
79 | `getTables` method. |
---|
80 | |
---|
81 | We define and register such an adapter using grok: |
---|
82 | |
---|
83 | >>> import grok |
---|
84 | >>> from waeup.sirp.widgets.interfaces import ITableProvider |
---|
85 | >>> class TableAdapter(grok.Adapter): |
---|
86 | ... grok.context(SomeContent) |
---|
87 | ... grok.provides(ITableProvider) |
---|
88 | ... |
---|
89 | ... def getTables(self): |
---|
90 | ... # We know that our context stores a table in `mytable` |
---|
91 | ... return (self.context.mytable,) |
---|
92 | >>> grok.testing.grok_component('TableAdapter', TableAdapter) |
---|
93 | True |
---|
94 | |
---|
95 | Now let's get a view on the object defined above. We want the view |
---|
96 | named ``yuistatictables.js``: |
---|
97 | |
---|
98 | >>> from zope.publisher.browser import TestRequest |
---|
99 | >>> from zope.component import getMultiAdapter |
---|
100 | >>> from zope.interface import Interface |
---|
101 | >>> view = getMultiAdapter((obj, TestRequest()), Interface, |
---|
102 | ... name='yuistatictables.js') |
---|
103 | >>> view |
---|
104 | <waeup.sirp.widgets.table.YUIStaticTableView object at 0x...> |
---|
105 | |
---|
106 | When we call the view we get pure JavaScript: |
---|
107 | |
---|
108 | >>> print view() |
---|
109 | <BLANKLINE> |
---|
110 | YAHOO.util.Event.addListener(window, "load", function() { |
---|
111 | ... |
---|
112 | draggableColumns:true }); |
---|
113 | }); |
---|
114 | |
---|
115 | Table views for objects that have no table |
---|
116 | ------------------------------------------ |
---|
117 | |
---|
118 | What happens, when no `ITableProvider` can be found for an object? The |
---|
119 | view will return an empty string. |
---|
120 | |
---|
121 | Let's try this with a simple object for which no adapter is |
---|
122 | registered. We can get the view: |
---|
123 | |
---|
124 | >>> obj = object() |
---|
125 | >>> view = getMultiAdapter((obj, TestRequest()), Interface, |
---|
126 | ... name='yuistatictables.js') |
---|
127 | >>> view |
---|
128 | <waeup.sirp.widgets.table.YUIStaticTableView object at 0x...> |
---|
129 | |
---|
130 | But if we call this adapter, we will get an empty result, because |
---|
131 | there is no ITableProvider adapter registered for the 'viewed' object: |
---|
132 | |
---|
133 | >>> view() |
---|
134 | '' |
---|