How to Render ColorPicker When Editing Record in JavaScript Grid?
This article explains how to render a ColorPicker when editing a record in the JavaScript Grid. You can render the ColorPicker component for a particular column while editing a record using the “Cell Edit Template” feature of JavaScript Grid component.
This is explained in the following sample code in which the ColorPicker component has been rendered using the “column.edit” property. The “rowDataBound” event triggers every time the row is bounded with data. Inside this event, the color value can able to be applied to the cell background.
JS
var grid = new ej.grids.Grid({
dataSource: data,
toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
columns: [
{ field: "OrderID", isPrimaryKey: true, headerText: "Order ID", width: 90 },
{ field: "CustomerID", headerText: "CustomerID", width: 90 },
{ field: "EmployeeID", headerText: "Employee ID", width: 90 },
{
field: "Color", headerText: "Color",
edit: {
create: function () {
elem = document.createElement('input');
return elem;
},
destroy: function () {
colorPickerObj.destroy();
},
read: function () {
return colorPickerObj.value;
},
write: function (args) {
colorPickerObj = new ej.inputs.ColorPicker({
value: args.rowData[args.column.field]
});
colorPickerObj.appendTo(elem);
}
},
width: 90
}
],
rowDataBound: rowdatabound,
});
grid.appendTo('#Grid');
function rowdatabound(args) {
var colIndex = grid.getColumnIndexByField("Color"); // Get the column index for Color
var colorTd = args.row.querySelectorAll("td.e-rowcell")[colIndex];
// Get the cell from Grid rows
var color = colorTd.innerText;//get the colour value of the cell
colorTd.innerHTML = "<div style='padding:3px'>" + color + "</div>";//Append a div element inside the colour Td element
colorTd.querySelector("div").style.backgroundColor = args.data.Color; // Apply the background colour to the div element inside the td
}
Output:

Figure 1: Applying Color in Grid cells using RowDataBound event

Figure 2: Rendering ColorPicker Component while editing.

Figure 3: Updating the selected color to the corresponding Cell.
You can also refer:
JavaScript Sample: JavaScript Demo
Angular Sample: Angular Demo
Conclusion
We hope you enjoyed learning about how to print only selected records in Grid of JavaScript.
You can refer to our JavaScript Grid page to know about its other groundbreaking feature representations. You can also explore our JavaScript DataGrid Documentation to understand how to manipulate data.
For current customers you can check out on our JavaScript components from the License and Download page. If you are new to Syncfusion, you can try our 30-day free trial to check out our JavaScript DataGrid and other JavaScript components.
If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!