Open Yatoo2018 opened 2 years ago
When the text is too long, it is not easy to handle the display effect.
NZ-OVERFLOW can instruction
自己实现一个指令,通过修改样式不触发悬浮事件,来达到截停tooltip
缺点: 无法动态生效,动态也可以改成实施出发,不过一般不需要吧 无法直接使用tooltip指令的开关,对应的方法都是内部属性,无法通过外部调用
@Directive({
selector: '[nzOverflow]',
host: {
'[style.overflow]': "'hidden'",
'[style.whiteSpace]': "'nowrap'",
'[style.textOverflow]': "'ellipsis'",
'[style.wordBreak]': "'break-all'"
}
// 或者通过全局样式
// [nzOverflow] {
// overflow: hidden;
// white-space: nowrap;
// text-overflow: ellipsis;
// word-break: break-all;
// }
})
export class NzOverflowDirective implements AfterViewInit {
constructor(
private elementRef: ElementRef,
private renderer2: Renderer2,
@Optional() @Self() private tooltip: NzTooltipDirective
) {}
private isEllipsis(): boolean {
const clientWidth = this.elementRef.nativeElement.clientWidth;
const scrollWidth = this.elementRef.nativeElement.scrollWidth;
return scrollWidth > clientWidth;
}
ngAfterViewInit(): void {
// 不缩略并且存在tooltip指令
if (!this.isEllipsis() && this.tooltip) {
this.renderer2.setStyle(this.elementRef.nativeElement, 'pointer-events', 'none');
}
}
}
此指令单独存在只等于缩略,需要配合tooltip
nz-typography里的nzEllipsis + nz-tooltip应该可以满足你的需求吧
基于@caoxicheng的思路 在table nzEllipsis属性基础上动态创建一个toolTip组件 兼容是因为之前的写法都是使用nzEllipsis + nz-tooltip实现的 这种实现方式tooltip永远存在
What problem does this feature solve?
文本过长时不好处理展示效果。
What does the proposed API look like?
nz-overflow 一个指令即可