- Ember.js Cookbook
- Erik Hanchett
- 533字
- 2021-07-16 12:58:03
Displaying a list of items
Often, you'll have a list of items that you'll need to iterate over. We can iterate through these items with the each
helper. This recipe will go over how to do this.
How to do it...
Let's say that we have a list of students and want to display them in our template. We'll use the each
helper to accomplish this.
- In a new project, generate
student
controller
andtemplate
:$ ember g template student $ ember g controller student
This will create the necessary files needed for our example.
- Update the
router.js
file with the newstudent
route:// app/router.js … Router.map(function() { this.route('student'); });
This will add a new
conditional
route. To access this route using the Ember server, open a web browser and navigate tohttp://localhost:4200/student
. - Update our student controller with an array of
students
as a property:// app/controllers/students.js import Ember from 'ember'; export default Ember.Controller.extend({ students: [ {name: 'Erik'}, {name: 'Jim'}, {name: 'Jane'}] });
This array has three student objects.
- In our
student.hbs
template, we'll iterate through thestudents
array using theeach
helper:// app/templates/student.hbs {{#each students as |student|}} {{student.name}}<br> {{/each}}
The first argument to the
each
helper is the array to be iterated over. In this case, this is thestudents
array that was declared in thestudent
controller. The|student|
blockparam
is what we'll use to iterate over the array.The
each
helper must be in the block form. In this example, each value of the student will be displayed with an HTML break afterward. - The output will look like this after being rendered:
Erik<br> Jim<br> Jane<br>
If, by chance, the array was empty, you can use
{{else}}
. - Add a new array to the template. This array can be empty or may not even exist:
// app/templates/student.hbs {{#each emptyArray as |item|}} {{item}} {{else}} Empty Array {{/each}}
The
else
block will be rendered only if the array is empty or doesn't exist.
Finding the index of the array
If needed, you can also access index
of the array in the second block param
.
- Create a new array and add the
index
blockparam
:// app/templates/student.hbs {{#each students as |student index|}} Student {{student.name}} is at index {{index}}<br> {{/each}}
After each iteration,
name
andindex
is displayed with an HTML break element. The index can be accessed using the double curly braces{{index}}
. - Assuming that we are using the same student array from earlier in this chapter, the rendered output will look as follows:
Student Erik is at index 0<br> Student Jim is at index 1<br> Student Jane is at index 2<br>
Keep in mind that
index
starts at0
and not1
.
How it works...
The each
helper uses block params to iterate through arrays. The each
helper takes an array argument and the block param
is used to iterate each inpidual item on the list. If the array doesn't exist or is empty, you can use else
to display a message instead.
In the recipes in this chapter, the students
array was declared in controller
. It had several student objects that could be accessed by the template. The template used this array and iterated over it with the each
helper.
- LabVIEW程序設(shè)計基礎(chǔ)與應(yīng)用
- 騰訊iOS測試實踐
- JavaScript+jQuery網(wǎng)頁特效設(shè)計任務(wù)驅(qū)動教程(第2版)
- NLTK基礎(chǔ)教程:用NLTK和Python庫構(gòu)建機(jī)器學(xué)習(xí)應(yīng)用
- Expert Android Programming
- Swift語言實戰(zhàn)精講
- ANSYS Fluent 二次開發(fā)指南
- 運(yùn)用后端技術(shù)處理業(yè)務(wù)邏輯(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- C語言從入門到精通
- 時空數(shù)據(jù)建模及其應(yīng)用
- 大學(xué)計算機(jī)基礎(chǔ)
- Java 從入門到項目實踐(超值版)
- 一步一步跟我學(xué)Scratch3.0案例
- 零基礎(chǔ)學(xué)C語言(第4版)
- C++ System Programming Cookbook