Ambuj Jauhari
Ambuj Jauhari

Reputation: 1299

Custom datepicker on cell edit with ag-grid

I am trying to implement a date picker on cell edit. i have tried the below example

https://www.ag-grid.com/javascript-grid-cell-editing/#example-datepicker-cell-editing

This example used jquery-ui datepicker

function getDatePicker() {
  function Datepicker() {}
  Datepicker.prototype.init = function(params) {
    this.eInput = document.createElement("input");
    this.eInput.value = params.value;
    $(this.eInput).datepicker({ dateFormat: "dd/mm/yy" });
  };
  Datepicker.prototype.getGui = function() {
    return this.eInput;
  };
  Datepicker.prototype.afterGuiAttached = function() {
    this.eInput.focus();
    this.eInput.select();
  };
  Datepicker.prototype.getValue = function() {
    return this.eInput.value;
  };
  Datepicker.prototype.destroy = function() {};
  Datepicker.prototype.isPopup = function() {
    return false;
  };
  return Datepicker;
}

This line

$(this.eInput).datepicker({ dateFormat: "dd/mm/yy" });

is used to add jquery-ui datepicker

How can i have a custom DatePicker react component that i want to include instead of jquery-ui datepicker ?

Upvotes: 4

Views: 4985

Answers (2)

Paul
Paul

Reputation: 447

Sample code from https://www.ag-grid.com/javascript-grid-cell-editing/#example-datepicker-cell-editing is written in a way it supports Jquery UI Datepicker in react project.

I have a suggested solution that i have tried with TextField Material UI and Material UI Native DatePicker. Kindly check below code it worked well for me.

const getDatePicker = () => {
  function Datepicker() {}
  Datepicker.prototype.init = function(params) {
      const fillZeros = (a) => {
          return (Number(a) < 10) ? '0' + a : a;
      }
      const getFormatedDate = (dateString ) => {
          const dateParse = new Date(dateString.split('/')[1]+'-' + dateString.split('/')[0]+'-'+dateString.split('/')[2]);
          const dd = dateParse.getDate();
          const mm = dateParse.getMonth() + 1; //January is 0!
          const yyyy = dateParse.getFullYear();
          console.log(dateString, yyyy + '-' + fillZeros(mm) + '-' + fillZeros(dd));
          return yyyy + '-' + fillZeros(mm) + '-' + fillZeros(dd);
      }
      this.textInput = React.createRef();
      const eInput = React.createElement(TextField, {
          type: "date",
          defaultValue: getFormatedDate(params.value),
          ref: this.textInput
      });
      this.div = document.createElement('div');
      this.div.className = "ag-cell-parent-append";
      ReactDOM.render(eInput, this.div);
  };
  Datepicker.prototype.getGui = function() {
      return this.div;
  };
  Datepicker.prototype.afterGuiAttached = function() {
      this.textInput.current.focus();
  };
  Datepicker.prototype.getValue = function() {
      return this.textInput.current.querySelector('input').value;
  };
  Datepicker.prototype.destroy = function() {};
  Datepicker.prototype.isPopup = function() {
      return false;
  };
  return Datepicker;
}

Full working example is in stackblitz

Upvotes: 0

Faust Life
Faust Life

Reputation: 71

You need to implement interface ICellEditorComp {...} a Doc about custom editors is here https://www.ag-grid.com/javascript-grid-cell-editor/

Upvotes: 0

Related Questions