This example builds a DataTable based on the columns information taken from the server. Instead of building a statically defined DataTable it will build it based on what the server says.
It has been based on the server-side pagination and server-side sorting examples. They have both been changed in that they do not use the DataSource to communicate with the server but use a plain Connection Manager request to fetch the information and then pass it as a local source to the DataSource. This allows full access to all the information received from the server before it gets sent to the DataTable. Reported bugs on both examples have been fixed as well.
The source files for this sample are available for download in a zip file. The PHP server script requires an active PHP interpreter. The YUI compoments are fetched from the YUI site.
New for 2.5.1The examples have been completely redone for this release. Server-side paging has been fully revamped. All the extra code required to do server-side paging around the DataTable is now completely gone since most is handled directly from within the DataTable. Now it only requires a few settings, some function overrides and little more.
The most noticeable change for the user is the page links, which are now provided by the DataTable itself, with the same features as for client-side paging and can be customized just as well.
Server-side sorting, though, is not really well integrated into the DataTable yet so that part is not so clean.
The DataTable is basically meant to represent data structured as in a regular database table. As such, it has a definition for each of the columns which applies to all rows in the table. Unless the designer provides customized code, every cell in the same column will behave the same.
This example uses an extra invisible column to provide data type definitions for each individual cell so that instead of a single definition applicable to all the cells in a column, each cell has its own definition. For the purpose of the example, this definition is only a string indicating whether the cell contains a number, text or date, nevertheless, in a real case there is no reason why it cannot be a complex object describing several attributes.
All columns have a custom formatter and editor function. These do not actually do any formatting or editing but they check the corresponding data type information stored in the extra column paired to the one being actually formatted or edited and then calls the standard formatters and editors provided for along the DataTable component
The example is fully contained in the HTML file, its only external resources are the YUI component files which are downloaded from the YUI download site.
New for 2.5.1No changes where required for this version
This example draws a very basic DataTable and on clicking on any cell, a YUI Panel will pop-up containing a YUI TabView with four tabs. Each tab has a child DataTable with different contents, all related to the clicked record. The contents in the cells of these child tables is generated dynamically and it reflects the base table row number and tab numbers active plus the row and column position of the cell.
In order to make the example totally self-contained, the data on all the tables is created locally. In an actual application the data would come from data servers. Besides changing the columns and field definitions to match the actual data, to make this example work in client-server environments the single argument to the constructors of all the new DataSources would need to be changed to the URL of the servers providing the data.
New for 2.5.1This example uses method
sendRequest
of the DataSource to retrieve new data for the child tables. It has been modified to accept the arguments in the new format.
The new version has provided backward-compatibility so it would have worked just the same the way it was.
This example allows you to do an invoice by dragging items out of a DataTable with a list of items into another DataTable. It has in-line cell editing with an editor with a regular expression validator. The invoice uses the TFOOT element of the table to do the totals
New for 2.5.1There are only two changes for this version.
Method getTableEl does not exist anymore so it has been replaced by a way around.
Also, the inline style definition for className number had to be replaced due to the new
'liner' element in between the HTML cell and the actual content.
Since now the headers are a separate table from the body of the DataTable the column widths are now calculated by the DataTable on each refresh. The footer is not included in this calculation since the DataTable ignores it exists. When first loaded, the right-hand table is empty and the footer with the totals does not match the headers (the titles in grey to the left don't end where they should). As soon as an item is dropped into the invoice, the borders align fine.
Fixed: Also, if you enter a large quantity on any of the items so that the totals
grow wide, the headers and the table won't match any longer.
Finally, there must be some padding or margin in the 'liner' element so that
the totals are not aligned with the item prices. This I should be able to fix ASAP.
The overflowing totals with large quantities was fixed by calling method _syncColWidths
after the values are recalculated. Though the DataTable does not know about the tfoot element,
the tbody of the table does adjust its width to it so _syncColWidths is able to adjust
the headers to it. This was a tip by Jenny Han Donnelly, thanks.
I often discourage the use of private methods and properties (those signaled by a leading underscore)
since they are unsupported. In this case, tfoot elements are also unsupported so this whole example
may break at any moment.
Also, the alignment issue was solved by adding the same padding to the extra cells
as the liner element has in the body of the table.
This example uses the new Selector component to hide and show columns in a DataTable with only two lines of code.
It uses the same server script as the Server driven DataTable example above.
New for 2.5.1Showing and hiding columns is now a feature of the DataTable itself so there is no need to do any tricks with CSS styls and the Selector component.
This example show an assortment of HTML controls: radio buttons, checkboxes, dropdowns, regular and image buttons and an in-line textbox and how to handle them.
New for 2.5.1Not much new, simply using the getTdLinerEl method to reach the cell value container.
Here, the DataTable is extended with a function deleteRowsBy analogous to
function getChildrenBy in YAHOO.util.Dom, where you provide a function
that will return a Boolean indicating whether the record is to be deleted or not.
This function receives an object with all the values of each record to decide
The example builds a couple of random trees. It will show a DataTable at the end of each branch.
In the first tree, several branches can be open at the same time so it builds a DataTable for each.
In the second tree, only one branch will be open at a time so it uses just one DataTable for all of them and just moves it around. It also uses a single DataSource with caching enabled so that if a node is visited again the data can be read from the cache.
This is a working example of how you can send JSON data via a POST request with Connection Manager's asyncRequest method, and how to read it on the PHP side.
After a message in the YUI discussion forum and after writting the article about how to do widgets, I did my own Progress Bar component.
After using the DataTable for a while and answering questions from fellow users about it I realized I could help a little bit by putting together all I had learned in the process so I wrote a big article which Eric Miraglia very kindly refused, and right he was on doing so! After clearing up my ideas I realized there were about four separate articles that could be written. The first two were then accepted for publication by Eric at the YUI blog site after some editing which he very kindly handled.
Some of the material in the article I originally prepared for publication at the YUI blog had no JavaScript at all, it was pure PHP so here it is with some extra PHP thrown in.
This article contains the code that would be the back-end to the code in the other two articles or, for that matter, in any AJAX application or, perhaps, it should be AJAJ since it uses JSON and not XML.
It also contains the code for my BuildSql function which is a sort of sprintf oriented towards SQL so that you write your SQL statement with placeholders and then the function takes care of inserting the rest of the arguments into the template with the proper format, quoting and escaping.
YUI's DataTable component has many options so that it can be used in all sorts of applications and environments. Any single user, though, uses just a few of them. It is a good idea to encapsulate those few options in a single place so that even if at some point you decide to make a change, it will be propagated to all places at once.
Whatever options the server provides will probably determine how we communicate with it. The nature of our application will define the types of data we handle. The looks and behavior of our site will fix some more options. Finally, we may desperately need some patch and can't wait for the next release. These are options that we may cast into our own DataTable, inherited from YUI DataTable
At one point, the YUI discussion forum got several messages about people developing their own widgets. The YUI blog also reported a few. All of them failed to conform to the current YUI standards, with good reason, those standards are not available anywhere.
There is a good reason for that, the YUI library has been continuously growing and perfecting itself, but nobody had time to get all that experience gained by the YUI team anywhere. Moreover, the YUI library itself is not a good means of learning those standards since many of the components follow different conventions. Adapting those components to the ever improving standards would have meant making them incompatible with earlier versions. This means that each component is frozen at its historical moment, and if you don't know the history of the library, you don't know why each one is as it is.
So, in the end, I went to the newest components and started learning from them and compiled a list of guidelines which I later put together into this article. Eric Miraglia was very kind to check that I wasn't saying anything stupid (both in my use of English and in describing the library) and also asked me to explore some features I had overlooked.
I've got to write a couple of articles for O'Reilly's Inside RIA web site.
Nothing new on them. I hope they finally get their act together, it is a hassle to work with them and I don't intend to publish anything else with them, but there they are:
A brief list of places that have good examples on the use of DataTables or similar components.
A utility to figure out what needs to be included when you are loading several components at once.
It uses the YUI Loader to find out what you need and in which order