|
190 | 190 |
|
191 | 191 | /** |
192 | 192 | * Create a table from a set of data. |
193 | | - * @param {Object[]} data As array of objects containing key-value pairs |
| 193 | + * @param {Integer} [x] : left-position for top-left corner of table |
| 194 | + * @param {Integer} [y] top-position for top-left corner of table |
| 195 | + * @param {Object[]} [data] As array of objects containing key-value pairs corresponding to a row of data. |
194 | 196 | * @param {String[]} [headers] Omit or null to auto-generate headers at a performance cost |
| 197 | +
|
195 | 198 | * @param {Object} [config.printHeaders] True to print column headers at the top of every page |
196 | 199 | * @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value |
197 | | - * @param {Object} [config.autoStretch] True to force the table to fit the width of the page |
| 200 | + * @param {Object} [config.margins] margin values for left, top, bottom, and width |
| 201 | + * @param {Object} [config.fontSize] Integer fontSize to use (optional) |
198 | 202 | */ |
| 203 | + |
199 | 204 | jsPDFAPI.table = function (x,y, data, headers, config) { |
| 205 | + if (!data) { |
| 206 | + throw 'No data for PDF table'; |
| 207 | + } |
200 | 208 |
|
201 | 209 | var headerNames = [], |
202 | 210 | headerPrompts = [], |
203 | 211 | header, |
204 | | - autoSize, |
205 | | - printHeaders, |
206 | | - autoStretch, |
207 | | - margins, |
208 | 212 | i, |
209 | 213 | ln, |
| 214 | + cln, |
210 | 215 | columnMatrix = {}, |
211 | 216 | columnWidths = {}, |
212 | 217 | columnData, |
|
216 | 221 | tableHeaderConfigs = [], |
217 | 222 | model, |
218 | 223 | jln, |
219 | | - func; |
| 224 | + func, |
| 225 | + |
| 226 | + //set up defaults. If a value is provided in config, defaults will be overwritten: |
| 227 | + autoSize = false, |
| 228 | + printHeaders = true, |
| 229 | + fontSize = 12, |
| 230 | + margins = {left:0, top:0, bottom: 0, width: this.internal.pageSize.width}; |
| 231 | + |
| 232 | + if (config) { |
| 233 | + //override config defaults if the user has specified non-default behavior: |
| 234 | + if(config.autoSize === true) { |
| 235 | + autoSize = true; |
| 236 | + } |
| 237 | + if(config.printHeaders === false) { |
| 238 | + printHeaders = false; |
| 239 | + } |
| 240 | + if(config.fontSize){ |
| 241 | + fontSize = config.fontSize; |
| 242 | + } |
| 243 | + if(config.margins){ |
| 244 | + margins = config.margins; |
| 245 | + } |
| 246 | + } |
220 | 247 |
|
221 | 248 | /** |
222 | 249 | * @property {Number} lnMod |
|
225 | 252 | this.lnMod = 0; |
226 | 253 | lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined }, |
227 | 254 | pages = 1; |
228 | | - if (config) { |
229 | | - autoSize = config.autoSize || false; |
230 | | - printHeaders = this.printHeaders = config.printHeaders || true; |
231 | | - autoStretch = config.autoStretch || true; |
232 | | - fontSize = config.fontSize || 12; |
233 | | - margins = config.margins || {left:0, top:0, bottom: 0, width: this.internal.pageSize.width}; |
234 | | - } |
| 255 | + |
| 256 | + this.printHeaders = printHeaders; |
235 | 257 | this.margins = margins; |
236 | 258 | this.setFontSize(fontSize); |
237 | 259 | this.table_font_size = fontSize; |
238 | 260 |
|
239 | | - if (!data) { |
240 | | - throw 'No data for PDF table'; |
241 | | - } |
242 | | - |
243 | | - // Set headers |
| 261 | + // Set header values |
244 | 262 | if (headers === undefined || (headers === null)) { |
245 | | - |
246 | 263 | // No headers defined so we derive from data |
247 | 264 | headerNames = this.getKeys(data[0]); |
248 | 265 |
|
|
261 | 278 | headerNames = headers; |
262 | 279 | } |
263 | 280 |
|
264 | | - if (config.autoSize) { |
265 | | - |
266 | | - // Create Columns Matrix |
| 281 | + if (autoSize) { |
| 282 | + // Create a matrix of columns e.g., {column_title: [row1_Record, row2_Record]} |
267 | 283 | func = function (rec) { |
268 | 284 | return rec[header]; |
269 | 285 | }; |
|
277 | 293 |
|
278 | 294 | // get header width |
279 | 295 | columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w); |
280 | | - |
281 | 296 | column = columnMatrix[header]; |
282 | 297 |
|
283 | 298 | // get cell widths |
284 | | - for (j = 0, ln = column.length; j < ln; j += 1) { |
| 299 | + for (j = 0, cln = column.length; j < cln; j += 1) { |
285 | 300 | columnData = column[j]; |
286 | | - |
287 | 301 | columnMinWidths.push(this.getTextDimensions(columnData).w); |
288 | 302 | } |
289 | 303 |
|
|
294 | 308 |
|
295 | 309 | // -- Construct the table |
296 | 310 |
|
297 | | - if (config.printHeaders) { |
| 311 | + if (printHeaders) { |
298 | 312 | var lineHeight = this.calculateLineHeight(headerNames, columnWidths, headerPrompts.length?headerPrompts:headerNames); |
299 | 313 |
|
300 | 314 | // Construct the header row |
|
315 | 329 | var lineHeight; |
316 | 330 | model = data[i]; |
317 | 331 | lineHeight = this.calculateLineHeight(headerNames, columnWidths, model); |
| 332 | + |
318 | 333 | for (j = 0, jln = headerNames.length; j < jln; j += 1) { |
319 | 334 | header = headerNames[j]; |
320 | | - this.cell(x, y, columnWidths[header], lineHeight, model[header], i + 2, headers[j].align); |
| 335 | + this.cell(x, y, columnWidths[header], lineHeight, model[header], i + 2, header.align); |
321 | 336 | } |
322 | 337 | } |
323 | 338 | this.lastCellPos = lastCellPos; |
|
0 commit comments