简体   繁体   English

如何为日期编写自定义过滤器功能

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

I have a DataTable with a column Timestamp: 我有一个带有时间戳列的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>

I want to use two calendars for filterfields "from" and "to" in order to filter for row between two dates. 我想使用两个日历作为“从”和“到”的过滤字段,以便过滤两个日期之间的行。

My filter function looks like this: 我的过滤器功能如下所示:

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;
};

Normally you execute a filter on the datatable with dt.filter() . 通常,您可以使用dt.filter()在数据表上执行过滤器。 How am i able to overwrite this to filter with my between function. 我如何覆盖此函数以使用我的函数间进行过滤。 What is the return value of dt.filter() ? dt.filter()的返回值是dt.filter()

I injected the new filterConstraint into the Datatable with 我将新的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