如何在JSPDF中打印这些动态数据?
我有这些示例数据:
{
lastName: "dasdasd",
totalAmount: 400,
cartItems: [
{
color: "Black",
size: "500",
quantity: 2,
cat: "ML",
name: "Tumbler",
price: 200
}
],
orderCreatedAt: { seconds: 1647338693, nanoseconds: 319000000 },
Address: "France",
houseNo: "7",
firstName: "Anna",
},
我想打印名字、姓氏、houseNo、地址、总额、购物车项目(仅名称和颜色),然后打印 orderCreatedAt 的时间戳日期。我在codesandbox中重新创建了这个: https:// /codesandbox.io/s/js-pdf-78272j?file=/src/App.js:184-627
const handlePrint = () => {
console.log("clicked");
const doc = new jsPDF();
doc.text("Order details", 20, 10);
const columns = [
"First Name",
"Last Name",
"House No.",
" Address",
" Cart Items",
"Total Amount",
"Order Created At"
];
const rows = [];
data.map((item) => rows.push(Object.values(item)));
doc.autoTable(columns, rows);
doc.save("order.pdf");
};
I have these example data:
{
lastName: "dasdasd",
totalAmount: 400,
cartItems: [
{
color: "Black",
size: "500",
quantity: 2,
cat: "ML",
name: "Tumbler",
price: 200
}
],
orderCreatedAt: { seconds: 1647338693, nanoseconds: 319000000 },
Address: "France",
houseNo: "7",
firstName: "Anna",
},
I wanted to print the first name, last name, houseNo, address, total amount, the cartItems(name and color only), and then the timestamp date of orderCreatedAt
. I recreated this in codesandbox: https://codesandbox.io/s/js-pdf-78272j?file=/src/App.js:184-627
const handlePrint = () => {
console.log("clicked");
const doc = new jsPDF();
doc.text("Order details", 20, 10);
const columns = [
"First Name",
"Last Name",
"House No.",
" Address",
" Cart Items",
"Total Amount",
"Order Created At"
];
const rows = [];
data.map((item) => rows.push(Object.values(item)));
doc.autoTable(columns, rows);
doc.save("order.pdf");
};
With these codes, it will print this data. It isn't arranged and then it shows [Object,object]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它显示[对象,对象]的原因是,当您
实际上是在不用任何数据转换的情况下推动整个数据对象,将其逐一推向
行
阵列。因此,在house no。
列上,[object,object]实际上表示cartitems
对象,以及address> address> address> address>对象]实际上是
OrderCreatEdat
的值。现在,您定义的
列
与所拥有的数据对象不符,因此您需要先进行一些数据转换。我已经编辑了codesandbox 在这里您的参考。另外,对于
OrderCreateDat
列,我猜您是从firebase获取数据,因此您可能需要从 firebase/firestore/firestore 包装进行转换,但人们还发现您可以将时间戳转换为与此公式。希望它有帮助。
The reason why it shows [Object,Object] is when you do
You are actually pushing the whole data object without any data transformation, to the
rows
array one by one. So on theHouse No.
column, the [Object,Object] actually represents thecartItems
object, and on theAddress
column the [Object,Object] actually is the value oforderCreatedAt
.Now the
columns
that you defined doesn't align with the data object you have, therefore you need to do some data transformation first. I have edited the codesandbox here for your reference.Also for the
orderCreatedAt
column, I'm guessing you are getting the data from firebase, so you may need to import the Timestamp from the firebase/firestore package to do the conversion, but people also found out you can convert the Timestamp to Date with this formula.Hope it helps.