How to render a date picker for date value cells in JavaScript Spreadsheet
This article explains how to render a date picker for date value cells in a JavaScript Spreadsheet. On the created event, bind a function to insert the DatePicker control. Also, check whether the header cell is a Date column, and then render the DatePicker control to the specified date cells in the beforeCellRender event.
[HTML]
<div id="spreadsheet"></div>
[JS]
//Initialize Spreadsheet component.
let spreadsheet = new ej.spreadsheet.Spreadsheet({
sheets: [
{
name: 'Car Sales Report',
ranges: [{ dataSource: defaultData }],
columns: [
{ width: 180 },
{ width: 130 },
{ width: 130 },
{ width: 180 },
{ width: 130 },
{ width: 120 },
],
},
],
created: function () {
spreadsheet.cellFormat(
{ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
'A1:F1'
);
// Bind the method to update date picker.
bindDatePicker();
},
beforeCellRender: function (args) {
// Condition to check the sheet index and the rendered cell is not the header.
if (
spreadsheet.activeSheetIndex === 0 &&
!args.element.classList.contains('e-header-cell')
) {
// Render templates dynamically for date valued cells.
let target = args.element.firstElementChild;
// Convert the input element as an EJ2 datepicker component.
new ej.calendars.DatePicker(
{
placeholder: 'Delivery Date',
value: spreadsheet.getDisplayText(args.cell),
change: onChange,
},
target
);
}
},
});
spreadsheet.appendTo('#spreadsheet');
function onChange(args) {
let sheet = spreadsheet.getActiveSheet();
let cellvalue = args.value && args.value.toLocaleDateString('en-US');
// update the changed date picker value in the cell model.
spreadsheet.updateCell({ value: cellvalue }, sheet.activeCell);
}
function bindDatePicker() {
//Fetch active sheet.
let sheet = spreadsheet.getActiveSheet();
// Fetch used range of the sheet.
let range = sheet.usedRange;
let rangeCollection = [];
// Iteration between the used range to update template on date valued cells.
for (let i = 0; i <= range.rowIndex; i++) {
let cells = sheet.rows[i].cells;
for (let j = 0; j <= range.colIndex; j++) {
// Condition to check whether the value is date or not.
if (cells[j] && cells[j].value && cells[j].format == 'mm-dd-yyyy') {
let address = ejs.spreadsheet.getCellAddress(i, j);
let ranges = {
template: '<input />',
address: address, // Specify the range you want.
};
rangeCollection.push(ranges);
}
}
}
// Update the range collection in which the date picker is rendered.
spreadsheet.sheets[0].ranges = rangeCollection;
spreadsheet.resize(); // To refresh the spreadsheet.
}
Sample Link:
Refer to the working sample for additional details and implementation: Sample
Output:
Documentation link:
https://ej2.syncfusion.com/javascript/documentation/spreadsheet/getting-started
https://ej2.syncfusion.com/javascript/documentation/datepicker/es5-getting-started
Demo link:
https://ej2.syncfusion.com/javascript/demos/#/material3/spreadsheet/cell-template.html
https://ej2.syncfusion.com/javascript/demos/#/material3/datepicker/default.html
Conclusion
We hope you enjoyed learning about how to render a date picker for date value cells in JavaScript Spreadsheet.
You can refer to our JavaScript Spreadsheet feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Spreadsheet example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, BoldDesk Support or feedback portal. We are always happy to assist you!