Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- YUI().use('dataschema-xml', 'datatable-sort', function(Y) {
- // This function generates a schema based on contents of a TABLE element
- var getSchemaFromTableNode = function(tableNode) {
- var fields = [],
- // Each record is held in a TR
- schema = {resultListLocator:"tr"},
- // Each field name is held in a TH
- thList = tableNode.all("th");
- // Generate field definitions based on TH
- thList.each(function(thNode, i){
- // Note that the XPath indexes are 1-based!
- // String().test();
- fields.push(
- {
- key:thNode.get("text"),
- sortable: (/^true$/i).test(thNode.getAttribute("data-sortable")),
- locator:"td["+(i+1)+"]"
- }
- );
- }),
- schema.resultFields = fields;
- return schema;
- };
- var tableNode = Y.one("#accounts"),
- // Generate schema dynamically
- schema = getSchemaFromTableNode(tableNode),
- // Create a temporary TBODY container to hold all data TRs
- tbody = document.createElement("tbody"),
- tbodyContainer = document.createDocumentFragment().appendChild(tbody);
- // Grab each TR in a TBODY but don't include TRs from the THEAD
- Y.all("#accounts tbody tr").each(function(trNode, i){
- // Cloning the TR keeps it in the document (optional)
- // Append each TR to the container
- tbodyContainer.appendChild(Y.Node.getDOMNode(trNode).cloneNode(true));
- });
- Y.one("#accounts table").hide();
- table = new Y.DataTable.Base({
- columnset: getSchemaFromTableNode(tableNode).resultFields,
- recordset: Y.DataSchema.XML.apply(schema, tbodyContainer).results
- }).plug(Y.Plugin.DataTableSort,
- lastSortedBy = {
- field: Y.one('#accounts th[data-initial-order]').get("text") ,
- dir: Y.one('#accounts th[data-initial-order]').getAttribute('data-initial-order')
- }
- ).render("#accounts");
- });
Add Comment
Please, Sign In to add comment