RactiveJS-06 List sections
来源:互联网 发布:不同后缀域名的区别 编辑:程序博客网 时间:2024/06/05 17:40
使用方法
使用{{#each list}}来对数组对象进行循环,看下面的例子:
<body> <div id="container"></div> <script type="text/ractive" id="template"> <table> <tr> <th>姓名</th> <th>年龄</th> <th>能力</th> </tr> {{#each superHeroes}} <tr> <td>{{name}}</td> <td>{{age}}</td> <td>{{ability}}</td> </tr> {{/each}} </table> </script> <script> var xmen = [{name : "金刚狼", age : 23, ability : 'eat'}, {name : "金刚狗", age : 32, ability : 'sleep'}, {name : "金刚熊", age : 23, ability : 'run'}]; var ractive1 = new Ractive({ el : '#container', template : '#template', data : { superHeroes : xmen } }); ractive1.set('superHeroes[0].name', '金刚猫') </script></body>
获得数组序号
Mustache并没能提供一个很好的方式获得数组序号,所以RactiveJS引入了序列变量(index reference)
{{#each list:num}} <!-- inside here, {{num}} is the index -->{{/each}}
通过将num声明为序列变量,就可以像使用其他变量一样使用它。将上面的表格的循环数据改为:
<table> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>能力</th> </tr> {{#each superHeroes:num}} <tr> <td>{{num+1}}</td> <td>{{name}}</td> <td>{{age}}</td> <td>{{ability}}</td> </tr>{{/each}}</table>
这样就可以获得各组的序号。
增加数组项
可以使用ractive.update(‘list’)来更新表格数据。注意,如果没有给此方法传入变量,则它会更新所有元素。
也可以直接使用数组方法(push,pop,shift,unshift,splice,sort,reverse)方法自动更新数组。如果不想让上述方法自动更新数据,可以再初始化选项中设置了modifyArrays: false。
<script> var xmen = [{name : "金刚狼", age : 23, ability : 'eat'}, {name : "金刚狗", age : 32, ability : 'sleep'}, {name : "金刚熊", age : 23, ability : 'run'}]; var ractive1 = new Ractive({ el : '#container', template : '#template', data : { superHeroes : xmen } }); xmen.push({name : "金刚兔", age : 123, ability : 'eat'}); xmen.reverse(); ractive1.update('superHeroes'); //ractive1.reverse('superHeroes',{name : "金刚兔", age : 123, ability : 'eat'} ); //ractive1.reverse('superHeroes');</script>
排序
==后边排序的例子我并没有看懂,回头再说吧。==
0 0
- RactiveJS-06 List sections
- RactiveJS-05 Conditional sections
- RactiveJS-01 Hello Ractive
- RactiveJS-02 Nested properties
- RactiveJS-03 Expressions
- RactiveJS-04 Event proxies
- RactiveJS-08 Extending Ractive
- RactiveJS-11 Transitions
- RactiveJS-12 Lifecycle eventes
- RactiveJS-13 Components
- RactiveJS-14 Components Example
- OpenMP Sections
- OpenMP Sections
- gcc -ffunction-sections -fdata-sections
- RactiveJS-07 Triples (embedded HTML)
- RactiveJS-09 Two-way binding
- RactiveJS-10 Partial + Animation +SVG
- Using Memory Sections
- linux命令中的参数前的一横(-)和两横(--)的区别
- ACM训练方法
- radial layout(径向布局)
- RactiveJS-05 Conditional sections
- 团体程序设计天梯赛-练习集L1-001
- RactiveJS-06 List sections
- Android扫雷:JSON 解析java.lang.IllegalStateException: Expected a name but was NUMBER 原因及解决方案
- 操作系统之抽烟者问题
- hdu 4614线段树+二分Vases and Flowers
- 杭电oj(java版)——1052 Tian Ji -- The Horse Racing
- 名企笔试:4399游戏2015校园(计算天数) 2017-04-01 算法爱好者
- RactiveJS-07 Triples (embedded HTML)
- 一个游戏程序员的代码书写观(一)
- BZOJ2436: [Noi2011]Noi嘉年华