这是Error | Removing disallowed attribute on SelectElement
的后续问题我创建了一个表,如下图所示:
on-change事件附加到选择下拉列表。对于第一行,事件按预期触发,下拉内容更改。但是,通过克隆添加新行时,事件侦听器不会触发。我需要一些帮助才能最好地解决附加事件。
html的
<link rel='import' href='../../../../packages/polymer/polymer.html' >
<link rel='import' href='../../../../packages/paper_elements/paper_checkbox.html' >
<link rel='import' href='../../../../packages/paper_elements/paper_button.html' >
<link rel='import' href='../../../../packages/core_elements/core_collapse.html' >
<link rel='import' href='../../../../packages/core_elements/communication_icons.html' >
<polymer-element name='phone-form'>
<template>
<table id='table-1'>
<thead>
<tr>
<th width='20px'></th>
<th width='67px'>Type</th>
<th width='130px'>Provider</th>
</tr>
</thead>
</table>
<table id='table'>
<tbody>
<tr>
<td><input name='chk' type='checkbox'></td>
<td>
<select id='phone-type'
selectedIndex='{{typeSelected}}'
on-change='{{onChangeTypeFired}}'>
<option template repeat='{{key in types.keys}}'
value='{{types[key]}}'>{{types[key]}}
</option>
</select>
</td>
<td>
<select id='phone-provider'
selectedIndex='{{providerSelected}}'
on-change='{{onChangeProviderFired}}'>
<option template repeat='{{key in providers.keys}}'
value='{{providers[key]}}'>{{providers[key]}}
</option>
</select>
</td>
</tr>
</tbody>
</table>
<div>
<paper-button raised id='add-row-btn' class='margin-8px'
on-click='{{addRow}}'>
Add Row
<core-icon id='add-row-btn-icon' icon=''></core-icon>
</paper-button>
<paper-button raised id='delete-row-btn' class='margin-8px'
on-click='{{deleteRow}}'>
Delete Row
<core-icon id='delete-row-btn-icon' icon='fa:minus-circle'></core-icon>
</paper-button>
</div>
</template>
<script type='application/dart' src='phone_form.dart'></script>
</polymer-element>
.dart
import 'package:polymer/polymer.dart';
import 'dart:html' as dom;
//import 'package:paper_elements/paper_toggle_button.dart' show PaperToggleButton;
import 'package:paper_elements/paper_button.dart' show PaperButton;
//import 'package:epimss_shared/epimss_shared.dart';
//import 'package:epimss_shared/epimss_shared_client.dart' hide DataEvent;
@CustomTag('phone-form')
class PhoneForm extends PolymerElement {
@observable String icon = '';
@observable String errorMsg;
String topic;
PaperButton addBtn;
@observable int typeSelected = 0;
@observable int providerSelected = 0;
@observable int relationsSelected = 0;
Map<String, String> types = const <String, String> {
'': '',
'Car': 'Car',
'Direct': 'Direct',
'Fax': 'Fax',
};
Map<String, String> providers = const <String, String> {
'': '',
'AT & T': 'AT & T',
'Cable & Wireless': 'Cable & Wireless',
};
PhoneForm.created() : super.created();
void onSelectTypeFired()
{
}
void onChangeTypeFired( dom.Event e, var detail, dom.SelectElement target)
{
print(target.value);
}
void onChangeProviderFired( dom.Event e, var detail, dom.SelectElement target)
{
print(target.value);
}
void onChangeRelationsFired( dom.Event e, var detail, dom.SelectElement target)
{
print(target.value);
}
void addRow()
{
var table = $['table'];
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i = 0; i < colCount; i++ )
{
var newcell = row.insertCell(i);
newcell.children.clear();
newcell.children.addAll(table.rows[0].cells[i].children.map((c) =>
c.clone(true)));
switch(newcell.childNodes[0].runtimeType.toString())
{
case 'text':
newcell.childNodes[0].value = '';
break;
case 'checkbox':
newcell.childNodes[0].checked = false;
break;
case 'select':
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
void deleteRow()
{
var rowsToDelete = [];
try{
var table = $['table'];
var rowCount = table.rows.length;
for(var i = 0; i < rowCount; i++)
{
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(chkbox != null && chkbox.checked)
{
if(rowCount <= 1)
{
print('Cannot delete all the rows.');
break;
}
else
{
rowsToDelete.add(i);
}
}
}
rowsToDelete.forEach( (row)
{
table.deleteRow(row);
});
}
catch(e)
{ print(e); }
}
@override
void attached() {
super.attached();
topic = this.dataset['topic'];
addBtn = $['add-btn'];
}
}
由于
答案 0 :(得分:1)
克隆只是不复制事件处理程序(参见例如https://groups.google.com/a/dartlang.org/forum/#!topic/misc/-z_8sVp_uPY)
您可以使用<template repeat>
生成上一个问题中提到的行和单元格。另一种方法是使用injectBoundHtml
(https://www.polymer-project.org/docs/polymer/databinding-advanced.html#boundhtml)。
使用injectBoundHtml
,您无法从DOM(innerHtml或类似内容)中读取HTML,因为您在此处获得的内容已经是Polymer解释的绑定。当您将HTML存储在Dart代码中的字符串中时,您可以多次添加它,并且每次插入时都会对其进行解释。
如果您希望在HTML文件中包含内容,则可以使用模板
injectBoundHtml((this.querySelector('#my_template') as TemplateElement)
.innerHtml);
(尚未尝试过)