简体   繁体   中英

How can i write a custom filterfunction for Date

I have a DataTable with a column Timestamp:

<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() . How am i able to overwrite this to filter with my between function. What is the return value of dt.filter() ?

I injected the new filterConstraint into the Datatable with

@ViewChild('dt') dataTable: DataTable;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM