动态添加铁列表及其所有数据

时间:2017-02-26 09:20:43

标签: polymer dart

我遇到了一个无法解决的问题。我需要从代码中添加<iron-list>元素,分配其项目等等。

我已经用Google搜索了几天,但在所有示例中,我发现HTML中已添加<iron-list>

我试过这个:

var msgs = new List();//I've added some data to this list 
var listTab = new Element.div();
var list = new Element.tag("iron-list");
var template = new Element.tag("template");
var item = new Element.tag("paper-item");
item.innerHtml = "[[item.text]]";
template.children.add(item);
list.children.add(template);
list.items = msgs;
list.as = "item";
listTab.children.add(list);

这会导致以下错误:

iron-list requires a template to be provided in light-dom
Uncaught Unhandled exception:
TypeError: this.ctor is not a function

以下代码导致相同的错误:

var msgs = new List();//I've added some data to this list 
var listTab = new Element.div();
IronList list = new IronList();
var template = new Element.tag("template");
var item = new Element.tag("paper-item");
item.innerHtml = "[[item.text]]";
template.children.add(item);
list.children.add(template);
list.items = msgs;
list.as = "item";
listTab.children.add(list);

最后,我尝试了一个非常直接的解决方案,但也没有做到:

var list= new Element.html('<iron-list items="{{msgs}}" as="item"><template><paper-item>[[item.text]]</paper-item></template></iron-list>');
listTab.children.add(list);

错误:

Polymer::Attributes: couldn`t decode Array as JSON
Removing disallowed element <IRON-LIST> from [object DocumentFragment]
Removing disallowed element <PAPER-ITEM> from [object DocumentFragment]
Removing disallowed element <ARRAY-SELECTOR> from iron-list
Uncaught Unhandled exception:
Bad state: No element

我真的很感激从dart代码添加<iron-list>元素的工作解决方案。

1 个答案:

答案 0 :(得分:2)

var validator = new NodeValidatorBuilder.common()
  ..allowElement('iron-list', attributes: ['items', 'as'])
  ..allowElement(...);

var list= new Element.html('<iron-list items="{{msgs}}" as="item"><template><paper-item>[[item.text]]</paper-item></template></iron-list>',
    validator: validator);
listTab.children.add(list);

var list= new Element.html('<iron-list items="{{msgs}}" as="item"><template><paper-item>[[item.text]]</paper-item></template></iron-list>',
    treeSanitizer: NodeTreeSanitizer.trusted);
listTab.children.add(list);