How to Render ColorPicker When Editing Record in 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
Fig 1: Applying Color in Grid cells using RowDataBound event
Fig 2: Rendering ColorPicker Component while editing.
Fig 3: Updating the selected color to the corresponding Cell.
I hope you enjoyed learning on how to render ColorPicker when editing record in JavaScript Grid.
You can refer to our JavaScript Grid 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 Grid 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, Direct-Trac, or feedback portal. We are always happy to assist you!