|
49 | 49 | return maxLn; |
50 | 50 | }; |
51 | 51 |
|
52 | | - jsPDFAPI.getTextDimentions = function (txt) { |
| 52 | + jsPDFAPI.getTextDimensions = function (txt) { |
53 | 53 | fontName = this.internal.getFont().fontName; |
54 | 54 | fontSize = this.internal.getFontSize(); |
55 | 55 | fontStyle = this.internal.getFont().fontStyle; |
|
110 | 110 | doc2.save('Test.pdf'); |
111 | 111 | |
112 | 112 | */ |
113 | | - |
| 113 | + |
| 114 | + /** |
| 115 | + * Create a table from an {#Ext.data.Store} or a set of data. |
| 116 | + * @param dataSet {Ext.data.Model[]|Ext.data.Store} |
| 117 | + * @param [headers] {Array} Omit or null to auto-generate headers at a performance cost |
| 118 | + * @param [autoSize=true] {Boolean} |
| 119 | + */ |
| 120 | + jsPDFAPI.table = function(dataSet, headers, config){ |
| 121 | + |
| 122 | + var models; |
| 123 | + |
| 124 | + this.lnMod = 0; |
| 125 | + |
| 126 | + if(config){ |
| 127 | + var autoSize = config.autoSize || false, |
| 128 | + printHeaders = this.printHeaders = config.printColumnHeaders || true, |
| 129 | + autoStretch = config.autoStretch || true; |
| 130 | + } |
| 131 | + |
| 132 | + if(!dataSet){ |
| 133 | + Ext.Error.raise('No data for PDF table'); |
| 134 | + } |
| 135 | + |
| 136 | + if(dataSet.isStore){ |
| 137 | + var isStore = true; |
| 138 | + models = dataSet.data.items; |
| 139 | + } else { |
| 140 | + models = dataSet; |
| 141 | + } |
| 142 | + |
| 143 | + // Set headers |
| 144 | + if(headers === undefined || (headers === null)){ |
| 145 | + // No headers defined so we derive from data |
| 146 | + if(isStore){ |
| 147 | + headers = Ext.Array.pluck(dataSet.model.getFields(), 'name'); |
| 148 | + } else { |
| 149 | + headers = Ext.Object.getKeys(models[0]); |
| 150 | + } |
| 151 | +// console.log(headers); |
| 152 | + } |
| 153 | + |
| 154 | + // Create Columns Matrix |
| 155 | + var columnMatrix = {}, |
| 156 | + columnWidths = {}, |
| 157 | + index, |
| 158 | + columnData, |
| 159 | + TextMetrics = new Ext.util.TextMetrics(); |
| 160 | + |
| 161 | + for (var i = 0, ln = headers.length; i < ln; i++) { |
| 162 | + index = headers[i]; |
| 163 | + columnMatrix[index] = Ext.Array.map(models, function(rec){ |
| 164 | + debugger; |
| 165 | + if(!rec.get){ |
| 166 | + console.log(rec.$className); |
| 167 | + } |
| 168 | + return rec.get(index); |
| 169 | + }); |
| 170 | + |
| 171 | + var columnMinWidths = [], column; |
| 172 | + |
| 173 | + // get header width |
| 174 | + columnMinWidths.push(this.getTextDimensions(index).w); |
| 175 | +// columnMinWidths.push(TextMetrics.getWidth(index)/3.7795275593333); |
| 176 | + |
| 177 | + column = columnMatrix[index]; |
| 178 | + |
| 179 | + // Get cell widths |
| 180 | + for (var j = 0, ln = columnMatrix[index].length; j < ln; j++) { |
| 181 | + columnData = columnMatrix[index][j]; |
| 182 | + columnMinWidths.push(this.getTextDimensions(columnData).w); |
| 183 | + |
| 184 | +// columnMinWidths.push(TextMetrics.getWidth(columnData)/3.7795275593333); |
| 185 | + } |
| 186 | + |
| 187 | + // get final column width |
| 188 | + columnWidths[index] = Ext.Array.max(columnMinWidths); |
| 189 | + } |
| 190 | + |
| 191 | + // Construct the table |
| 192 | + |
| 193 | + // Construct the header row |
| 194 | + var header, tableHeaderConfigs = []; |
| 195 | + for (var i = 0, ln = headers.length; i < ln; i++) { |
| 196 | + header = headers[i]; |
| 197 | + tableHeaderConfigs.push([10, 10, columnWidths[header], 25, String(header)]); |
| 198 | + } |
| 199 | + |
| 200 | + this.setTableHeaderRow(tableHeaderConfigs); |
| 201 | + |
| 202 | + this.printHeaderRow(1); |
| 203 | + |
| 204 | + // Construct the data rows |
| 205 | + var record; |
| 206 | + |
| 207 | + for (var i = 0, ln = models.length; i < ln; i++) { |
| 208 | + record = models[i]; |
| 209 | + |
| 210 | + for (var j = 0, jln = headers.length; j < jln; j++) { |
| 211 | + index = headers[j]; |
| 212 | + this.cell(10, 10, columnWidths[index], 25, String(record.get(index)), i+2); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + return this; |
| 217 | + }; |
| 218 | + |
| 219 | + jsPDFAPI.setTableHeaderRow = function(config){ |
| 220 | + this.tableHeaderRow = config; |
| 221 | + }; |
| 222 | + |
| 223 | + jsPDFAPI.printHeaderRow = function(lineNumber){ |
| 224 | + this.printingHeaderRow = true; |
| 225 | + for (var i = 0, ln = this.tableHeaderRow.length; i < ln; i++) { |
| 226 | + var tableHeaderCell = this.tableHeaderRow[i]; |
| 227 | + var tmp = [].concat(tableHeaderCell); |
| 228 | + tmp = tmp.concat(lineNumber); |
| 229 | + this.cell.apply(this,tmp); |
| 230 | + } |
| 231 | + this.printingHeaderRow = false; |
| 232 | + }; |
| 233 | + |
114 | 234 | jsPDFAPI.cell = function (x, y, w, h, txt, ln) { |
| 235 | + if(this.printingHeaderRow !== true && this.lnMod !== 0){ |
| 236 | + ln = ln + this.lnMod; |
| 237 | + } |
| 238 | + |
115 | 239 | if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1) { |
116 | 240 | this.cellAddPage(); |
| 241 | + if(this.printHeaders && this.tableHeaderRow){ |
| 242 | + this.printHeaderRow(ln); |
| 243 | + this.lnMod++; |
| 244 | + ln++; |
| 245 | + } |
117 | 246 | this.setLastCellPosition(undefined, undefined, undefined, undefined, undefined); |
118 | 247 | if (this.getMaxLn() === 0) { |
119 | 248 | this.setMaxLn(ln); |
120 | 249 | } |
121 | 250 | } else if (pages > 1 && this.getMaxLn() === ln / pages) { |
122 | 251 | this.cellAddPage(); |
| 252 | + if(this.printHeaders && this.tableHeaderRow){ |
| 253 | + this.printHeaderRow(ln); |
| 254 | + this.lnMod++; |
| 255 | + ln++; |
| 256 | + } |
| 257 | + |
123 | 258 | this.setLastCellPosition(undefined, undefined, undefined, undefined, undefined); |
124 | 259 | } |
125 | | - var curCell = this.getLastCellPosition(), |
126 | | - dim = this.getTextDimentions(txt); |
| 260 | + var curCell = this.getLastCellPosition(); |
| 261 | + |
127 | 262 | if (curCell.x !== undefined && curCell.ln === ln) { |
128 | 263 | x = curCell.x + curCell.w; |
129 | 264 | if (curCell.y !== undefined && curCell.y === y) { |
|
0 commit comments