0

アプリケーションで GET 操作を行うと、データが配列で返されます。

Value":[{"Id":"6b7","Notes":"testing","CreatedBy":"User1"},{"Id":"6b7","Notes":"Testing 1","CreatedBy":"User2"}]

上記を使用してテンプレートにデータを入力します。

<div class="create-note">
    <form>
        **<input type="hidden" id="Id" name="Id" value="{{this.Value.Id}}" />**       

        <textarea id="Notes" name="Notes"></textarea>       
        <button for="" type="submit" class="btn btn-primary ">Add Note</button>
        <button type="reset" class="btn">Cancel</button>
    </form>
            {{#unless this.Value.length}}
    <div class="alert alert-info">Notes do not exist.</div>
{{else}}
<table class="table">
    <thead>
        <tr>                        
            <th style="text-align:left">Note</th>
            <th style="text-align:left">Created By</th>
        </tr>
    </thead>
    <tbody>
        {{#each this.Value}}
            <tr>                
                <td>
                    {{this.Notes}}
                </td>
                <td>
                    {{this.CreatedBy}}
                </td>
            </tr>
        {{/each}}
    </tbody>
</table>
{{/unless}}
</div>

非表示のフォーム フィールド (Id) に入力するにはどうすればよいですか。value={{this.Value.Id}} は、配列があるため機能しません。

4

1 に答える 1

2

JavaScriptIdで引き出して、必要な場所に配置します。

var data = {
    "Value": [
        {"Id": "6b7", "Notes": "testing", "CreatedBy": "User1"},
        {"Id": "6b7", "Notes": "Testing 1", "CreatedBy": "User2"}
    ]
};
data.Id = data.Value[0].Id;

var tmpl = Handlebars.compile($('#template').html());
var html = tmpl(data);

そして、テンプレートでを参照{{this.Id}}(または単に) します。{{Id}}

デモ: http://jsfiddle.net/ambiguous/WSAxn/

または、値を掘り下げることができるヘルパーを追加しますが、それは不必要な複雑さのようです。

ハンドルバーを使用する場合は、データをハンドルバーに適した形式に変換することに慣れる必要があります。ハンドルバーのシンプルさには、長所と短所の両方があります。

于 2012-09-20T21:39:50.247 に答える