How to bind paper-toggle-button to a Boolean in Po

2019-04-16 00:31发布

问题:

I'm triyng to bind a paper-toggle-button to a boolean but it's not working. I already tried binding with $= and evaluating with ?=. I want to show if the user isActive.

<template is="dom-repeat" items={{users}}>
 <button class="heading" aria-expanded$="[[isExpanded(opened{{index}})]]" aria-controls="collapse{{index}}" onclick="toggle('#collapse{{index}}')" >{{item.firstName}} {{item.lastName}}<paper-toggle-button style="float: right;" checked$="{{item.isActive}}">Activo</paper-toggle-button></button>
  <iron-collapse id="collapse{{index}}" tabindex="0" opened?="{{opened{{index}}}}">
    <div class="content horizontal layout">
        <div class="flex-1">
          <paper-item>
            <paper-item-body two-line>
              <div>Email</div>
              <div secondary>{{item.email}}</div>
            </paper-item-body>
          </paper-item>
          <paper-item>
            <paper-item-body two-line>
              <div>Teléfono</div>
              <div secondary>{{item.phone}}</div>
            </paper-item-body>
          </paper-item>
          <paper-item>
            <paper-item-body two-line>
              <div>Género</div>
              <div secondary>{{item.gender}}</div>
            </paper-item-body>
          </paper-item>
        </div>
      </div>
    </div>
  </iron-collapse>
</template>

JS:

ready: function() {
   this.users = [
     {firstName: 'Pedro', lastName: 'Vargas', email: 'pedro@mail.com', phone:'(222) 212 12 12', gender: 'Masculino', isActive:'true'},
     {firstName: 'Andrea', lastName: 'Vargas', email: 'andrea@mail.com', phone:'(222) 212 12 13', gender: 'Femenino', isActive:'false'},
     {firstName: 'Juan', lastName: 'Martínez', email: 'juan@mail.com', phone:'(222) 212 12 12', gender: 'Masculino', isActive:'true'},
   ];}
function toggle(selector) {
  document.querySelector(selector).toggle();
}
document.querySelector('template[is=dom-repeat]').isExpanded = function(opened) {
  return String(opened);
};

回答1:

The problem is with your array. All of the isActive objects are wrapped in quotes which makes them a string.

You want to make them a boolean so remove the quotes:

{
  firstName: 'Pedro',
  lastName: 'Vargas',
  email: 'pedro@mail.com',
  phone: '(222) 212 12 12',
  gender: 'Masculino',
  isActive: true
}