Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 车把状况';if语句_Javascript_Handlebars.js - Fatal编程技术网

Javascript 车把状况';if语句

Javascript 车把状况';if语句,javascript,handlebars.js,Javascript,Handlebars.js,我正在了解handlebar.js,我想向社区提出一个问题。我知道我还有很多要学习的,我在路上,但是我想看看这个问题的例子。 在JS中创建的包含以下对象的数组: var data = [ { Field: "id", Type: "int(11)", Null: "NO", Key: "PRI", Default: null, Extra: "auto_increment" }, { Field: "id2", Type: "in

我正在了解handlebar.js,我想向社区提出一个问题。我知道我还有很多要学习的,我在路上,但是我想看看这个问题的例子。 在JS中创建的包含以下对象的数组:

var data = 
[
{
    Field: "id",
    Type: "int(11)",
    Null: "NO",
    Key: "PRI",
    Default: null,
    Extra: "auto_increment"
},
{
    Field: "id2",
    Type: "int(131)",
    Null: "N3O",
    Key: "PR3I",
    Default: null,
    Extra: "auto_increment"
}
];
<table>
        <thead>
        <tr>

        {{#each this}}
           {{#only_once this}}
            {{#key_value this}}
                <th>{{key}}</th>      
            {{/key_value }}
            {{/only_once}}
        {{/each}}

        </tr>    
        </thead>
...
格式是这样的,因为我从服务器接收到的JSON看起来就是这样,但是现在为了测试,我不想进行ajax调用

模板:

var data = 
[
{
    Field: "id",
    Type: "int(11)",
    Null: "NO",
    Key: "PRI",
    Default: null,
    Extra: "auto_increment"
},
{
    Field: "id2",
    Type: "int(131)",
    Null: "N3O",
    Key: "PR3I",
    Default: null,
    Extra: "auto_increment"
}
];
<table>
        <thead>
        <tr>

        {{#each this}}
           {{#only_once this}}
            {{#key_value this}}
                <th>{{key}}</th>      
            {{/key_value }}
            {{/only_once}}
        {{/each}}

        </tr>    
        </thead>
...

嗯,我试着写一个助手,但我觉得我做错了什么。我的理论是,当模板中的“
this
”指向数组时,我给它一个if,然后增加
I
,检查数组的索引是否大于0,最后如果它是真的,那么就发回一个false——所以我想它会告诉if,如果它没有在数组中运行代码,但我认为是错误的

在循环数组时,手柄提供帮助。我想你不需要任何定制助手

  • {{@index}}
    返回索引(0,1,2…)
  • {{@key}}
    返回键(字段、类型等)
  • {{@first}
    布尔值,用于标记这是否是数组中的第一行
  • {{@last}}
    布尔值,用于标记这是否是数组中的最后一行
如@SimonBoudrias在其回答中所述,由于Handlebars 1.1.0
{@first}
{{{{each}}
助手本机支持

因此,您可以仅使用Handlebar本机帮助程序打印数组中第一个对象的所有属性名称,如下所示:

{{#each array}}
    {{#if {{@first}}}}
        <!-- It is the first object on the array, print the key for each attribute -->
        {{#each this}}
            <th>{{@key}}</th>
        {{/each}}
   {{/if}}
{{/each}}
然后,仅当索引等于0时,您可以在模板中调用它,如下所示:

{{#each array}}
    {{#ifIsZero {{@index}}}}
        <!-- @index is equal to 0, do something -->
        <!-- eg. print the key for each attribute of the object using {{@key}} -->
        {{#each object}}
            <th>{{@key}}</th>
        {{/each}}

    {{else}}
        <!-- otherwise, do something else -->

    {{/ifIsZero}}
{{/each}}
{{#每个数组}
{{{#ifIsZero{{@index}}}
{{{#每个对象}}
{{@key}}
{{/每个}}
{{else}
{{/ifIsZero}}
{{/每个}}

我在车把的文档中看到了它,但我总是在控制台上看到一个错误:未捕获错误:第7行的解析错误:…ch this}{{@index}}------------------------------应该是'ID',得到'undefined',我应该使用其他东西而不是'this'吗?我试图使用“数据”,但它无法识别。在示例中,我只看到在对象内部使用{{{#each}}在数组或其他东西中循环。当循环对象时,使用
@key
而不是
@index
看起来您的数据对象不是您想象的那样。使用
{{log this}}
检查什么是真正的()。并确保使用最新版本的车把。哦,是的,该死的,这是一个多么愚蠢的错误!!:)我使用了一个github示例中的cdn,它指向Handlebar的beta版本,现在我再次尝试@index,它工作得非常好!尽管我的另一个问题仍然存在,所以我将使用下划线.js检查可能性。这是一个延伸。如果要执行此操作,请使用下划线模板。如果你想减少逻辑,请使用手柄。@SimonBoudrias:你说得对,添加条件if语句是一种延伸,这就是为什么我在答案的第一行提到它。无论如何,我已经更新了我的答案,以使用您在答案中提到的
first
帮助程序,因此建议的解决方案仅使用本机帮助程序。