簡體   English   中英

如何為日期編寫自定義過濾器功能

[英]How can i write a custom filterfunction for Date

我有一個帶有時間戳列的DataTable:

<p-dataTable sortMode="multiple" scrollable="scrollable" scrollHeight="150" [value]="currentChartData" #dt>
        <p-column field="timestamp" header="Timestamp" [sortable]="true" [filter]="true">
            <ng-template pTemplate="filter" let-col>
                <div class="d-flex flex-row flex-wrap justify-content-center align-content-center">
                    <div style="padding-right: 29px">
                        <p-calendar [(ngModel)]="from" [showTime]="true"
                                    (onSelect)="filter(dt, 'from')"
                                    (onClearClick)="filter(dt, 'from')"
                                    showButtonBar="true" readonlyInput="true"
                                    [inputStyle]="{'width': '8em'}" styleClass="ui-column-filter" appendTo="body"
                                    dateFormat="dd.mm.yy">
                        </p-calendar>
                    </div>
                    <div style="padding-right: 29px">
                        <p-calendar [(ngModel)]="to" [showTime]="true"
                                    (onSelect)="filter(dt, 'to')"
                                    (onClearClick)="filter(dt, 'to')"
                                    showButtonBar="true" readonlyInput="true"
                                    [inputStyle]="{'width': '8em'}" styleClass="ui-column-filter" appendTo="body"
                                    dateFormat="dd.mm.yy">
                        </p-calendar>
                    </div>
                </div>
            </ng-template>
            <ng-template let-row="rowData" pTemplate="body">
                {{row.timestamp.toLocaleString()}}
            </ng-template>
        </p-column></p-dataTable>

我想使用兩個日歷作為“從”和“到”的過濾字段,以便過濾兩個日期之間的行。

我的過濾器功能如下所示:

between(value: any, from: any, to: any): boolean {
    if (from === undefined || from === null) {
        return true;
    }
    if (to === undefined || to === null) {
        return false;
    }
    if ((from === undefined || from === null ||
         (typeof from === 'string' && from.trim() === '') || from <= value) &&
        (to === undefined || to === null ||
         (typeof to === 'string' && to.trim() === '') || to >= value)) {
        return true;
    }
    return false;
};

通常,您可以使用dt.filter()在數據表上執行過濾器。 我如何覆蓋此函數以使用我的函數間進行過濾。 dt.filter()的返回值是dt.filter()

我將新的filterConstraint注入到Datatable中,

@ViewChild('dt') dataTable: DataTable;

ngAfterViewChecked() {
    if (this.dataTable !== undefined) {
        const customFilterConstraints = this.dataTable.filterConstraints;
        customFilterConstraints[ 'between' ] = this.between; 
        this.dataTable.filterConstraints = customFilterConstraints;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM