2015-02-11 72 views
0

此事件不火克隆錶行單元格的部件是一個後續問題Error | Removing disallowed attribute on SelectElement的變化當事件改變

我創建了一個表,如圖下圖:

Table with cloned rows

電流變化事件附加到選擇下拉菜單。對於第一行,按預期方式觸發事件並顯示下拉內容更改。但是,通過克隆添加新行時,事件偵聽器不會觸發。我需要一些幫助,以便如何最好地讓附加事件觸發。

的.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']; 
    } 
} 

感謝

回答

1

克隆只是不復制事件處理程序(例如,見https://groups.google.com/a/dartlang.org/forum/#!topic/misc/-z_8sVp_uPY

您可以使用<template repeat>來生成上一個問題中提到的行和單元格。另一種方法是使用injectBoundHtmlhttps://www.polymer-project.org/docs/polymer/databinding-advanced.html#boundhtml)。

With injectBoundHtml您不能從DOM(innerHtml或類似的)讀取HTML,因爲這裏得到的內容已經與Polymer解釋的綁定關聯。當您將DART代碼中的HTML存儲爲字符串時,您可以多次添加它,並且每次插入時都會對其進行解釋。

如果您希望在HTML文件中的內容,你可以使用一個模板,而不是

injectBoundHtml((this.querySelector('#my_template') as TemplateElement) 
    .innerHtml); 

(實際上沒有試過)