javascript - Angular Dom:- 将缺失的单元格(td)添加到 Angular 材质日历
问题描述
我正在尝试在角度材料日历中添加缺失的单元格。下面是我的html
<mat-calendar
[dateClass]="dateClass()"
[startAt]="month"
[selected]="selectedDate"
(selectedChange)="onSelect($event)">
</mat-calendar>
我使用 ViewChildern 访问它的 dom
@ViewChildren(MatCalendar, { read: ElementRef }) calendars: QueryList<ElementRef>;
我能够在角度材料日历的底部添加空的额外行
this.calendars.forEach(calendarEl => {
const tbody: HTMLElement = calendarEl.nativeElement.querySelector('tbody');
//ToDO:-- identify last row, count number of cells in last row,if its less than 7 add missing cells
//Added extra row to the bottom
const tr = (document.createElement('tr') as HTMLTableRowElement);
tr.className = 'date-class';
for (let i = 0; i < 7; i++) {
tr.appendChild(document.createElement('td'));
}
tbody.appendChild(tr);
});
解决方案
我们可以做一些事情,比如获取最后一行并将追加数量添加到该行,以便孩子<td>
的总数变为 7。<td>
let lastRow = calendarEl.nativeElement.querySelector('tbody tr:last-child');
let vaccantColCount = 7 - lastRow.querySelectorAll('td').length;
console.log(vaccantColCount);
while(vaccantColCount > 0) {
lastRow.appendChild(document.createElement('td'));
vaccantColCount--;
}
推荐阅读
- java - C# DES 与 Java DES Byte[] 和 BlockSize 不同
- c# - 如何在 Visual Studio 2019 中编辑 cw 片段?
- c# - 如何反序列化具有可变数量字符串的 JSON 对象?
- java - 如何更新 SQLite 中单行的多个列?
- python-3.x - type.__call__ 如何知道其结果的类型?
- javascript - ECMAScript 模块与对象作为命名空间
- android - 字符串参数未从本地通知发送到活动
- python - 为什么这段代码会执行 closed[init[0]][init[1]] 而不是 closed[init[0]][init[0]]?
- azure - C# 等效于 Get-AzEventHubNamespaceKey
- docker - 无法从远程 Linux 机器连接到 docker