Articles in this section
Category / Section

How to render ColorPicker component for particular column while editing a record

1 min read

You can render the ColorPicker component for a particular column while editing a record using the “Cell Edit Template” feature of 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

 

Apply Color in Grid Cells

Fig 1: Applying Color in Grid cells using RowDataBound event

Edit the color

Fig 2: Rendering ColorPicker Component while editing.

 

updating the color to the corresponding cell

Fig 3: Updating the selected color to the corresponding Cell.

 

JavaScript Demo

Angular Demo

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied