dataschema-table.mustache revision 819e90d415ed17d59af3a247b2ad9d6feb0c21b5
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright<div class="intro">
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright <p>DataSchema.XML can be used to retrieve data held in HTML TABLE elements.</p>
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright<div class="example yui3-skin-sam">
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright {{>dataschema-table-source}}
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright<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>
6aaa468f50af23f08685835d1984767b81a4446aDerek GathrightYUI().use("dataschema-xml", function(Y) {
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek Gathright var tableEl = Y.Node.getDOMNode(Y.one("#simple")),
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Each record is held in a TR
9d2b984ddc990a339c99c2ee8ab13348846a0bcbDerek Gathright resultListLocator: "tr",
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek Gathright // Note that the XPath indexes are 1-based!
64a0687a6370761e2fa57761cde7398a870ecc60Derek Gathright resultFields: [
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright {key:"beverage", locator:"td[1]"},
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright {key:"price", locator:"td[2]"}
db1bbf94979caed0f2f215224d52100ba61a42aaDerek Gathright data_out = Y.DataSchema.XML.apply(schema, tableEl));
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright alert(data_out);
b28e90463180540ce576a7941dcade8d95beee76Derek Gathright<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>
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright <li>Access the contents of the THEAD to generate our schema dynamically;</li>
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright <li>and access only the TR elements contained in TBODY elements for data values, ignoring those TR elements in the THEAD.</li>
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek GathrightYUI().use("dataschema-xml", function(Y) {
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek Gathright // This function generates a schema based on contents of a TABLE element
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek Gathright var getSchemaFromTableNode = function(tableNode) {
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek Gathright var fields = [],
c9871cbdfcd26b5d20932513df38a0f231ddc33fDerek Gathright // Each record is held in a TR
db92b84e128cab8bd1aecfa3c38718a339eb501fDerek Gathright schema = {resultListLocator:"tr"},
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Each field name is held in a TH
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright thList = tableNode.all("th");
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Generate field definitions based on TH
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright thList.each(function(thNode, i){
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Note that the XPath indexes are 1-based!
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"});
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright return schema;
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright var tableNode = Y.one("#complex"),
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Generate schema dynamically
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright schema = getSchemaFromTableNode(tableNode),
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Create a temporary TBODY container to hold all data TRs
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright tbodyContainer = document.createDocumentFragment().appendChild(tbody);
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Grab each TR in a TBODY but don't include TRs from the THEAD
b28e90463180540ce576a7941dcade8d95beee76Derek Gathright Y.all("#complex tbody tr").each(function(trNode, i){
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Cloning the TR keeps it in the document (optional)
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright // Append each TR to the container
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright tbodyContainer.appendChild(Y.Node.getDOMNode(trNode).cloneNode(true));
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright var data_out = Y.DataSchema.XML.apply(schema, tbodyContainer));
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright alert(data_out);