Articles in this section
Category / Section

How to Render ColorPicker When Editing Record in JavaScript Grid?

5 mins read

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:

 

Apply Color in Grid Cells

      Figure 1: Applying Color in Grid cells using RowDataBound event


Edit the color

     Figure 2: Rendering ColorPicker Component while editing.

 

updating the color to the corresponding cell

     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 forumsBoldDesk Support, or feedback portal. We are always happy to assist you!







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