<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>table &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/table/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 23 Mar 2024 14:11:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to create a custom range calendar filter for Angular PrimeNg data turbo table</title>
		<link>https://codepills.com/how-to-create-a-custom-range-calendar-filter-for-angular-primeng-data-turbo-table/</link>
					<comments>https://codepills.com/how-to-create-a-custom-range-calendar-filter-for-angular-primeng-data-turbo-table/#comments</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Fri, 14 Aug 2020 17:48:34 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Angular]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[custom filter]]></category>
		<category><![CDATA[ngPrime]]></category>
		<category><![CDATA[PrimeNg]]></category>
		<category><![CDATA[range]]></category>
		<category><![CDATA[table]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1065</guid>

					<description><![CDATA[In this article, we will look at how to create a custom filter for PrimeNg calendar selection with a date range <a href="https://codepills.com/how-to-create-a-custom-range-calendar-filter-for-angular-primeng-data-turbo-table/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>Let&#8217;s look at how to implement the Angular selection range for the newest Angular PrimeNg version. This tutorial was developed on <b>v10.0.0-rc-1</b> version.</p>
<p><span id="more-1065"></span></p>
<p>I had difficulty finding a proper solution for range selection in the PrimeNg turbo data table for the newest version. Therefore I have decided to create a workaround. For the time selection, each click event on the calendar panel fire only one piece of data &#8211; the currently selected date. So the calendar range is stateless. You need to remember somehow to keep in memory what date was fired before and implement this logic in a custom filter.</p>
<p>I observed that there are several rules according to which <code>p-calendar</code> component behave when the selection mode <code>"range"</code> is selected. Here are my findings according to which I programmed the logic:</p>
<ul>
<li>At first, the calendar component has an empty, undefined selection.</li>
<li>If one date is selected, only one date is fired on click event.</li>
<li>If the same date as date previously selected is selected, new date (which is the same as previously selected date) is fired on click event.</li>
<li>If a date before the already selected date is selected, the selection is cancelled and earlier date is fired on click event.</li>
<li>If a date after the already selected date is selected, the selection range is visible.</li>
<li>If any new date is selected after the selection range is visible, a new date is selected, fired upon click event and selection range is cancelled.</li>
</ul>
<p>When all this wrote down, here is the implementation code for component and custom filter:</p>
<p><b>record-list.component.html</b></p>
<pre><code class="language-html">&#60;p-table #dt [value]="records" sortMode="multiple" [paginator]="true" [rowsPerPageOptions]="rowsPerPageTable" [rows]="rowsPerTable" [showCurrentPageReport]="true" [(first)]="page" styleClass="p-datatable-striped" currentPageReportTemplate="Showing {first} to {last} of {totalRecords} entries"&#62;
    &#60;ng-template pTemplate="header"&#62;
        &#60;tr class="ui-table-thead"&#62;
            &#60;th [ngStyle]="{'width':'50%'}" pSortableColumn="createdDate"&#62;Created &#60;p-sortIcon field="createdDate"&#62;&#60;/p-sortIcon&#62;&#60;/th&#62;
            &#60;th [ngStyle]="{'width':'50%'}" pSortableColumn="info"&#62;Info &#60;p-sortIcon field="info"&#62;&#60;/p-sortIcon&#62;&#60;/th&#62;
        &#60;/tr&#62;
        &#60;tr class="ui-table-thead"&#62;
            &#60;th scope="col"&#62;
                &#60;p-calendar (onSelect)="onDateSelect($event)" [style]="{'width':'100%'}" selectionMode="range" [readonlyInput]="true" (onClearClick)="onDateClear($event)" [showButtonBar]="true" placeholder="Created Date" class="p-column-filter" dateFormat="yy-mm-dd"&#62;&#60;/p-calendar&#62;
            &#60;/th&#62;
            &#60;th scope="col"&#62;
                &#60;input pInputText type="text" [style]="{'width':'100%'}" (input)="dt.filter($event.target.value, 'info', 'contains')" placeholder="Info"&#62;
            &#60;/th&#62;
        &#60;/tr&#62;
    &#60;/ng-template&#62;

    &#60;ng-template pTemplate="body" let-rowData&#62;
        &#60;tr ng-repeat="rowData in let-rowData" ng-click="showClient(rowData)"&#62;
            &#60;td&#62;{{ rowData.createdDate }}&#60;/td&#62;
            &#60;td&#62;{{ rowData.info }}&#60;/td&#62;
        &#60;/tr&#62;
    &#60;/ng-template&#62;

&#60;/p-table&#62;</code></pre>
<p><b>record-list.component.ts</b></p>
<pre><code class="language-typescript">export class RecordListComponent implements OnInit {

    public records: IssueShortInfo[];

    @ViewChild('dt') table: Table;

    cols: any[];
    dateRangeStart: string;
    dateRangeEnd: string;

    rowsPerPageTable: number[] = [25, 50, 100, 200];

    page: number = 0;
    rowsPerTable: number = 25;

    constructor(private recordService: RecordService) {}

    ngOnInit() {

        this.recordService.getAllRecords().subscribe(page => {
            this.getRecords(page);
        }, (error => {
            console.log(error)
        }));

        this.cols = [
            {field: 'id', header: 'Id'},
            {field: 'createdDate', header: 'Created Date'},
            {field: 'info', header: 'Info'}
        ];

        FilterUtils['customCreatedDateFilter'] = (value: string, filter) => {

            if (this.dateRangeStart === value && this.dateRangeEnd === undefined) {
                return true;
            }

            if (this.dateRangeStart === value || this.dateRangeEnd === value) {
                return true;
            }

            if (this.dateRangeStart !== undefined && this.dateRangeEnd !== undefined &&
                moment(this.dateRangeStart).isBefore(value) && moment(this.dateRangeEnd).isAfter(value)) {
                return true;
            }

            return false;
        };
    }

    getRecords(page: Page<IncomingRecordDTO>) {
        this.records = page.content.map(incomingRecordDTO => new Record(incomingRecordDTO));
    }

    onDateSelect($event) {

        const eventDate = this.formatDate($event);

        if (this.dateRangeStart === undefined) {
            this.dateRangeStart = eventDate;
        } else if (moment($event).isBefore(this.dateRangeStart)) {
            this.dateRangeStart = eventDate;
            this.dateRangeEnd = undefined;
        } else if (moment($event).isSame(this.dateRangeStart) && this.dateRangeStart !== undefined && this.dateRangeEnd === undefined) {
            this.dateRangeEnd = eventDate;
        } else if (moment($event).isSame(this.dateRangeStart) && this.dateRangeStart !== undefined && this.dateRangeEnd !== undefined) {
            this.dateRangeStart = eventDate;
            this.dateRangeEnd = undefined;
        } else if (moment($event).isAfter(this.dateRangeStart) && this.dateRangeStart !== undefined && this.dateRangeEnd !== undefined) {
            this.dateRangeStart = eventDate;
            this.dateRangeEnd = undefined;
        } else {
            this.dateRangeEnd = eventDate;
        }

        this.table.filter(eventDate, 'createdDate', 'customCreatedDateFilter');
    }

    onDateClear($event) {
        this.dateRangeStart = undefined;
        this.dateRangeEnd = undefined;
        this.table.filter('', 'createdDate', 'equals');
    }

    formatDate(date) {
        let month = date.getMonth() + 1;
        let day = date.getDate();

        if (month < 10) {
            month = '0' + month;
        }

        if (day < 10) {
            day = '0' + day;
        }
        return date.getFullYear() + '-' + month + '-' + day;
    }

    customCreatedDateArrayFilter(event) {
        this.table.filter(event, 'createdDate', 'customCreatedDateFilter');
    }

}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-create-a-custom-range-calendar-filter-for-angular-primeng-data-turbo-table/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
