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

荒凉一梦 提交于 2019-12-06 12:37:24

问题


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
}


来源:https://stackoverflow.com/questions/33761370/how-to-bind-paper-toggle-button-to-a-boolean-in-polymer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!