如何在JSPDF中打印这些动态数据?

发布于 2025-01-19 17:34:04 字数 1313 浏览 2 评论 0原文

我有这些示例数据:

{
    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");
  };

使用这些代码,它将打印此 数据。未排列然后显示[Object,object] 输入图片此处描述

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]
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-26 17:34:04

它显示[对象,对象]的原因是,当您

data.map((item) => rows.push(Object.values(item)));

实际上是在不用任何数据转换的情况下推动整个数据对象,将其逐一推向阵列。因此,在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

data.map((item) => rows.push(Object.values(item)));

You are actually pushing the whole data object without any data transformation, to the rows array one by one. So on the House No. column, the [Object,Object] actually represents the cartItems object, and on the Address column the [Object,Object] actually is the value of orderCreatedAt.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文