Comments (7)
I solved my issue by modifying the code a bit to accept a parse function. see below:
format for column object:
{title:'Date',key:'Created',parse:function(data){var date = new Date(data); return date.getFullYear();}}
Modified code (added else statement to initData:
function initData(params) {
// Transform from String[] to Object[]
if (typeof params.columns[0] === 'string') {
params.data.forEach(function (row, i) {
var obj = {};
for (var j = 0; j < row.length; j++) {
obj[j] = params.data[i][j];
}
params.data[i] = obj;
});
params.columns.forEach(function (title, i) {
params.columns[i] = {title: title, key: i};
});
}
else{
params.columns.forEach(function (title, i){
if(title.hasOwnProperty('parse')){
params.data.forEach(function (row, j) {
row[title.key] = title.parse(row[title.key]);
});
}
});
}
}
from jspdf-autotable.
Looks good! Will probably implement that in the next version of the library or accept a pull request if you create one.
from jspdf-autotable.
K - one other thought - this works well, but currently if i try to use the same key twice but parse differently, it overwrites because its writing back to the same data array passed in... it might be better to use the init function to always parse out the passed in data into a new array with keys generated by index...
example..
var columns = [
{title:'Year',key:'Created',parse:function(data){var date = new Date(data); return date.getFullYear();}},
{title:'Month',key:'Created',parse:function(data){var date = new Date(data); return date.getMonth();}}
}
This won't work without creating an entirely new array for data with new key values. Should be really easy, but will impact performance a little bit. May do this tomorrow.
from jspdf-autotable.
It has been unique keys by design. What is your use case for multiple keys with the same name? Anyway, I will consider allowing that in future versions.
from jspdf-autotable.
Well, the case that comes to the top of my head is the one mentioned above, where your data contains one date field (called Created) that you would like to parse into two separate columns on the PDF, like Day and Time (Day: Monday; Time: 5:00pm). Currently, they both get parsed to the row property for Created, so they overwrite each other.
Another example is when your data contains concatenations that you would like to parse to separate fields. For example, if your data includes the field Name which contains data formatted like: 'Hogan, Joe' - you may want to parse that into First Name: 'Joe' and Last Name 'Hogan' columns on your PDF.
Another example is when you have data in an array, like data = [{id:1, people: {results: [{name: 'Joe', phone: '123-456'}, {name:'John', phone: '789-1011'}]}}]
Using the parse function you can parse this out however your want, but only to a single column. You may, instead, want to parse it to a Names column and a Phone Numbers column...
from jspdf-autotable.
I think the benefits of forcing unique keys for columns outweigh the ones you mention. For example it means that:
- The order of the columns and headers doesn't matter
- You will always get the right data for the right header.
- You can style a specific column in the
drawCell
method/option
Your use cases are interesting and are definitely something I will look into however. Even if unique keys are forced there would still be possible to offer a custom parse function. You could for example rename the created
columns to created_month
and created_year
before calling the plugin method or omit the keys completely and use the array initialize option. It's not documented yet, but looks like this:
var cols = ["Header 1", "Header 2", "Header 3"];
var data = [
["Data 1", "Data 2", "Data 3"],
["Info 1", "Info 2", "Info 3"],
["Thing 1", "Thing 2", "Thing 3"]
];
var doc = new jsPDF('p', 'pt');
doc.autoTable(cols, data);
doc.save("table.pdf");
from jspdf-autotable.
I ended up not adding a parse function, but the createdCell
hook can be used in version 2.0 to modify the cells data after it has been parsed by the plugin.
from jspdf-autotable.
Related Issues (20)
- HTML Entities or HEX Code issues for symbols HOT 5
- line breaks are not working
- Change row colors on page break HOT 1
- jspdf Auto Table, can we break the if the lines are more than 10 HOT 1
- (0 , import_jspdf_autotable.default) is not a function HOT 9
- Textfield Signature?
- 表格项合并时表格项与表头信息不对应 HOT 2
- Multitable loop doesn't align properly and creates unusual gaps or overlaps each other HOT 11
- HorizontalPageBreak page break can continue on same page if vertical space is enough HOT 6
- Cannot Read html tags HOT 1
- Merge Cell Inside the body of the table HOT 1
- Parsing HTML with tables HOT 1
- Rowspan on heading HOT 1
- Inconsistent Table Width Across Pages When Exporting Using jsPDF-AutoTable HOT 4
- Table Border is off and value is not displayed when the first column is having large rowSpan. HOT 7
- Some columns are shrinked because of large texts in some columns HOT 2
- I wanted to highlight some part of text in cell text. HOT 1
- How to set specific startY even after page break HOT 1
- Autotable heading or subheading HOT 1
- Text with horizontal spaces are going out of cell boundry or text is hidden under the next column. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jspdf-autotable.