/** =============================================================== DATA GRID v1.0 Copyright (c) 2008,2009 Jason Fries, Iowa Electronic health Markets, http://iehm.uiowa.edu/iehm/index.html University of Iowa dependencies: revisions: 2009.7.22 first release =============================================================== **/ function DataGrid( fields, itemRenderers, labelFunctions ) { this.element = document.createElement("DIV"); this.dataprovider = []; this.columns = []; this.minColumns = 6; this.maxRows = 10; this.table; this.init(fields, itemRenderers, labelFunctions); } DataGrid.VERSION = "Release 1.0.0 [07.22.2009]"; /** * TODO: Better way of implementing columns */ DataGrid.prototype.init = function( fields, itemRenderers, labelFunctions ) { //create column for each field for(var i=0; i 0){ this.element.removeChild( this.table.element ); this.table = null; } var rows = Math.max(this.columns[0].data.length, this.maxRows); //create new table this.table = new NFTable("", rows, this.columns.length, true ); this.table.element.setAttribute("class", "dataGrid"); this.table.element.setAttribute("className", "dataGrid"); //this.table.element.setAttribute("border", 1); //this.table.element.style.width = "100%"; //this.table.element.style.border = "1px solid #ADADAD"; //this.table.element.style.background = "white url(../imgs/broker/gradient.png) repeat-x top" //enable header this.table.enableHeader = true; for(var i=0; i < this.columns.length; i++){ this.table.headerCells[i].appendChild( document.createTextNode(this.columns[i].headerText) ); this.table.headerCells[i].style.borderBottom = "1px solid #ADADAD"; this.table.headerCells[i].style.textAlign = this.columns[i].textAlign; if(i != this.columns.length-1) this.table.headerCells[i].style.borderRight = "1px solid #cFcFcF"; //HACK Fix column span for contract name if(this.columns[i].dataField == "color") this.table.headerCells[i].setAttribute("colSpan",2); if(this.columns[i].dataField == "name"){ this.table.headerCells[i].parentNode.removeChild(this.table.headerCells[i]); } } for(var i=0; i < rows; i++){ for(var j=0; j < this.columns.length; j++){ var data = this.columns[j].data[i]; try{ this.table.cell(i,j).appendChild( data ); }catch(e){ this.table.cell(i,j).appendChild( document.createTextNode(" ") ); } this.table.cell(i,j).style.padding = "2px"; this.table.cell(i,j).setAttribute("align",this.columns[j].textAlign); this.table.cell(i,j).style.textAlign = this.columns[j].textAlign; this.table.cell(i,j).style.minWidth = this.columns[j].minWidth + "px"; if(this.columns[j].width != "auto") this.table.cell(i,j).style.width = this.columns[j].width + "px"; this.table.cell(i,j).style.height = this.columns[j].minHeight + "px"; this.table.cell(i,j).setAttribute("height", this.columns[j].minHeight + "px"); if(j != this.columns.length-1) this.table.cell(i,j).style.borderRight = "1px solid #ADADAD"; } //alternating row colors if(i%2 == 0) this.table.row(i).style.backgroundColor = "#E5E5E5"; } //append new table this.element.appendChild(this.table.element); } /** * Data Provider * @description */ DataGrid.prototype.setDataprovider = function( data ) { this.dataprovider = data; this.refresh(); } /** * Add Column * @descriptions Create datagrid column */ DataGrid.prototype.addColumn = function( col ) { this.columns.push( col ); } /** =============================================================== COLUMN v1.0 Copyright (c) 2008,2009 Jason Fries, Iowa Electronic health Markets, http://iehm.uiowa.edu/iehm/index.html University of Iowa dependencies: revisions: 2009.7.22 first release =============================================================== **/ function Column(parent) { this.parent = parent; this.dataField = null; this.itemRenderer = textItemRenderer; //how do we display this data this.headerText = ""; this.sortable = true; this.editable = false; this.textAlign = "center"; this.labelFunction = defaultLabelFunction; this.minWidth = 30; this.minHeight = 28; this.width = "auto"; //rendered table cell data items this.data = []; } Column.prototype.createCell = function() { return document.createElement("TD"); } /** * Set Data * @description Clear existing data and render each data item */ Column.prototype.refresh = function() { this.data = []; for(var i=0; i < this.parent.dataprovider.length; i++ ){ var item = this.parent.dataprovider[i]; var dataItem = " "; //get column's target parameter for(var param in item){ if( param != this.dataField) continue; dataItem = item[param]; break; } this.data.push( this.itemRenderer( this.labelFunction(dataItem) ) ); } } /** ITEM RENDERERS **/ function textItemRenderer(data) { return document.createTextNode(data); } function iconItemRenderer(data) { var icon = createShadedIcon( data, 20, "/iehm/imgs/broker/insetFINAL.png" ); icon.style.verticalAlign = "middle"; icon.style.width = "20px"; return icon; } function buttonItemRenderer(data) { if(!data) return document.createElement("DIV"); var button = document.createElement("INPUT"); button.setAttribute("type", "button"); button.value = data.label; button["parent"] = data; button.style.width = "75px"; AttachEvent( button, "click", data.clickHandler ); return button; } /** * Convert NFOrder to JSON Object for DataGrid rendering */ function orderToJSON( order ) { var handler = function() { alert("Click!"); }; if(!order) { return { id:-1, color:0xFF0000, login:"error", name:"error", type:"error", qty:"error", price:"error", action:{id:-1, label:"accept",clickHandler:handler} }; } return { id:order.id, color:order.market.color, login:order.user.login, name:order.name, type:order.type, qty:order.qty, price:order.price, action:{id:order.id, qty:order.qty, price:order.price, type:order.type, name:order.name, label:"accept", clickHandler:handler} }; } function orderBookToDataProvider( dp ) { var converted = []; for(var i=0; i < dp.length; i++){ converted.push( orderToJSON(dp[i]) ); } return converted; } /** LABEL FUNCTIONS **/ function defaultLabelFunction(item) { return item; } /** * Default Label Function * @description return input unchanged */ function priceLabelFunction(item) { return item + "\u00A2"; } function qtyLabelFunction(item) { return item + " x"; }