Creating Template Contents Dynamically Based on a List of Items

Polymer provides you a way to generate elements based on a list of items using template repeater (dom-repeat).

HTML

  1. <dom-module id="employees-list">
  2. <template>
  3. <table>
  4. <tr on-click="processElement">
  5. <th>Name</th><th>Title</th><th>Email</th>
  6. </tr>
  7. <template is="dom-repeat" items="[[employees]]">
  8. <tr on-click="handleClick" id="[[item.name]]">
  9. <td>{{item.name}}</td>
  10. <td>{{item.title}}</td>
  11. <td>{{item.email}}</td>
  12. </tr>
  13. </template>
  14. </table>
  15. </template>
  16. <script>
  17. class EmployeesList extends Polymer.Element {
  18. static get is() {return 'employees-list'}
  19. }
  20. customElements.define(EmployeesList.is, EmployeesList);
  21. </script>
  22. </dom-module>

The above template might look like this once populated with a list of employees:

NameTitleEmail

John D

Developer

jd@foo.bar

Jane D

Designer

janed@foo.bar

Mike D

Architect

mikey@foo.bar

The dom-repeat element marks the content that is generated for each item in a list. In the above example the table’s rows and everything inside the row element <tr>…​</tr> is created for each item in the list.

The value of the items attribute declares the items to loop. The item property will be set on each instance’s binding scope so that sub-properties binding should be used.

Note
Refer to dom-repeat element documentation for details.
Note
Not all tags do allow dom-repeat elements inside. In this cases the alternative <template is=”dom-repeat”> has to be used.

Populating the List of Items

You should declare a method in the template’s model interface for setting the list of beans that should be shown. The name of the method should match the name in the dom-repeat definition; data for [[employees]] is set by a method named setEmployees.

Java

  1. public class EmployeesTable extends PolymerTemplate<EmployeesModel> {
  2. public interface EmployeesModel extends TemplateModel {
  3. @Include({ "name", "title", "email" })
  4. void setEmployees(List<Employee> employees);
  5. List<Employee> getEmployees();
  6. }
  7. public void setEmployees(List<Employee> employees) {
  8. getModel().setEmployees(employees);
  9. }
  10. public List<Employee> getEmployees() {
  11. return getModel().getEmployees();
  12. }
  13. }
Note
The @Include annotation is used here to limit which properties to import into the model. This is done to exclude the id property whose type is unsupported. Alternatively you can use a @Exclude(“id”) in this specific case.

The Employee bean should have getters corresponding the properties used inside the dom-repeat definition in the template, e.g. getName() for employee.name.

Java

  1. public class Employee {
  2. private String name;
  3. private String title;
  4. private String email;
  5. private long id;
  6. public Employee(String name, String title, String email, long id) {
  7. this.name = name;
  8. this.title = title;
  9. this.email = email;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public String getTitle() {
  15. return title;
  16. }
  17. public String getEmail() {
  18. return email;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public void setTitle(String title) {
  24. this.title = title;
  25. }
  26. public void setEmail(String email) {
  27. this.email = email;
  28. }
  29. public long getId() {
  30. return id;
  31. }
  32. public void setId(long id) {
  33. this.id = id;
  34. }
  35. }
Note
Setters are not required here. The template engine will use only the getter to fetch values from employee beans.

The list property updates are propagated only from the server side to the client side. The two-way data binding doesn’t work with the list property. It means that client side changes in the list property are not sent to the server.

In the example below the update in the messages property won’t be sent to the server side if method addItem is called.

JavaScript

  1. class MyTemplate extends Polymer.Element {
  2. static get properties() {
  3. return {
  4. messages: {
  5. type: Array,
  6. value: [],
  7. notify: true
  8. }
  9. };
  10. }
  11. addItem() {
  12. this.push('messages', 'foo');
  13. }
  14. }

Updating the Items

The beans that you add to the model using the setEmployees() method are used to populate the model only. It means that any update made to the bean will not update the model. To be able to update the model items you should use getEmployees() which returns bean proxies which are connected to the model. Any change made to the proxy instance will be reflected to the model.

Here is the way to update the title for all items:

Java

  1. public void updateTitle() {
  2. getEmployees().forEach(employee -> employee.setTitle("Mr."));
  3. }
Note
You can also use setEmployees() method with a new list of updated beans to repopulate the model. This is not very convenient if you want to update only a single item or a single property.

Accessing item indices

As you may have noticed, there is an event handler in the demo. If you’re unfamiliar with event handlers, please reference the tutorial: Handling User Events in a Template

This event handler is used to demonstrate a shorthand that allows us to access current item index, by annotating the event handler with @RepeatIndex annotation:

Java

  1. @EventHandler
  2. public void processElement(@RepeatIndex int itemIndex) {
  3. System.out.println(getEmployees().get(itemIndex).getName());
  4. }
Note
There is a limitation: parameter type should be either int or Integer.