ngzorro 自定义指令操作 nz-table 列拖动缩放

创建指令

ng g d drap-table-column

import {AfterViewInit, Directive, ElementRef, Input} from '@angular/core';

@Directive({
    selector: '[appDrapTableColumn]'
})

export class DrapTableColumnDirective implements AfterViewInit {
    @Input('appDrapTableColumn') columnWidths: string[];
    @Input() usePixel = false;

    constructor(private $element: ElementRef<HTMLTableElement>) {
    }

    ngAfterViewInit() {
        const el = (() => {
            let result = this.$element.nativeElement;
            if (result.tagName === 'NZ-TABLE') {
                result = result.querySelector('table');
            }
            if (result.tagName !== 'TABLE') {
                throw new TypeError('Must be a TABLE element');
            }
            if (!result.tHead) {
                throw new TypeError('Must have a THEAD element');
            }
            return result;
        })();
        el.classList.add('table-resizable');
        const tr = el.tHead.rows[0];
        if (!tr) {
            throw new TypeError('Must have at least one TR element inside THEAD element');
        }

        setTimeout(() => {
            const ths = Array.from(tr.cells) as HTMLTableHeaderCellElement[];
            if (ths.length <= 1) { return; }
            --ths.length; // 最后一个方格总是自动列宽的(用于占满整行)

            if (!Array.isArray(this.columnWidths)) {
                this.columnWidths = new Array(ths.length).fill('');
            } else {
                this.columnWidths.length = ths.length;
                this.columnWidths.forEach((x, i, arr) => {
                    if (!x) {
                        arr[i] = '';
                    }
                });
            }

            ths.forEach(th => {
                if (th.classList.contains('no-resize')) {
                    return;
                }
                if (this.columnWidths[th.cellIndex]) {
                    th.width = this.columnWidths[th.cellIndex];
                }

                const i = document.createElement('i');
                i.classList.add('resize-indicator');
                th.appendChild(i);

                i.addEventListener('mousedown', e => {
                    if (e.button === 1) { // 鼠标中键
                        th.width = '';
                        return;
                    }
                    if (e.button !== 0) {
                        return;
                    }
                    const startX = e.pageX;
                    const startThWidth = th.clientWidth;
                    document.body.classList.add('table-resizing');

                    let mousemoveHandler;

                    document.body.addEventListener('mousemove', mousemoveHandler = e => {
                        if (e.button !== 0) {
                            return;
                        }
                        const pixel = e.pageX - startX + startThWidth;
                        if (this.usePixel) {
                            th.width = pixel + '';
                        } else {
                            th.width = pixel / tr.offsetWidth * 100 + '%';
                        }
                        this.columnWidths[th.cellIndex] = th.width;
                    });

                    document.body.addEventListener('mouseup', e => {
                        if (e.button !== 0) {
                            return;
                        }
                        document.body.removeEventListener('mousemove', mousemoveHandler);
                        document.body.classList.remove('table-resizing');
                    }, { once: true });
                });
            });
        });
    }
}

缩放样式

.table-resizable th {
    position: relative;
}

body.table-resizing {
    cursor: col-resize !important;
    user-select: none;
}

.resize-indicator {
    display: block;
    position: absolute;
    top: 0;
    right: -5px;
    width: 9px;
    height: 100%;
    cursor: col-resize;
}

在app.module.ts中引入自定义指令

import {NgModule} from '@angular/core';
import {DrapTableColumnDirective} from './drap-table-column.directive';

@NgModule({
    declarations: [DrapTableColumnDirective],
    exports: [DrapTableColumnDirective],
    providers: []
})
export class DirectiveModule { }

在app.module.ts中引入自定义指令

imports: [
	DirectiveModule
],

使用

<nz-table [appDrapTableColumn]></nz-table>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值