如何使用ejs将json中的数据动态添加到表中?

时间:2019-07-12 19:08:58

标签: node.js express ejs

所以我正在尝试从json文件导入数据并将其显示在表上。我会用硬编码将json文件放入其中,这确实很长,似乎很浪费时间。

我想要一个if语句,不是所有的对象都具有valueQuantity的值,所以我希望它显示其他内容。

我不断收到错误->无法读取未定义的'value'属性 它建议使用EJS-Lint,但是我不确定如何实现它。任何建议,我将不胜感激!

 ```
    <table id="documentTable">
    <tr>
    <% for (var i=0; i<obser.length; i++) {%
        <tr>
        <td><%-obser[i].type%></td>
        <td><%-obser[i].code.naming[0].display%></td>
        <% if (obser[i].valueQuantity.value) { %>
             <td><%-obser[i].valueQuantity.value%></td>
        <% } else {
             <td></td>
            <% } %>
       </td>
    </table>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

.forEach()

此外,最好使用{% for obj in patients %} <div class="modal fade" id="modalDelete{{ obj.pk }}" tabindex="-1" role="dialog" aria-labelledby="modalDelete" aria-hidden="true"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Delete patient!</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <p>Are you sure you want to delete this patient?</p> </div> <div class="modal-footer"> <form method="POST" action="{% url 'patients:patient_delete' obj.pk %}"> {% csrf_token %} <input class="btn btn-danger" value="Yes" type="submit" > <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </form> </div> </div> </div> </div> {% endfor %}循环遍历数组。 (items