Changing color of Specific row

1 Answer 33 Views
Data Source General Discussions Grid
Swapnil
Top achievements
Rank 1
Swapnil asked on 24 Nov 2023, 09:22 AM
Hi Team,

In kendo grid jquery I have added data to a data Source in JSON format. In this format, there is a property in the JSON. If that property is true, I want to change the color of the row. Is it possible to set a different color?

gridData = @Html.Raw(ViewData["DataDetailsJson"]);


var dataSource = new kendo.data.Data Source({
    data: gridData,
    batch: true,
    autoSync: true,
    schema: {
        model: {
            id: "uid",
            fields: {
                id: { hidden: true },
                Project: { editable: false },
                User: { editable: false },
                Status: { editable: false },
            },
        },
    },
});

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 29 Nov 2023, 08:43 AM

Hello,

I would suggest using columns.template:

- https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/configuration/columns.template

In the template you can customize the cell appearance. For example you can add custom class. Then, you can use the class to style the needed rows. 
Below is an example:

columns: [ 
          {
            field: "name"    
          },
          {
            field: 'color',
            template: function(dataItem) {
              let html = ''
              if(dataItem.color){
                html = "<span class='custom'>" + dataItem.color + "</span>";
              }else{
                html = "<span>" + dataItem.color + "</span>";
              }
              return html;              
            }
          }
        ],
        dataBound: function(){
          $('.custom').closest('tr').css('background-color', 'violet').css('color', 'white')
        },

Here is a Dojo example where this is demonstrated - https://dojo.telerik.com/@NeliKondova/AjeCiXok

I hope this helps.

Regards,
Neli
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Swapnil
Top achievements
Rank 1
commented on 07 Dec 2023, 11:26 AM

Hi Neil ,

I'm currently working on a project where I have a grid populated with data from the following JSON format:

[
  {
    "id": 1,
    "name": "Item 1",
    "status": true
  },
  {
    "id": 2,
    "name": "Item 2",
    "status": false
  },
  {
    "id": 3,
    "name": "Item 3",
    "status": true
  },
  {
    "id": 4,
    "name": "Item 4",
    "status": false
  }
]

I'm looking for advice on the best approach to dynamically add colors to rows in the grid based on the "status" property. If the status is true, I want to apply a specific color to that row; otherwise, I want it to remain the default.

I'm open to suggestions and would love to hear about any recommended practices or efficient ways to handle this. If you have any insights or advice, please share them with me.

Thank you so much for your time and assistance.

Best regards, Swapnil

Neli
Telerik team
commented on 12 Dec 2023, 07:19 AM

Hi Swapnil,

You can use the same approach as described in my previous reply. If you need to change the colors of the row after a value in the Status column has been edited, you can handle the save event.

- https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/save

In the event handler you can add or remove the class applied to the entire row:

 dataBound: function(){  
            $('.custom').closest('tr').addClass('custom-row')
          },         
          save: function(e){ 
           if(e.values.status){
                $(e.container).closest('tr').addClass('custom-row')
          }else if(e.values.status == false){
               $(e.container).closest('tr').removeClass('custom-row')
           }         
 },

Here you will find the modified Dojo example where the data form the provided snippet is used. 

Regards,

Neli

Tags
Data Source General Discussions Grid
Asked by
Swapnil
Top achievements
Rank 1
Answers by
Neli
Telerik team
Share this question
or