简体   繁体   English

如何克隆/复制DOM节点的影子Dom?

[英]How do you clone/copy a DOM node's shadow Dom?

I am trying to clone a table row with web API's cloneNode() method. 我正在尝试使用Web API的cloneNode()方法克隆表行。 Inside those table rows are table data with some vaadin web components that use shadow DOM to get and render its data. 在这些表行中是表数据,其中包含一些使用影子DOM来获取和呈现其数据的vaadin Web组件。

When using cloneNode() to do this, the shadow DOM is not cloned/copied, so now I am left with some vaadin combo-boxes that have no output when rendered. 当使用cloneNode()执行此操作时,影子DOM不会被克隆/复制,因此现在剩下一些vaadin组合框,这些组合框在渲染时没有输出。

Is there some way to overcome this? 有什么办法可以克服这个问题?

Example of a table cell that was cloned using cloneNode() : 使用cloneNode()克隆的表单元格的示例:

<td style="text-align:center;">
   <vaadin-combo-box id="xxxlist" 
    value="{{definition.lkp_xxx_unit_id}}"
    item-label-path="value" item-value-path="id">
   </vaadin-combo-box>
</td>

Then after that I have this block of code to actually get the items for the vaadin-combo-box component: 然后,在此之后,我得到了这段代码,以实际获取vaadin-combo-box组件的项目:

ready: function() {
        app.addEventListener('xxx-choices-changed', function(event) {
          this.$.xxxlist.items = app.choices['lkp_xxx_id'];
        }.bind(this));
        this.$.xxxlist.items = app.choices['lkp_xxx_id'];
      }

Any idea how to clone the node with the shadow DOM attached? 任何想法如何克隆附有阴影DOM的节点?

You should not clone the shadow dom contents. 您不应克隆阴影dom内容。 The web component ( <vaadin-combo-box> in this case) is responsible for creating that with JavaScript when a new instance of that element is created. 当创建该元素的新实例时,Web组件(在这种情况下为<vaadin-combo-box> )负责使用JavaScript创建该组件。 So, when you clone a web component and attach it to the DOM, it'll create the shadow DOM by itself. 因此,当您克隆Web组件并将其附加到DOM时,它将自行创建影子DOM。

Your problem here is probably that the items property is not set for the cloned elements. 您的问题可能是未为克隆的元素设置items属性。 Note, that the ready method is only run for the first instance of a Polymer web component, not for all instances. 请注意, ready方法仅在Polymer Web组件的第一个实例上运行,而不是在所有实例上运行。 Use connectedCallback or constructor instead if you want to run some code for every instance. 如果要为每个实例运行一些代码,请改用connectedCallbackconstructor

If you want to clone a node and it´s children you have to tell cloneNode so. 如果要克隆节点及其子节点,则必须告诉cloneNode Please try setting the deep param of cloneNode to true. 请尝试将cloneNode的深色参数设置为true。

element.cloneNode(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM