我想使用reset()
元素的<iron-form>
方法重置表单按钮。
所需的行为是,在点击重置按钮后,原来unchecked
为unchecked
的两个按钮和最初checked
的两个按钮成为checked
。
实际行为是,点击重置按钮后,所有按钮都会翻到unchecked
。
Here is my Plunker demo.请注意,实际上我正在从Firebase加载数据。在演示中,我从myjson.com加载它。
checked
按钮和两个unchecked
。unchecked
按钮。unchecked
。所需的行为适用于最初unchecked
为unchecked
的两个按钮以及原来checked
为checked
的两个按钮。
<base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
<link rel="import" href="polymer/polymer-element.html">
<link rel="import" href="paper-toggle-button/paper-toggle-button.html">
<link rel="import" href="iron-form/iron-form.html">
<link rel="import" href="iron-ajax/iron-ajax.html">
<link rel="import" href="paper-button/paper-button.html">
<dom-module id="my-demo">
<template>
<style>
paper-button, form > * {
margin-top: 40px;
font-size: 18px;
}
</style>
<iron-ajax
id="ajax"
auto
url="https://api.myjson.com/bins/1dntup"
last-response="{{item}}"
handle-as="json"
>
</iron-ajax>
<iron-form id="form">
<form>
<paper-toggle-button name="alice" checked="{{item.alice}}">Alice</paper-toggle-button>
<paper-toggle-button name="bob" checked="{{item.bob}}">Bob</paper-toggle-button>
<paper-toggle-button name="charlie" checked="{{item.charlie}}">Charlie</paper-toggle-button>
<paper-toggle-button name="dave" checked="{{item.dave}}">Dave</paper-toggle-button>
</form>
</iron-form>
<paper-button on-tap="_onTap">Reset</paper-button>
</template>
<script>
class MyDemo extends Polymer.Element {
static get is() {
return 'my-demo';
}
static get properties() {
return {
item: {
type: Object,
notify: true,
},
};
}
ready() {
super.ready();
}
_onTap() {
this.$.form.reset();
}
}
window.customElements.define(MyDemo.is, MyDemo);
</script>
</dom-module>
Polymer Slack Site的用户@madietov指出the source code here使用名为_init()
的受保护方法来初始化默认设置。在_init()
元素返回其值之前,似乎会调用此<iron-ajax>
方法。
_init()
设置名为_defaults
的受保护属性。如果我可以访问受保护的方法或受保护的属性,那么我可以在<iron-ajax>
返回其初始值之后调用该方法或设置属性。
答案 0 :(得分:0)
替换
this.$.form.reset();
与
this.$.ajax.generateRequest();
https://plnkr.co/edit/4ROGJbzqk98X6FtbVSCN?p=preview
<base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
<link rel="import" href="polymer/polymer-element.html">
<link rel="import" href="paper-toggle-button/paper-toggle-button.html">
<link rel="import" href="iron-form/iron-form.html">
<link rel="import" href="iron-ajax/iron-ajax.html">
<link rel="import" href="paper-button/paper-button.html">
<dom-module id="my-demo">
<template>
<style>
paper-button, form > * {
margin-top: 40px;
font-size: 18px;
}
</style>
<iron-ajax
id="ajax"
auto
url="https://api.myjson.com/bins/1dntup"
last-response="{{item}}"
handle-as="json"
>
</iron-ajax>
<iron-form id="form">
<form>
<paper-toggle-button name="alice" checked="{{item.alice}}">Alice</paper-toggle-button>
<paper-toggle-button name="bob" checked="{{item.bob}}">Bob</paper-toggle-button>
<paper-toggle-button name="charlie" checked="{{item.charlie}}">Charlie</paper-toggle-button>
<paper-toggle-button name="dave" checked="{{item.dave}}">Dave</paper-toggle-button>
</form>
</iron-form>
<paper-button on-tap="_onTap">Reset</paper-button>
</template>
<script>
class MyDemo extends Polymer.Element {
static get is() {
return 'my-demo';
}
static get properties() {
return {
item: {
type: Object,
notify: true,
},
};
}
ready() {
super.ready();
}
_onTap() {
//this.$.form.reset();
this.$.ajax.generateRequest();
}
}
window.customElements.define(MyDemo.is, MyDemo);
</script>
</dom-module>
信用
评论摘要。
帽子提示:@AneesHameed