108
Views
5
Comments
Solved
how to use table records in javascript

Hello,

I'm trying to generate pdf using javascript library "jspdf autotable", it's working fine if I inserted the table records by myself as shown

My question is: how to use the records from the table automatically without writing all the records by myself?

p.s: I tried to use JSONSerailize and using the output of it in the javascript widget but it's not working.

PaginationandSortingExercise.oml
2022-05-02 13-50-49
Paulo Ritto
Solution

Hi,

To easily manipulate your table of records as a Javascript Object, you should do the following:

const jsonObj = JSON.parse($parameters.Data);

After that you can consult that jsonObj to check the list of records it has and pass it to the PDF function.

I don't have the time to post the code, but I will get to you later if you still didn't manage to do it.


PS: Don't use var in JS (I saw you used that), const/let is already supported in OS, go check the differences in documentation to see the drawbacks of var, use const for values that you are not going to change.


Cheers,

Paulo

UserImage.jpg
Mohamed Zakaria

Hello Paulo,

Is this what you mean to change?

I did that but still doesn't work.

2022-05-02 13-50-49
Paulo Ritto
Solution

Right, now to get the collumns you can do this:


Object.keys will return the array of key values (collumns) of your table.


Object.values will return the row values of your table. you have to make a for loop to get all value rows, increasing the index:


Then pass the collumns and allrows to your pdf function

Cheers,

Paulo

2022-05-02 13-50-49
Paulo Ritto
Solution

Hi,

To easily manipulate your table of records as a Javascript Object, you should do the following:

const jsonObj = JSON.parse($parameters.Data);

After that you can consult that jsonObj to check the list of records it has and pass it to the PDF function.

I don't have the time to post the code, but I will get to you later if you still didn't manage to do it.


PS: Don't use var in JS (I saw you used that), const/let is already supported in OS, go check the differences in documentation to see the drawbacks of var, use const for values that you are not going to change.


Cheers,

Paulo

UserImage.jpg
Mohamed Zakaria

Hello Paulo,

Is this what you mean to change?

I did that but still doesn't work.

2022-05-02 13-50-49
Paulo Ritto

Hi, 

Your JSON.parse will return your whole table, not just the rows, that's why it doesn't work doing it like that. You have to check which attribute of that object returns the rows. to check the object structure, you can console.log the result of that JSON.parse, to check the structure that you have in that object.


Cheers,

Paulo

UserImage.jpg
Mohamed Zakaria

Yes as you said it returns the whole table


2022-05-02 13-50-49
Paulo Ritto
Solution

Right, now to get the collumns you can do this:


Object.keys will return the array of key values (collumns) of your table.


Object.values will return the row values of your table. you have to make a for loop to get all value rows, increasing the index:


Then pass the collumns and allrows to your pdf function

Cheers,

Paulo

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.