Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to Display message if the array is empty in Polymerjs

Can someone please help how to make this template show if the filter is not found in the array.

<template is="dom-if" if="{{itemsEmpty}}">
     The array is empty!
</template> 

here is my entire code. but for some reasons the if condition in the dom-if template is not working

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src='bower_components/webcomponentsjs/webcomponents-lite.min.js'></script>

 <link rel="import" href="bower_components/polymer/polymer.html">
 <link rel="import" href="bower_components/iron-image/iron-image.html">
 <!--<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">-->
 <link rel="import" href="bower_components/paper-item/paper-item.html">
 <link rel="import" href="bower_components/paper-input/paper-input.html">
 <link rel="import" href="bower_components/paper-dropdown-menu/paper-dropdown-menu.html">
 <link rel="import" href="bower_components/paper-listbox/paper-listbox.html">
 <link rel="import" href="bower_components/paper-button/paper-button.html">
<style>
.taller{
  height:120px;
}
[vertical-align="top"] ul {
  margin-top: 0;
}
[vertical-align="bottom"] ul {
  margin-bottom: 0;
}
button, paper-button {
  border: 1px solid #ccc;
  background-color: #eee;
  /*padding: 1em;*/
  border-radius: 3px;
  cursor: pointer;
}
button:focus {
  outline: none;
  border-color: blue;
}
</style>
</head>
<body>
     <dom-module id="employee-list">
         <template>

             <paper-input value="{{filterValue}}" label="Search For a   Company" floatingLabel id="searchCompany"></paper-input>
             <paper-dropdown-menu label="Select Project Type">
                  <paper-listbox class="dropdown-content" >
                         <template is="dom-repeat" items="{{items}}" as="test" filter="{{Filter(filterValue:input)}}">
                              <paper-item value="{{test.fname}}">{{test.fname}} - {{test.lname}}</paper-item>

         </template>
     </paper-listbox>
     </paper-dropdown-menu>
     <paper-listbox >
    <template is="dom-repeat" items="{{items}}" filter="{{Filter(filterValue)}}">
        <div class="row">
            <div class="col-sm-12" style="font-size:15px;font-family:'Open Sans'">
                {{item.fname}} - {{item.lname}}
            </div>
            <hr />
            <template is="dom-if" if="{{itemsEmpty}}">
              The array is empty!
          </template>    
        </div>

    </template>
</paper-listbox>
</template>
<script>
       Polymer({
       is: 'employee-list',
       properties: {
       items: {
          type: Array,
          observer: '_itemsChanged'
       },
       filterValue: {
          type: String,
          notify:true
        },
       itemsEmpty: {
          type: Boolean
        }
      },
      ready: function() {
           this.items = [{'fname': 'Jack', 'lname':'Bayo'}, {'fname': 'Skellington','lname':'Dar' }];
      },
     _itemsChanged: function(items){
            this.itemsEmpty = items.length == 0;
      },
     Filter: function (val) {
         return function (items) {
            if (!items) return false;
            if (val != null || val != undefined) {
                return (items.fname && ~items.fname.toLowerCase().indexOf(val.toLowerCase())) ||
            (items.lname && ~items.lname.toLowerCase().indexOf(val.toLowerCase()));
        }
        else
            return true;
    };
  }
});
</script>
</dom-module>
<employee-list></employee-list>
</body>
</html>

I will really appreciate any help here. Here is the plunker: http://plnkr.co/edit/Qy6LeAfe93u4CK2G43eX?p=preview

Thank you

like image 720
Builda Avatar asked May 16 '16 22:05

Builda


1 Answers

I've found a number of problems with your sample:

  1. The polymer.html import breaks your plunk (see all the errors from registerElement). That's because the other imports try to import Polymer from different URLs
  2. The dom-if is inside the dom-repeat
  3. Observing items won't work because the items property isn't changed when you use filter.

What does change is renderedItemCount property, which you can observe and use to control the dom-if. The property is updated whenever filter fires or items array changes.

To sum up:

  1. Remove Polymer import
  2. Move dom-if outside repeater
  3. Add binding to dom-repeat: rendered-item-count="{{renderedCount}}"
  4. Change the if property to use the actually rendered item count: if="{{!renderedCount}}"

Here's how the element's template can look:

<paper-input value="{{filterValue}}" label="Search For a Company" floatingLabel id="searchCompany"></paper-input>

<paper-listbox >
  <template is="dom-repeat" items="{{items}}" filter="{{Filter(filterValue)}}" rendered-item-count="{{renderedCount}}">
    <div class="row">
      <div class="col-sm-12" style="font-size:15px;font-family:'Open Sans'">
        {{item.fname}} - {{item.lname}}
      </div>
      <hr />   
    </div>               
  </template>

  <template is="dom-if" if="{{!renderedCount}}">
    The array is empty!
  </template> 
</paper-listbox>
like image 176
Tomasz Pluskiewicz Avatar answered Jan 04 '23 00:01

Tomasz Pluskiewicz