dataschema-table.mustache revision 11d2780adc5c17fefe1ea2d70c7e75ca6647c49c
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence<div class="intro">
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews <p>DataSchema.XML can be used to retrieve data held in HTML TABLE elements.</p>
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence<div class="example yui3-skin-sam">
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence {{>dataschema-table-source}}
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews<p>DataSource.XML's `apply()` method supports passing in DOM nodes or document fragments. Use XPath strings to define data locations. In this example, we are retrieving data held in the rows of a TABLE element.</p>
dafcb997e390efa4423883dafd100c975c4095d6Mark AndrewsYUI().use("dataschema-xml", function(Y) {
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews var tableEl = Y.Node.getDOMNode(Y.one("#simple")),
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews // Each record is held in a TR
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence resultListLocator: "tr",
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence // Note that the XPath indexes are 1-based!
50105afc551903541608b11851d73278b23579a3Mark Andrews resultFields: [
821644d49b73b49f2abc5463bc53a3132f612478Mark Andrews {key:"beverage", locator:"td[1]"},
821644d49b73b49f2abc5463bc53a3132f612478Mark Andrews {key:"price", locator:"td[2]"}
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington data_out = Y.DataSchema.XML.apply(schema, tableEl);
4f37905cc38162128a507e619e38ae535720686bAndreas Gustafsson alert(data_out);
5a77e9620a0b2f7417469c98be374de49d0eccc6Andreas Gustafsson<p>If the table has a THEAD element and/or multiple TBODY elements, special considerations must be taken to apply the schema to the appropriate collection of TR elements. In the following complex example we leverage the power of the Node API to</p>
50105afc551903541608b11851d73278b23579a3Mark Andrews <li>Access the contents of the THEAD to generate our schema dynamically;</li>
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington <li>and access only the TR elements contained in TBODY elements for data values, ignoring those TR elements in the THEAD.</li>
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian WellingtonYUI().use("dataschema-xml", function(Y) {
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // This function generates a schema based on contents of a TABLE element
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington var getSchemaFromTableNode = function(tableNode) {
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington var fields = [],
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Each record is held in a TR
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington schema = {resultListLocator:"tr"},
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Each field name is held in a TH
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Generate field definitions based on TH
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington thList.each(function(thNode, i){
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Note that the XPath indexes are 1-based!
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"});
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington return schema;
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington var tableNode = Y.one("#complex"),
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Generate schema dynamically
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington schema = getSchemaFromTableNode(tableNode),
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Create a temporary TBODY container to hold all data TRs
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington tbodyContainer = document.createDocumentFragment().appendChild(tbody);
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington // Grab each TR in a TBODY but don't include TRs from the THEAD
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington Y.all("#complex tbody tr").each(function(trNode, i){
var data_out = Y.DataSchema.XML.apply(schema, tbodyContainer);