waeup.sirp.widgets ****************** Widgets for the WAeUP SRP. .. :doctest: .. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer Tables ====== >>> from waeup.sirp.widgets.table import Col, Table >>> id = Col( ... header='Id', width=100, sortable=True, type='int', ... data = (1, 2, 3)) >>> mycol1 = Col( ... header='Column 1', width=100, sortable=True, ... data = ('bli', 'bla', 'blub')) >>> mycol2 = Col( ... header='Column 2', width=100, sortable=True, ... data = ('schwi', 'schwa', 'schwafel')) >>> table = Table('My Table', cols=(id, mycol1, mycol2)) >>> print table.renderHTML()
... ... ... ... ... ... ...
IdColumn 1Column 2
1blischwi
2blaschwa
3blubschwafel
>>> print table.internal_title mytable YUITable -------- A YUI table extends a regular table by adding some JavaScript code-generation facilities suitable for rendering YUI based datatables. Futhermore they provide a `need` method, which includes all needed YUI scripts and stylesheets. >>> from waeup.sirp.widgets.table import YUITable >>> table = YUITable('My YUI Table', cols=(id, mycol1, mycol2)) >>> print table.getJSTableCode() YAHOO.util.Event.addListener(window, "load", function() {... ... draggableColumns:true }); }); Naturally, YUITable objects are suited for use by views. YUIStaticTableView ================== This is a view named ``yuistatictables.js`` which from a users' perspective can be used like a static file. It is, however, not static, but returns the JavaScript code suitable for a given table. We define some content object that contains a table for which we want the JavaScript to be generated: >>> class SomeContent(object): ... mytable = table >>> obj = SomeContent() Now we need an adapter, that knows how to get a table from this kind of object. The YUIStaticTableView looks up such an adapter for a given context to find a table to manage. The adapter has to implement `ITableProvider` from `waeup.sirp.widgets.interfaces` and should provide a `getTables` method. We define and register such an adapter using grok: >>> import grok >>> from waeup.sirp.widgets.interfaces import ITableProvider >>> class TableAdapter(grok.Adapter): ... grok.context(SomeContent) ... grok.provides(ITableProvider) ... ... def getTables(self): ... # We know that our context stores a table in `mytable` ... return (self.context.mytable,) >>> grok.testing.grok_component('TableAdapter', TableAdapter) True Now let's get a view on the object defined above. We want the view named ``yuistatictables.js``: >>> from zope.publisher.browser import TestRequest >>> from zope.component import getMultiAdapter >>> from zope.interface import Interface >>> view = getMultiAdapter((obj, TestRequest()), Interface, ... name='yuistatictables.js') >>> view When we call the view we get pure JavaScript: >>> print view() YAHOO.util.Event.addListener(window, "load", function() { ... draggableColumns:true }); }); Table views for objects that have no table ------------------------------------------ What happens, when no `ITableProvider` can be found for an object? The view will return an empty string. Let's try this with a simple object for which no adapter is registered. We can get the view: >>> obj = object() >>> view = getMultiAdapter((obj, TestRequest()), Interface, ... name='yuistatictables.js') >>> view But if we call this adapter, we will get an empty result, because there is no ITableProvider adapter registered for the 'viewed' object: >>> view() ''