I have two tables.
I want to set the width of the column of one table to equal the width of the column of another table.
Like this :
<table class="table-header table">
<thead class="table-bordered">
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Education</th>
</tr>
</thead>
</table>
And the other table:
<table class="table-header table">
<thead class="table-bordered">
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Education</th>
</tr>
</thead>
<tbody>
<tr>
<td> Ross </td>
<td> Ross </td>
<td> M</td>
<td> BsC </td>
..
</tbody>
What I want to do is have all the headers of the first table to always equal the headers width of the second table. Because on the second table they get resized based on the content.
So I know I can add a HostListener('window:resize') But how do I assign these values in Angular ? So that they're always the same. As if it's a duplicate of one another. Any guidance?
This does work but if you were to do a ctrl f5 it breaks the table. In order for me to fixed this issue I had to use ngAfterVeiwChecked but however life cycle hook constantly triggers when the dom changes or screen size changes ect... I'm not sure I can find a efficient way.
In the picture you will see the headers break if you were to do a ctrl + f5 but however after that if you just simply refreshed your page via the refresh button it's fine. It's just when you do a ctrl f5
Broken Headers
First you need to get a reference of your source table columns and your target table columns, you can do this with a template variables and the
@ViewChildren
decorator.In your template:
And then inside you component class
This way you can get a
QueryList
of elements references, so you can access thenativeElement
of each table heading (the actualDOM
Nodes) and get each individualoffsetWidth
. Notice@ViewChild
and@ViewChildren
decorated properties are only available after theAfterViewInit
lifecycle hook. AQueryList
is an array-like data structure and it has some array methods likeforEach
,map
, etc...Inside
ngAfterViewInit
you want to iterate over the list of source table header cells, get each one width and then iterate over target header cell list and set the width. For this, you can use thesetStyle
method fromRenderer2
. Then you can call this method also onwindow.resize
event or whatever