I want to use a component as a table row template, but can't seem to find a way, is this possible?
When I do the below, it doesn't put the component in the tbody, it puts it above the table.
Component
<table>
<tbody>
<reportingline params='name: "Phil"'></reportingline>
</tbody>
</table>
Template
<tr>
<td data-bind="text: col1"></td>
</tr>
Use the virtual component binding. This approach allows you to use your component without a wrapper element.
<table>
<tbody>
<!-- ko component: { name:"reportingline", params: { name: 'Phil' } } --><!-- /ko -->
</tbody>
</table>
I am afraid that does not look to be possible. From the Polymer FAQ:
Until the addition of HTML
<template>, certain elements like<select>,<table>, and others had special parser rules to prevent anything other than<option>and<tr>from being their children, respectively. Because of these legacy rules, browsers that don’t support<template>will lift unexpected elements out of context and make them siblings [..]
So the only way to treat custom elements as table rows — at least in some modern browsers — would be to use the HTML <template> tag, which Knockout components do not use.
Allowed elements inside <table> according to the WHATWG HTML standard document are
In this order: optionally a
captionelement, followed by zero or morecolgroupelements, followed optionally by atheadelement, followed optionally by atfootelement, followed by either zero or moretbodyelements or one or moretrelements, followed optionally by atfootelement (but there can only be onetfootelement child in total), optionally intermixed with one or more script-supporting elements.
("script-supporting elements" being <script> and <template>)
The best workaround might be to make up your own custom <table> tag as well and give it proper styling through display: table. If you do so, please consider giving it semantic meaning as well through ARIA roles such as grid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With