Search code examples
javascripthtmldompolymerpolymer-2.x

Polymer DOM-repeat content depending on received data


I have made a DOM-repeat that recieves multiple types of data.
Now I want the DOM-repeat to only show the items related to that type.
Is it possible to do this and how would I be able to achieve this?
When the item.Type is Zone I want it to display the item.Name and item.ZoneID
when the item.Type is Building I want it to display the item.BuildingID, item.Address1 and item.Address2

This is my code:

 <template is="dom-repeat" items="[[data]]">
          <paper-card id="departmentspaperContainer2">
            <div class="card-content">
              <h3>Type: {{item.Type}}</h3>
              <hr/>
              <h4>Name:       {{item.Name}}</h4>
              <h4>BuildingID: {{item.BuildingID}}</h4>
              <h4>ZoneID:     {{item.ZoneID}}</h4>
              <h4>Address1:   {{item.Address1}}</h4>
              <h4>Address2:   {{item.Address2}}</h4>
            </div>
          </paper-card>
        </template>

Solution

  • Something similar to what 'HakanC' is proposing, but differently, you can use the dom-if and bind the response of your filter function :

    Here is a working jsfiddle (to use in chrome)

    <dom-module id="os-test">
        <template>
            <template is="dom-repeat" items="{{data}}">
                <paper-card id="departmentspaperContainer2">
                    <div class="cardContent" type="{{item.Type}}">
                      <h3>Type: [[item.Type]]</h3>
                      <hr/>
                      <template is='dom-if' if='{{showZone(item.Type)}}'>
                         <h4>Name: [[item.Name]]</h4>
                         <h4>ZoneID: [[item.ZoneID]]</h4>
                      </template>
                      <template is='dom-if' if='{{showBuilding(item.Type)}}'>
                         <h4>BuildingID:[[item.BuildingID]]</h4>
                         <h4>Address1: [[item.Address1]]</h4>
                         <h4>Address2: [[item.Address2]]</h4>
                      </template>
                    </div>
                </paper-card>
            </template>
        </template>
    </dom-module>
    <script>
      class OsTestElement extends Polymer.Element {
        static get is() {
          return 'os-test';
        }
    
        static get properties() {
          return {
            data: {
              type: Array,
              value: () => {
                return [
                    {
                    Type:"Zone",
                    Name:"valueName1",
                    BuildingID:"valueBuildingID1",
                    ZoneID:"valueZoneID1",
                    Address1:"valueAddress11",
                    Address2:"valueAddress21",
                  },
                  {
                    Type:"Zone",
                    Name:"valueName2",
                    BuildingID:"valueBuildingID2",
                    ZoneID:"valueZoneID2",
                    Address1:"valueAddress12",
                    Address2:"valueAddress22",
                  },
                  {
                    Type:"Building",
                    Name:"valueName3",
                    BuildingID:"valueBuildingID3",
                    ZoneID:"valueZoneID3",
                    Address1:"valueAddress13",
                    Address2:"valueAddress23",
                  },
                  {
                    Type:"Building",
                    Name:"valueName4",
                    BuildingID:"valueBuildingID4",
                    ZoneID:"valueZoneID4",
                    Address1:"valueAddress14",
                    Address2:"valueAddress24",
                  }
                ]
              }
            }
          }
        }
    
        showZone(item) {
            return (item==="Zone");
        }
    
        showBuilding(item) {
            return (item==="Building");
        }
      }
      window.customElements.define(OsTestElement.is, OsTestElement);
    </script>