【发布时间】:2019-04-17 00:00:14
【问题描述】:
我有一个表格,我希望用户能够通过单击触发方法并在多个单元格上拖动(即更改被单击/拖动的单元格的背景颜色)。
我想在 Angular 中创建它。
当我使用 click 方法时,它只触发第一次点击的单元格,而不是鼠标按下的任何其他单元格(即,我必须单击每个单元格以突出显示或取消突出显示)。
下面是stackblitz
组件:
<table>
<TR>
<TD *ngFor="let b of colCount"
(click)="b.highlight = !b.highlight"
[class.highlight]="b.highlight"
></TD>
</TR>
</table>
TS:
colCount = [{highlight: true},{highlight: true},{highlight: true},{highlight: true},{highlight: true},{highlight: true}]
select(b) {
console.log(b)
b.highlight = !b.highlight
}
CSS:
td {
border: 1px solid black;
width: 20px !important;
height: 20px !important;
}
.highlight {
background-color: blue;
}
【问题讨论】:
-
您需要使用事件
mousedown、mousemove和mouseup进行设置。我建议在mousedown发生时在组件mouseIsDown中设置一个标志,然后在标志为真时更改mousemove中的突出显示,然后在mouseup中取消设置标志。 -
谢谢。你见过这样的例子吗?