Git Product home page Git Product logo

Comments (7)

JoeHogan avatar JoeHogan commented on August 26, 2024

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.

simonbengtsson avatar simonbengtsson commented on August 26, 2024

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.

JoeHogan avatar JoeHogan commented on August 26, 2024

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.

simonbengtsson avatar simonbengtsson commented on August 26, 2024

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.

JoeHogan avatar JoeHogan commented on August 26, 2024

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.

simonbengtsson avatar simonbengtsson commented on August 26, 2024

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.

simonbengtsson avatar simonbengtsson commented on August 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.