This is a migrated thread and some comments may be shown as answers.

Native Grid: Add a class to a row based on data

7 Answers 342 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
Wayne Hiller asked on 22 Jul 2020, 01:11 PM
Native Grid: Is there a way to assign a class to a row (tr) based on the data without having to completely render the row in a row template? Seems like it would be a pretty common operation.

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 24 Jul 2020, 12:01 PM

Hello,

Thank you for getting in touch with us.

Yes indeed - you are right. In the current version of the Kendo Native Grid there is no class property for the row and we should use template as for example it is done here in the general case- https://stackblitz.com/edit/s5rkb6-iwsg5p?file=index.js.

If your scenario is somehow more complicated please let me know so I could be more helpful.

Regards,
Plamen
Progress Telerik

0
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
answered on 24 Jul 2020, 03:21 PM

Thank you. I knew about the row rendering but I was missing the part about passing the defautSlots. This will work fine :)

 

return h(
                'tr', 
                { 
                  class: [defaultClass,'custom']
                }, 
                defaultSlots);

0
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
answered on 24 Jul 2020, 03:43 PM
One issue though, rendering a row this way breaks the rowclicked event.
0
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
answered on 24 Jul 2020, 04:17 PM

This seems to work:

renderRow(h, trElement, defaultSlots, props, listeners) {
            var cls = props.isAltRow ? "k-master-row k-alt" : "k-master-row";
            if (props.selectedField && props.dataItem[props.selectedField]) {
                cls += " k-state-selected";
            }
            if (props.dataItem.__viewed__) {
                cls += " viewed";
            }

            return h("tr", {
                class: cls,
                on: {
                    click: function(e) {
                        if (listeners.rowclick) {
                            listeners.rowclick({ dataItem: props.dataItem });
                        }
                    }
                }
            },
            defaultSlots);
        }

 

0
Plamen
Telerik team
answered on 28 Jul 2020, 01:12 PM

Hello,

Yes, this is the way to add the click event vie render function template - if you have further questions please don't hesitate to contact us again.

Regards,
Plamen
Progress Telerik

0
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
answered on 05 Aug 2020, 02:54 PM
There has to be a better way to do this. I find that I am having to recreate all the "row" functionality in the render function. Grouping is all messed up too now. It seems like a lot of work just to put a class on a TR.
0
Plamen
Telerik team
answered on 07 Aug 2020, 11:00 AM

Hello,

Thank you for getting back to us.

If you want to handle all the cases you will have to add a bit more logic in the template. Here is example that has all the default checks in it and worked correctly at my side with grouping - https://stackblitz.com/edit/8ah33e-glxq3t?file=index.js.

Please review it and let me know if you have further questions.

Regards,
Plamen
Progress Telerik

Tags
Grid
Asked by
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
Answers by
Plamen
Telerik team
Wayne Hiller
Top achievements
Rank 1
Veteran
Iron
Share this question
or