| 1 | (function(){ |
|---|
| 2 | function AddEventListener( elem, evt, func, capture){ |
|---|
| 3 | capture = capture || false; |
|---|
| 4 | if(elem.addEventListener) elem.addEventListener( evt, func, capture); |
|---|
| 5 | else elem.attachEvent('on'+evt, func); |
|---|
| 6 | return func; |
|---|
| 7 | }; |
|---|
| 8 | var InitQuery = function(){ |
|---|
| 9 | function createTableRow( numTds ){ |
|---|
| 10 | var tr = document.createElement('tr'); |
|---|
| 11 | var td = document.createElement('td'); |
|---|
| 12 | td.style.backgroundColor="#EEF"; |
|---|
| 13 | for(var i=0 ; i < numTds ; i++){ |
|---|
| 14 | tr.appendChild(td.cloneNode(true)); |
|---|
| 15 | } |
|---|
| 16 | return tr; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | var _tbls = document.getElementsByTagName('table'); |
|---|
| 20 | var tbls = [], tbl, cell; |
|---|
| 21 | // filter so that we only get ticket listing tables |
|---|
| 22 | for(var i=0 ; tbl = _tbls[i] ; i++ ){ |
|---|
| 23 | if(tbl.className == 'listing tickets') tbls.push(tbl); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | // find numerical columns |
|---|
| 27 | tbl = tbls[0]; |
|---|
| 28 | |
|---|
| 29 | var cells = tbl.tBodies[0].rows[0].cells; |
|---|
| 30 | var cellIdxs = [], columnNames = {}; |
|---|
| 31 | for(var i=0 ; cell = cells[i] ; i++ ){ |
|---|
| 32 | if(cells[i].textContent!="" && !isNaN(Number(cells[i].textContent))){ |
|---|
| 33 | cellIdxs.push(i); |
|---|
| 34 | if(tbl && tbl.tHead.rows.length > 0) |
|---|
| 35 | columnNames[i] = tbl.tHead.rows[0].cells[i].textContent; |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | // total the numerical columns and add a total row to each table |
|---|
| 40 | var totals, total_totals =[], idx; |
|---|
| 41 | for(var i=0 ; tbl = tbls[i] ; i++ ){ |
|---|
| 42 | totals = []; |
|---|
| 43 | for(var k=0 ; row = tbl.tBodies[0].rows[k] ; k++){ |
|---|
| 44 | for(var j=0 ; idx = cellIdxs[j] ; j++){ |
|---|
| 45 | if(totals[idx] == null) totals[idx] = 0; |
|---|
| 46 | if(total_totals[idx] == null) total_totals[idx] = 0; |
|---|
| 47 | if(row.cells[idx].textContent!="" && !isNaN(Number(row.cells[idx].textContent))){ |
|---|
| 48 | var val = Number(row.cells[idx].textContent); |
|---|
| 49 | total_totals[idx] += val |
|---|
| 50 | totals[idx] += val; |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | if(tbl.tBodies[0].rows.length > 0){ |
|---|
| 55 | var tr = createTableRow(tbl.tBodies[0].rows[0].cells.length); |
|---|
| 56 | for(var j=0 ; idx = cellIdxs[j] ; j++){ |
|---|
| 57 | tr.cells[idx].appendChild(document.createTextNode(totals[idx])); |
|---|
| 58 | } |
|---|
| 59 | tbl.tBodies[0].appendChild(tr); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | // If we are grouping by things, we are going to want to add a complete total to the bottom |
|---|
| 63 | if(tbls.length > 1){ |
|---|
| 64 | var totalHtml = document.createElement('div'); |
|---|
| 65 | totalHtml.style.backgroundColor="#EEF"; |
|---|
| 66 | for(var j=0 ; idx = cellIdxs[j] ; j++){ |
|---|
| 67 | totalHtml.appendChild(document.createTextNode("Total "+columnNames[idx]+": "+total_totals[idx])); |
|---|
| 68 | totalHtml.appendChild(document.createElement('br')); |
|---|
| 69 | } |
|---|
| 70 | tbls[0].parentNode.appendChild(totalHtml); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | AddEventListener(window, 'load', InitQuery) |
|---|
| 74 | })() |
|---|