MarkoJS用于循环对象数组

问题描述 投票:1回答:2

我真的需要一个非常简单和琐碎的问题的帮助,但在这里它。我在NodeJS的服务器端使用marko并通过以下方式呈现我的视图:

ctx.render({
});

与koa-router和koa。我需要帮助html部门如何循环,或循环所有这些通过以下方式显示:

<ul>
  <li>
  </li>
</ul>

我已经尝试过并尝试过尝试,但是我太沮丧了,请有人拯救我,因为感觉就像周四的星期一大脑放屁-_-

"invoices": [
    {
        "id": 1,
        "customer_id": 1,
        "line_items_total": 187,
        "additional_fees": 10,
        "tax_rate": 0.07,
        "sub_total": 210.79
    },
    {
        "id": 2,
        "customer_id": 4,
        "line_items_total": 100,
        "additional_fees": 0,
        "tax_rate": 0.07,
        "sub_total": 107
    },
    {
        "id": 3,
        "customer_id": 2,
        "line_items_total": 48.4,
        "additional_fees": 0,
        "tax_rate": 0.07,
        "sub_total": 51.79
    },
    {
        "id": 4,
        "customer_id": 9,
        "line_items_total": 286,
        "additional_fees": 35,
        "tax_rate": 0.07,
        "sub_total": 343.47
    }
]

完整的项目文件位于:GitHub

这是在:

/routes/invoices/invoices.js

并且查询可以在以下位置找到:

/db/queries

指的是:

queries.objects.getAllObjects()

在:

/routes/invoices/invoices.js
node.js koa knex.js koa-router marko
2个回答
1
投票

您可以使用以下语法循环遍历数组,这是正确的:

<ul>
    <li for(invoice in data.invoices)>${invoice}</li>
</ul>

如果您还需要这样做,Marko还允许您遍历对象的属性:

<ul>
    <li for(invoice in data.invoices)>
            <ul>
                <li for(key,value in invoice)>
                    <strong>${key}</strong>: ${value}
                </li>
            </ul>
    </li>
</ul>

供参考:https://markojs.com/docs/core-tags/#codeltforgtcode


0
投票

L-O-L得到它,对于将来引用它的人来说,这很简单:

invoices.marko

文件以获得基本的了解。我当然可以:

${invoice.id}
${invoice.customer_id}
${invoice.line_items_total}
${etc}

列出所需.key的每个单独的属性/属性/值

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Invoices</title>
</head>
<body>
  <ul>
    <li for(invoice in data.invoices)>${invoice}</li>
  </ul>
</body>

© www.soinside.com 2019 - 2024. All rights reserved.