Changeset 12902

Show
Ignore:
Timestamp:
03/05/08 03:30:53 (5 months ago)
Author:
alex
Message:

add a new, greatly simplified, style of table construction from HTML tables and make working with dojo.data stores stupidly easy. Event handlers aren't 100% and lots of stuff isn't fully configurable from the new table markup syntax just yet, but for getting someone started with the grid, this is the new hotness.

Refs #5503

Location:
dojox/trunk/grid
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • dojox/trunk/grid/Grid.js

    r12878 r12902  
    255255}); 
    256256 
     257dojox.Grid.markupFactory = function(props, node, ctor){ 
     258        // handle setting up a data model for a store if one 
     259        // isn't provided. There are some caveats: 
     260        //              * we only really handle dojo.data sources well. They're the future 
     261        //                so it's no big deal, but it's something to be aware of. 
     262        //              * I'm pretty sure that colgroup introspection is missing some of 
     263        //                the available settable properties.  
     264        //              * No handling of cell formatting and content getting is done 
     265        var d = dojo; 
     266        var widthFromAttr = function(n){ 
     267                var w = d.attr(n, "width")||"auto"; 
     268                if((w != "auto")&&(w.substr(-2) != "em")){ 
     269                        w = parseInt(w)+"px"; 
     270                } 
     271                return w; 
     272        } 
     273        if(!props.model && d.hasAttr(node, "store")){ 
     274                // if a model isn't specified and we point to a store, assume 
     275                // we're also folding the definition for a model up into the 
     276                // inline ctor for the Grid. This will then take properties 
     277                // like "query", "rowsPerPage", and "clientSort" from the grid 
     278                // definition. 
     279                var mNode = node.cloneNode(false); 
     280                d.attr(mNode, { 
     281                        "jsId": null, 
     282                        "dojoType": d.attr(node, "dataModelClass") || "dojox.grid.data.DojoData" 
     283                }); 
     284                props.model = d.parser.instantiate([mNode])[0]; 
     285        } 
     286        // if(!props.model){ console.debug("no model!"); } 
     287        // if a structure isn't referenced, do we have enough 
     288        // data to try to build one automatically? 
     289        if(     !props.structure &&  
     290                node.nodeName.toLowerCase() == "table"){ 
     291 
     292                // try to discover a structure 
     293                props.structure = d.query("> colgroup", node).map(function(cg){ 
     294                        var sv = d.attr(cg, "span"); 
     295                        var v = {  
     296                                noscroll: (d.attr(cg, "noscroll") == "true") ? true : false, 
     297                                __span: (!!sv ? parseInt(sv) : 1), 
     298                                cells: [] 
     299                        }; 
     300                        if(d.hasAttr(cg, "width")){ 
     301                                v.width = widthFromAttr(cg); 
     302                        } 
     303                        return v; // for vendetta 
     304                }); 
     305                if(!props.structure.length){ 
     306                        props.structure.push({ 
     307                                __span: Infinity, 
     308                                cells: [] // catch-all view 
     309                        });  
     310                } 
     311                // check to see if we're gonna have more than one view 
     312                 
     313                // for each tr in our th, create a row of cells 
     314                d.query("thead > tr", node).forEach(function(tr, tr_idx){ 
     315                        var cellCount = 0; 
     316                        var viewIdx = 0; 
     317                        var lastViewIdx; 
     318                        var cView = null; 
     319                        d.query("> th", tr).map(function(th){ 
     320                                // what view will this cell go into? 
     321 
     322                                // NOTE: 
     323                                //              to prevent extraneous iteration, we start counters over 
     324                                //              for each row, incrementing over the surface area of the 
     325                                //              structure that colgroup processing generates and 
     326                                //              creating cell objects for each <th> to place into those 
     327                                //              cell groups.  There's a lot of state-keepking logic 
     328                                //              here, but it is what it has to be. 
     329                                if(!cView){ // current view book keeping 
     330                                        lastViewIdx = 0; 
     331                                        cView = props.structure[0]; 
     332                                }else if(cellCount >= (lastViewIdx+cView.__span)){ 
     333                                        viewIdx++; 
     334                                        // move to allocating things into the next view 
     335                                        lastViewIdx += cView.__span; 
     336                                        lastView = cView; 
     337                                        cView = props.structure[viewIdx]; 
     338                                } 
     339 
     340                                // actually define the cell from what markup hands us 
     341                                var cell = { 
     342                                        name: d.trim(d.attr(th, "name")||th.innerHTML), 
     343                                        field: d.trim(d.attr(th, "field")||""), 
     344                                        colSpan: parseInt(d.attr(th, "colspan")||1) 
     345                                }; 
     346                                cellCount += cell.colSpan; 
     347                                cell.field = cell.field||cell.name; 
     348                                cell.width = widthFromAttr(th); 
     349                                if(!cView.cells[tr_idx]){ 
     350                                        cView.cells[tr_idx] = []; 
     351                                } 
     352                                cView.cells[tr_idx].push(cell); 
     353                        }); 
     354                }); 
     355                // console.debug(dojo.toJson(props.structure, true)); 
     356        } 
     357        return new dojox.Grid(props, node); 
     358} 
     359 
     360 
    257361// alias us to the right location 
    258362dojox.grid.Grid = dojox.Grid;