简体   繁体   English

Mustache.js呈现矩阵

[英]Mustache.js render a matrix

How do I render a matrix in Mustache.js? 如何在Mustache.js中渲染矩阵?

Unfortunally I can not change the structure of the Matrix. 不幸的是,我无法更改矩阵的结构。

The following Matrix: 以下矩阵:

    var DiskMakerModels = [
        [ "Blue", "WD" ],
        [ "Green", "WD" ],
        [ "Black", "WD" ],
        [ "Purple", "WD" ],
        [ "Red", "WD" ],
        [ "Red Pro", "WD" ],
        [ "Gold", "WD" ],
        [ "Barracuda", "Seagate" ],
        [ "Firecuda", "Seagate" ],
        [ "Ironwolf", "Seagate" ],
        [ "Skyhawk", "Seagate" ]
    ];

Needs to be rendered into the following template: 需要呈现到以下模板中:

<script id="tableTemplate" type="x-tmpl-mustache">
    {{#rows}}
        <div class="list-item noPrint" dir="ltr">
            <input type="readonly" class="rowNum" value="1">
            <button onclick="addTableElement(this)">+</button>
            <input type="text" class="txtInp" value="{{.[0]}}">
            <input type="text" class="txtInp" value="{{.[1]}}">
            <button onClick="removeTableElement(this);">-</button>
        </div>
    {{/rows}}
</script>

where: {{.[0]}} and {{.[1]}} should be the first and second items of the specific row. 其中: {{.[0]}}{{.[1]}}应该是特定行的第一和第二项。

I believe the following tweak should do the trick: 我相信以下调整可以解决问题:

<script id="tableTemplate" type="x-tmpl-mustache">
    {{#rows}}
        <div class="list-item noPrint" dir="ltr">
            <input type="readonly" class="rowNum" value="1">
            <button onclick="addTableElement(this)">+</button>
            {{#.}}
                <input type="text" class="txtInp" value="{{.}}">
            {{/.}}
            <button onClick="removeTableElement(this);">-</button>
        </div>
    {{/rows}}
</script>

I've tested it out and it seems to work, though I'm running a php version of mustache so your mileage may vary. 我已经对其进行了测试,尽管我运行的是胡须的php版本,所以它似乎可以正常工作,因此您的行驶里程可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM