Linking data between paper elements

Is it possible to bind the state (attribute) of a checkbox for paper [checked \ unchecked] dynamically with an attribute such as [readonly | disabled] inside an I / O element? This is my implementation so far:

<template repeat="{{item in lphasen}}">
  <div center horizontal layout>
    <paper-checkbox unchecked on-change="{{checkStateChanged}}" id="{{item.index}}"></paper-checkbox>
      <div style="margin-left: 24px;" flex>
        <h4>{{item.name}}</h4>
      </div>
      <div class="container"><paper-input disabled floatingLabel id="{{item.index}}" label="LABEL2" value="{{item.percent}}" style="width: 120px;"></paper-input></div>
  </div>
</template>

      

The behavior should be as follows: When the user unchecks the checkbox for paper, then the paper input element on the same line should be disabled and / or read-only, and vice versa. Is it possible to directly link multiple double-mustache elements or do I need to loop over the DOM somehow to manually set an attribute on a paper input element? If YES, can someone please explain how?

+3


source to share


2 answers


Another way to bind the checked state of the flag-paper.

<polymer-element name="check-input">
<template>
    <style>
    #checkbox {
      margin-left: 1em;
    }
    </style>
    <div center horizontal layout>
        <div><paper-input floatingLabel label="{{xlabel}}" value="{{xvalue}}" disabled="{{!xenable}}" type="number" min="15" max="200"></paper-input></div>
        <div><paper-checkbox id="checkbox" label="Enable" checked="{{xenable}}"></paper-checkbox></div>
    </div>
</template>
<script>
Polymer('check-input', {
  publish:{xenable:true, xvalue:'',xlabel:''}
});
</script>
</polymer-element>
    <div>
       <check-input xenable="true" xvalue="100" xlabel="Weight.1"></check-input>
       <check-input xenable="false" xvalue="185" xlabel="Weight.2"></check-input>
   </div>

      



jsbin demo http://jsbin.com/boxow/

+5


source


My preferred approach would be to refactor the code to create a Polymer element responsible for one element. This way, all the behavior of a particular object is encapsulated in one place.

Once this is done, there are several ways to do it.

The simplest would be to simply create a responder event for the checkbox that toggles the property value and sets the disabled attribute accordingly.

<paper-checkbox unchecked on-tap="{{checkChanged}}"></paper-checkbox>
//Other markup for item name display
<paper-input disabled floatingLabel id="contextRelevantName" style="width:120 px;"></paper-input>

      

One of the benefits of bringing this into your own polymer element is that you no longer need to worry about the unique ID. The control id is obfuscated by the shadowDOM.

For scripting, you would do something like this:



publish: {
  disabled: {
    value: true,
    reflect: false
  }
}

checkChanged: function() {
  this.$.disabled= !this.$.disabled;
  this.$.contextRelevantName.disabled = this.$.disabled;
}

      

I haven't tested this, so there might be some syntax changes and what you have, but that should get you most of the way.

Edit

Based on the example code in your comment below, I modified your code to make it work. The key is to make 1 element containing one line, rather than multiple elements containing only parts of a whole. so the code below has been removed a bit to only enable this checkbox and the input it should disable. You can easily add an element to other elements of the displayed element.

<polymer-element name="aw-leistungsphase" layout vertical attributes="label checked     defVal contractedVal">
<template>
<div center horizontal layout>
    <div>
    <paper-checkbox checked on-tap="{{checkChanged}}" id="checkbox" label="{{label}}"></paper-checkbox>
    </div>

      <div class="container"><paper-input floatingLabel id="contractedInput" label="Enter Value" value="" style="width: 120px;"></paper-input></div>
  </div>
</template>
<script>
  Polymer('aw-leistungsphase', {

  publish: {
    /**
     * The label for this input. It normally appears as grey text inside
     * the text input and disappears once the user enters text.
     *
     * @attribute label
     * @type string
     * @default ''
     */
    label: '',
    defVal : 0,
    contractedVal : 0
  },

  ready: function() {
    // Binding the project to the data-fields
    this.prj = au.app.prj;
    // i18n mappings
    this.i18ndefLPHLabel = au.xlate.xlate("hb.defLPHLabel");
    this.i18ncontractedLPHLabel = au.xlate.xlate("hb.contractedLPHLabel");
  },

  observe : {
    'contractedVal' : 'changedLPH'
  },

  changedLPH: function(oldVal, newVal) {
    if (oldVal !== newVal) {
      //this.prj.hb.honlbl = newVal;
      console.log("Geänderter Wert: " + newVal);
    }
  },
  checkChanged: function(e, detail, sender) {

    console.log(sender.label + " " + sender.checked);
    if (!this.$.checkbox.checked) {
      this.$.contractedInput.disabled = 'disabled';
    }
    else {
      this.$.contractedInput.disabled = '';
    }

    console.log("Input field disabled: " + this.$.contractedInput.disabled);
  }
});
</script>
</polymer-element>

      

0


source







All Articles