Skip to content

Commit 8578b2b

Browse files
author
Laura Steadman
committed
Fix bugs and tidy cell plugin .table()
1 parent 6d30d40 commit 8578b2b

1 file changed

Lines changed: 43 additions & 28 deletions

File tree

jspdf.plugin.cell.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,28 @@
190190

191191
/**
192192
* 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.
194196
* @param {String[]} [headers] Omit or null to auto-generate headers at a performance cost
197+
195198
* @param {Object} [config.printHeaders] True to print column headers at the top of every page
196199
* @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)
198202
*/
203+
199204
jsPDFAPI.table = function (x,y, data, headers, config) {
205+
if (!data) {
206+
throw 'No data for PDF table';
207+
}
200208

201209
var headerNames = [],
202210
headerPrompts = [],
203211
header,
204-
autoSize,
205-
printHeaders,
206-
autoStretch,
207-
margins,
208212
i,
209213
ln,
214+
cln,
210215
columnMatrix = {},
211216
columnWidths = {},
212217
columnData,
@@ -216,7 +221,29 @@
216221
tableHeaderConfigs = [],
217222
model,
218223
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+
}
220247

221248
/**
222249
* @property {Number} lnMod
@@ -225,24 +252,14 @@
225252
this.lnMod = 0;
226253
lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
227254
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;
235257
this.margins = margins;
236258
this.setFontSize(fontSize);
237259
this.table_font_size = fontSize;
238260

239-
if (!data) {
240-
throw 'No data for PDF table';
241-
}
242-
243-
// Set headers
261+
// Set header values
244262
if (headers === undefined || (headers === null)) {
245-
246263
// No headers defined so we derive from data
247264
headerNames = this.getKeys(data[0]);
248265

@@ -261,9 +278,8 @@
261278
headerNames = headers;
262279
}
263280

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]}
267283
func = function (rec) {
268284
return rec[header];
269285
};
@@ -277,13 +293,11 @@
277293

278294
// get header width
279295
columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w);
280-
281296
column = columnMatrix[header];
282297

283298
// 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) {
285300
columnData = column[j];
286-
287301
columnMinWidths.push(this.getTextDimensions(columnData).w);
288302
}
289303

@@ -294,7 +308,7 @@
294308

295309
// -- Construct the table
296310

297-
if (config.printHeaders) {
311+
if (printHeaders) {
298312
var lineHeight = this.calculateLineHeight(headerNames, columnWidths, headerPrompts.length?headerPrompts:headerNames);
299313

300314
// Construct the header row
@@ -315,9 +329,10 @@
315329
var lineHeight;
316330
model = data[i];
317331
lineHeight = this.calculateLineHeight(headerNames, columnWidths, model);
332+
318333
for (j = 0, jln = headerNames.length; j < jln; j += 1) {
319334
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);
321336
}
322337
}
323338
this.lastCellPos = lastCellPos;

0 commit comments

Comments
 (0)