Framework:Javascript GridAngular GridReact GridVue Grid

JavaScript Grid: Date Component

You can create your own date components, and AG Grid will use them every time it needs to ask the user for a date value. The date components are currently used in date filters.

Simple Date Component

Below is a simple example of cell renderer class:

class CustomDateComponent {
   init(params) {
       const template = `
           <input type="text" data-input style="width: 100%;" />
           <a class="input-button" title="clear" data-clear>
               <i class="fa fa-times"></i>
           </a>`;

       this.params = params;
   
       this.eGui = document.createElement('div');
       this.eGui.setAttribute('role', 'presentation');
       this.eGui.classList.add('ag-input-wrapper');
       this.eGui.classList.add('custom-date-filter');
       this.eGui.innerHTML = template;
   
       this.eInput = this.eGui.querySelector('input');
   
       this.picker = flatpickr(this.eGui, {
           onChange: this.onDateChanged.bind(this),
           dateFormat: 'd/m/Y',
           wrap: true
       });
   
       this.picker.calendarContainer.classList.add('ag-custom-component-popup');
   
       this.date = null;
   }

   getGui() {
       return this.eGui;
   }

   onDateChanged(selectedDates) {
       this.date = selectedDates[0] || null;
       this.params.onDateChanged();
   }

   getDate() {
       return this.date;
   }

   setDate(date) {
       this.picker.setDate(date);
       this.date = date;
   }

   setInputPlaceholder(placeholder) {
       this.eInput.setAttribute('placeholder', placeholder);
   }
}

Example: Custom Date Component

The example below shows how to register a custom date component that contains an extra floating calendar picker rendered from the filter field. The problem with this approach is that we have no control over third party components and therefore no way to implement a preventDefault when the user clicks on the Calendar Picker (for more info see Custom Floating Filter Example). Our way of fixing this problem is to add the ag-custom-component-popup class to the floating calendar.

Registering Date Components

By default the grid will use the browser provided date picker for Chrome and Firefox (as we think it's nice), but for all other browsers it will just provide a simple text field. You can use your own date picker to AG Grid by providing a custom Date Component as follows:

const gridOptions = {
   // Here is where we specify the component to be used as the date picker widget
   components: {
       agDateInput: CustomDateComponent
   }
};

Please see Provided Components for more information about overrided AG Grid provided components (as we're doing here by overriding agDateInput).

Custom Date Interface

The interface for a custom date component is as follows:

interface IDateComp {
   // Mandatory methods

   // The init(params) method is called on the component once. See below for details on the parameters.
   init(params: IDateParams): void;

   // Returns the DOM element for this component
   getGui(): HTMLElement;

   // Returns the current date represented by this editor
   getDate(): Date;

   // Sets the date represented by this component
   setDate(date: Date): void;

   // Optional methods

   // Sets the input text placeholder
   setInputPlaceholder(placeholder: string): void;

   // Sets the input text aria label
   setInputAriaLabel(label: string): void;

   // Gets called when the component is destroyed. If your custom component needs to do
   // any resource cleaning up, do it here.
   destroy(): void;
}

Custom Filter Parameters

The init(params) method takes a params object with the items listed below:

interface IDateParams {
    // Callback method to call when the date has changed
    onDateChanged: () => void;
}