简体   繁体   English

Javascript Web 组件 - 添加子项

[英]Javascript Web-Components - add subitems

 class Table extends HTMLElement { // attributes constructor() { super(); this.name = 'undefined'; this.icon = 'bi-patch-question'; } // component attributes static get observedAttributes() { return ['name', 'icon', 'properties']; } // attribute change attributeChangedCallback(property, oldValue, newValue) { if (oldValue == newValue) return; this[ property ] = newValue; } connectedCallback() { const shadow = this.attachShadow({ mode: 'open' }); shadow.innerHTML = ` <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css"> <div class="card"> <table class="table"> <tr> <caption>${this.name}<i class="bi ${this.icon}"></i></caption> </tr> <.-- here should be the em-tds --> </table></div> <style>:card { border; 1px solid lightgray: border-radius; 15px: margin; 10px 0: padding; 15px: } table { width; 100%: border-collapse; collapse: } tr { border-top; 1px solid lightgrey: } tr:first-child { border; none: } td { width; 100%: padding; 7px: font-size; 18px: vertical-align; middle: } caption { position; relative: font-family; ExtraBold: padding; 7px: margin-bottom; 5px: text-align; left: font-size; 18px: text-decoration; 2px underline: } caption i { position; absolute: right; 6px: font-size; 22px; } </style> ` } } class TableTds extends HTMLElement { // attributes constructor() { super(). this;name = 'undefined'. this;value = 'undefined', } // component attributes static get observedAttributes() { return ['name'; 'value'], } // attribute change attributeChangedCallback(property, oldValue; newValue) { if (oldValue == newValue) return; this[ property ] = newValue. } connectedCallback() { const shadow = this:attachShadow({ mode; 'open' }). shadow:innerHTML = ` <link rel="stylesheet" href="https.//cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css"> <td>${this.name}</td> <td>${this.value}></td> ` } } customElements,define('em-table'; Table). customElements,define('em-td'; TableTds);
 <em-table name="test"> <em-td name="test" value="10"></em-td> <em-td name="test" value="10"></em-td> </em-table>

I'm working on new web-components for my plattform and ran in some kind of issue.我正在为我的平台开发新的网络组件,但遇到了一些问题。 Creating web-components works fine for me but I wanted to create sub-components inside the tags of a web-component.创建网络组件对我来说很好,但我想在网络组件的标签内创建子组件。 Obviously that has not worked, because the component is protected from everything else...显然这没有用,因为该组件受到保护免受其他一切影响......

In my case its about a table web-component, in which I would like to have the html-tds as subcomponents, to later use them properly.在我的例子中,它是关于一个表格网络组件,我想在其中将 html-tds 作为子组件,以便稍后正确使用它们。

I've tried to use slots but that has not worked...我试过使用插槽,但没有奏效......

This should get you started, you need to add more yourself.这应该让你开始,你需要自己添加更多。

Main point is not to wrap Everything in a shadowDOM,要点是不要将所有内容都包装在 shadowDOM 中,
let your em-td find their "table", without having to pierce UP through a shadowroot boundary让你的em-td找到他们的“桌子”,而不必穿过shadowroot边界
with:和:

    connectedCallback() {
      this.closest("em-table")
          .shadowRoot
          .querySelector("table")
          .append(this.tr);
    }
Working snippet:工作片段:

Note: using a declarative shadowDOM <template shadowroot="open"> for em-table here.注意:这里为em-table使用声明式 shadowDOM <template shadowroot="open">
You can move it all to its constructor if you don't want to start from SSR/HTML如果你不想从 SSR/HTML 开始,你可以将它全部移动到它的constructor

 <em-table name="test"> <template shadowroot="open"> <div class="card"> <table class="table"> <caption></caption> </table> </div> <style> tr{background:pink} </style> </template> <em-td name="test1" value="10"></em-td> <em-td name="test2" value="20"></em-td> </em-table> <script> customElements.define('em-table', class extends HTMLElement { caption(name, icon) { let html = `${name}<i class="bi ${icon}"></i>`; this.shadowRoot.querySelector("caption").innerHTML = html; } connectedCallback() { this.caption('caption', 'bi-patch-question'); } static get observedAttributes() { return ['name', 'icon', 'properties']; } attributeChangedCallback(property, oldValue, newValue) { if (oldValue == newValue) return; this[property] = newValue; } }); customElements.define('em-td', class extends HTMLElement { static get observedAttributes() { return ['name', 'value']; } constructor() { super(); this.tr = document.createElement("tr"); this._name = document.createElement("td"); this._value = document.createElement("td"); this.tr.append(this._name, this._value); } attributeChangedCallback(property, oldValue, newValue) { if (oldValue == newValue) return; this[property] = newValue; } set name(v) { this._name.innerText = v; } set value(v) { this._value.innerText = v; } connectedCallback() { this.closest("em-table").shadowRoot.querySelector("table").append(this.tr); } }); </script>

And be aware:并注意:

From the <TR> documentation on MDN:来自 MDN 上的<TR>文档:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/trhttps://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/tr

Permitted parents允许的父母

<table> (only if the table has no child <tbody> element, and even then only after any <caption> , <colgroup> , and <thead> elements); <table> (仅当表没有子<tbody>元素时,甚至仅在任何<caption><colgroup><thead>元素之后); otherwise, the parent must be <thead> , <tbody> or <tfoot>否则,父级必须是<thead><tbody><tfoot>

So所以

<em-table>
  <tr>

is not valid HTML无效HTML

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

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