0

Iv been trying to create an angularJS component for constructing a dynamic html table from a provided array. The components template includes some ng-transclude directives as 'placeholders'/slots for templates provided by the component consumer.

Is it possible to transclude a <tr> template (with nested <th>), to be used inside the table <thead>?

I have tried using all variations for transclution: transclude: true, transclude: 'element' and transclude: { header: 'header'} (the third option seems as the most suitable, as I will eventually required multiple elements to be transcluded).

Component:

app.component("virtualTable", {
  bindings: {
    someArray: "<"
  },
  transclude: {
    header: "header"
  },
  templateUrl: () => {
    return "/app/components/virtual-table.component.html";
  });

Component Template:

<div>
  <table>
    <thead>
      <tr ng-transclude="header"></tr>
    </thead>
    <tbody>
      <tr ng-repeat="item in vt.someArray">
        ...another place for transclution...
      </tr>
    </tbody>
  </table>
</div>

Consuming HTML (component declaration):

<virtual-table some-array="vm.personList">
    <header>
        <th>First Column</th>
        <th>Second Column<th>
    </header>
</virtual-table>

I excpect the compiled DOM to look like this:

<table>
    <thead>
        <tr>
            <th>First Column</th>
            <th>Second Column</th>
        </tr>
    </thead>
...

But instead I get the innerHTML of the transcluded element (header) as Text:

<table>
    <thead>
        <tr ng-transclude="header">
            <header class="ng-scope">
                First Column
                Second Column
        </tr>
    </header>
...

I Suspect the browser (chrome 76) to somehow strip the <tr> tags from the transcluded element before it is being provided to the component (as it illegal us to use <tr> outside of a <table>). I would like to maintain an html table structure. Is The Any Way to Solve This?

2
  • Maybe it will help you: stackoverflow.com/questions/40525417/… It's not a component, but it's a dynamic table. You can copy the idea and tailor it to your needs. Commented Aug 25, 2019 at 12:34
  • Toda (thanks) @MiriGold, However the construction of the table has to be within a component (or directive) for the purpose of reusability. This component is meant to be used throughout a huge web application (currently about 400 unique pages) as a replacement for kendo (grid) library. For this reason, I must keep it as modular/generic as possible. For this to happen, the consumer should be able to provide the table head row (among other parts of the table) via transclusion. Commented Aug 25, 2019 at 16:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.