动态车把表创建

时间:2019-05-13 02:58:50

标签: javascript handlebars.js

我无法使用把手动态创建表格。 “数组”是对象的数组(每个对象都有ID,名称,描述和类别)。

我不知道下面的代码为什么可用于li元素插入而不可用于表。有人可以帮忙吗?

        let arrays = [{ id: 0, name: "name0", description: "this is is name0","category":1} ,{ id: 1, name: "name1", description: "this is is name1","category":2},...]
let template =Handlebars.compile($('#template1').html())
let html = template({arrays: arrays})

$('#div1').append(html)          

    <template id="template1">
        <table>
            <thead>
            <tr>
                <th>ID</th>
                <th>name</th>
                <th>description</th>
            </tr>

            </thead>
            <tbody>
                {{#each arrays}}
                <tr>
                    <td>{{id}}</td>
                    <td>{{name}}</td>
                    <td>{{desciption}}</td>
                </tr>
                {{/each}}
            </tbody>
        </table>
    </template>

期望是一个只有ID名称和描述但没有类别的表

1 个答案:

答案 0 :(得分:0)

您正在尝试在处理编译后插入模板。除了与车把无关的零件之外,您的模板将无法使用。如果您要在HTML文件中完成插入后立即处理模板,请尝试在追加之后而不是之前进行模板编译。

通常,我使用jquery $(document).ready(...)进行此类延迟。

相关问题