[4920] | 1 | waeup.sirp.widgets |
---|
| 2 | ****************** |
---|
[4405] | 3 | |
---|
| 4 | Widgets for the WAeUP SRP. |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | :doctest: |
---|
| 8 | |
---|
| 9 | Tables |
---|
| 10 | ====== |
---|
| 11 | |
---|
[4920] | 12 | >>> from waeup.sirp.widgets.table import Col, Table |
---|
[4405] | 13 | >>> id = Col( |
---|
| 14 | ... header='Id', width=100, sortable=True, type='int', |
---|
| 15 | ... data = (1, 2, 3)) |
---|
| 16 | >>> mycol1 = Col( |
---|
| 17 | ... header='Column 1', width=100, sortable=True, |
---|
| 18 | ... data = ('bli', 'bla', 'blub')) |
---|
| 19 | |
---|
| 20 | >>> mycol2 = Col( |
---|
| 21 | ... header='Column 2', width=100, sortable=True, |
---|
| 22 | ... data = ('schwi', 'schwa', 'schwafel')) |
---|
| 23 | |
---|
| 24 | >>> table = Table('My Table', cols=(id, mycol1, mycol2)) |
---|
| 25 | >>> print table.renderHTML() |
---|
| 26 | <div id="mytable-tablecontainer"><table id="mytable-table"><thead><tr><th>Id</th><th>Column 1</th><th>Column 2</th></tr></thead> |
---|
| 27 | ...<tbody><tr> |
---|
| 28 | ...<td>1</td><td>bli</td><td>schwi</td> |
---|
| 29 | ...</tr><tr> |
---|
| 30 | ...<td>2</td><td>bla</td><td>schwa</td> |
---|
| 31 | ...</tr><tr> |
---|
| 32 | ...<td>3</td><td>blub</td><td>schwafel</td> |
---|
| 33 | ...</tr></tbody></table></div> |
---|
| 34 | |
---|
| 35 | >>> print table.internal_title |
---|
| 36 | mytable |
---|
| 37 | |
---|
[4420] | 38 | YUITable |
---|
| 39 | -------- |
---|
| 40 | |
---|
| 41 | A YUI table extends a regular table by adding some JavaScript |
---|
| 42 | code-generation facilities suitable for rendering YUI based |
---|
| 43 | datatables. Futhermore they provide a `need` method, which includes |
---|
| 44 | all needed YUI scripts and stylesheets. |
---|
| 45 | |
---|
| 46 | |
---|
[4920] | 47 | >>> from waeup.sirp.widgets.table import YUITable |
---|
[4420] | 48 | >>> table = YUITable('My YUI Table', cols=(id, mycol1, mycol2)) |
---|
| 49 | >>> print table.getJSTableCode() |
---|
| 50 | <BLANKLINE> |
---|
| 51 | YAHOO.util.Event.addListener(window, "load", function() {... |
---|
| 52 | ... |
---|
| 53 | draggableColumns:true }); |
---|
| 54 | }); |
---|
| 55 | |
---|
| 56 | Naturally, YUITable objects are suited for use by views. |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | YUIStaticTableView |
---|
| 60 | ================== |
---|
| 61 | |
---|
| 62 | This is a view named ``yuistatictables.js`` which from a users' |
---|
| 63 | perspective can be used like a static file. It is, however, not |
---|
| 64 | static, but returns the JavaScript code suitable for a given table. |
---|
| 65 | |
---|
| 66 | We define some content object that contains a table for which we want |
---|
| 67 | the JavaScript to be generated: |
---|
| 68 | |
---|
| 69 | >>> class SomeContent(object): |
---|
| 70 | ... mytable = table |
---|
| 71 | |
---|
| 72 | >>> obj = SomeContent() |
---|
| 73 | |
---|
| 74 | Now we need an adapter, that knows how to get a table from this kind |
---|
| 75 | of object. The YUIStaticTableView looks up such an adapter for a given |
---|
| 76 | context to find a table to manage. The adapter has to implement |
---|
[4920] | 77 | `ITableProvider` from `waeup.sirp.widgets.interfaces` and should provide a |
---|
[4469] | 78 | `getTables` method. |
---|
[4420] | 79 | |
---|
| 80 | We define and register such an adapter using grok: |
---|
| 81 | |
---|
| 82 | >>> import grok |
---|
[4942] | 83 | >>> grok.testing.grok('waeup.sirp') |
---|
[4920] | 84 | >>> from waeup.sirp.widgets.interfaces import ITableProvider |
---|
[4420] | 85 | >>> class TableAdapter(grok.Adapter): |
---|
| 86 | ... grok.context(SomeContent) |
---|
| 87 | ... grok.provides(ITableProvider) |
---|
| 88 | ... |
---|
[4469] | 89 | ... def getTables(self): |
---|
[4420] | 90 | ... # We know that our context stores a table in `mytable` |
---|
[4469] | 91 | ... return (self.context.mytable,) |
---|
[4420] | 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 |
---|
[4920] | 104 | <waeup.sirp.widgets.table.YUIStaticTableView object at 0x...> |
---|
[4420] | 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 |
---|
[4920] | 128 | <waeup.sirp.widgets.table.YUIStaticTableView object at 0x...> |
---|
[4420] | 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 | '' |
---|