Home  >  Article  >  Web Front-end  >  js template engine handlebars.js practical tutorial??Using index in loop_html/css_WEB-ITnose

js template engine handlebars.js practical tutorial??Using index in loop_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:07:131381browse

 1 <!DOCTYPE html> 2 <html> 3   <head> 4     <META http-equiv=Content-Type content="text/html; charset=utf-8"> 5     <title>关于循环中索引的使用 - by 杨元</title> 6   </head> 7   <body> 8     <h1>关于循环中索引的使</h1> 9     <!--基础html框架-->10     <table>11       <thead>12         <tr>13           <th>序号</th>14           <th>姓名</th>15           <th>性别</th>16           <th>年龄</th>17         </tr>18       </thead>19       <tbody id="tableList">20         21       </tbody>22     </table>23     24     <!--插件引用-->25     <script type="text/javascript" src="script/jquery.js"></script>26     <!--注意!这个例子用的是新版本handlebars,旧版本不支持-->27     <script type="text/javascript" src="script/handlebars-v1.3.0.js"></script>28     29     <!--Handlebars.js模版-->30     <!--Handlebars.js模版放在script标签中,保留了html原有层次结构,模版中要写一些操作语句-->31     <!--id可以用来唯一确定一个模版,type是模版固定的写法-->32     <script id="table-template" type="text/x-handlebars-template">33       {{#each student}}34       <tr>35         <td>{{addOne @index}}</td>36         <td>{{name}}</td>37         <td>{{sex}}</td>38         <td>{{age}}</td>39       </tr>40       {{/each}}41     </script>42     43     <!--进行数据处理、html构造-->44     <script type="text/javascript">45       $(document).ready(function() {46         //模拟的json对象47          var data = {48                       "student": [49                           {50                               "name": "张三",51                               "sex": "0",52                               "age": 1853                           },54                           {55                               "name": "李四",56                               "sex": "0",57                               "age": 2258                           },59                           {60                               "name": "妞妞",61                               "sex": "1",62                               "age": 1963                           }64                       ]65                   };66         67         //注册一个Handlebars模版,通过id找到某一个模版,获取模版的html框架68         //$("#table-template").html()是jquery的语法,不懂的童鞋请恶补。。。69         var myTemplate = Handlebars.compile($("#table-template").html());70         71         //注册一个Handlebars Helper,用来将索引+1,因为默认是从0开始的72         Handlebars.registerHelper("addOne",function(index,options){73           return parseInt(index)+1;74         });75         76         //将json对象用刚刚注册的Handlebars模版封装,得到最终的html,插入到基础table中。77         $('#tableList').html(myTemplate(data));78       });79     </script>80   </body>81 </html>

The sequence number refers to: during the loop process, the index of the current loop. To put it bluntly, it is the current index of the loop. The number of loops usually starts from 0, the same is true in Handlebars.js.

What’s the use? The most common thing is to add a serial number to each row when displaying the table to clearly show how many records there are on the page, as in the example above.

In addition, in order to distinguish different IDs in the loop, Xiaocai just needs a serial number to ensure that the IDs are not repeated. Children's shoes who have used bootstrap should know that many events can be located by ID, such as the Listgroup control.

Without further ado, in Handlebars.js, you can get the current index through {{@index}}, which means that the @index variable represents the current index.

In the above example, it is not good for the table serial number to start from 0, so a Helper is registered and the index is 1.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn