javascript - Creating a custom table row -
i trying create custom table row having difficulty getting behave properly. i've tried 2 below methods , give bizarre results. realize easy to without custom elements small example of larger project. can change achieve desired result?
class customtablerow extends htmlelement { constructor(){ super(); var shadow = this.attachshadow({mode: 'open'}); this.tablerow = document.createelement('tr'); var td = document.createelement('td'); td.innertext = "rowtitle"; this.tablerow.appendchild(td); var td2 = document.createelement('td'); td2.innertext = "rowcontent"; td2.colspan = 4; this.tablerow.appendchild(td2); shadow.appendchild(this.tablerow); } } customelements.define('custom-tr', customtablerow); //attempt 2 var newtr = new customtablerow; document.getelementbyid('table2body').appendchild(newtr); td { border: 1px solid black; } <span>attempt 1:</span> <table> <thead> <tr> <th>one</th> <th>two</th> <th>three</th> <th>four</th> <th>five</th> </tr> </thead> <tbody> <custom-tr /> </tbody> </table> <hr> <span>attempt 2:</span> <table id="table2"> <thead> <tr> <th>one</th> <th>two</th> <th>three</th> <th>four</th> <th>five</th> </tr> </thead> <tbody id="table2body"> <!-- should append here --> </tbody> </table> <hr> <span>this how want look:</span> <table id="table2"> <thead> <tr> <th>one</th> <th>two</th> <th>three</th> <th>four</th> <th>five</th> </tr> </thead> <tbody> <tr> <td>row title</td> <td colspan="4">row content</td> </tbody> </table>
a <table> element , subcomponents <tbody>, <tr> require specific syntax. example, <tr> elements authorized children of <tbody>.
therefore cannot define element , insert in <tbody> or <table>. if moved outide of <table> @ parsing. hence display of first example (look code in dev tools).
instead should define customized tag instead in this answer similar question.
or should redefine complete custom table structure <custom-table>, <custom-tbody>... in this other answer.
also, should use closing tag <custom-tr></custom-tr>, , insert css rule in shadow dom if want applied inside it.
Comments
Post a Comment