Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo DropDownList - add class at the optionLabel

Given following Kendo dropdown I would like to add a class onto the optionLabel select, so when ddl expanded I can visually distinguish in between what is the option label and what are the options. Ideally this should be done from dataBound and obviously must be done from js. I am looking for a fancy solution, I don't really want to traverse much of the DOM.

http://trykendoui.telerik.com/@vojtiik/uLEc

      $("#products").kendoDropDownList({
                    dataTextField: "ProductName",
                    dataValueField: "ProductID",
                    optionLabel: "select",
                    dataBound: function(e) {
                        // find the option label and add class
                    },
                    dataSource: {
                        transport: {
                            read: {
                                dataType: "jsonp",
                                url: "http://demos.telerik.com/kendo-ui/service/Products",
                            }
                        }
                    }
                });
like image 372
Vojtiik Avatar asked Mar 19 '23 20:03

Vojtiik


2 Answers

Please try with the below code snippet.

Method1:

<style type="text/css">
    #products_listbox li:first-child
    {
        background-color: Red !important;
        color: Yellow !important;
    }
</style>

Note : Products_list, in this Products is your dropdown ID.

Method2:

<script type="text/javascript">
    $(document).ready(function () {
        $("#products").kendoDropDownList({
                dataTextField: "ProductName",
                dataValueField: "ProductID",
                optionLabel: "select",
                open: function(e){
                        var listItem = $( "#products_listbox li:first-child" );
                        listItem.css( "background-color", "red" );
                        listItem.css( "color", "Yellow" );
                        },
                dataSource: {
                    transport: {
                        read: {
                            dataType: "jsonp",
                            url: "http://demos.telerik.com/kendo-ui/service/Products",
                        }
                    }
                }
            });
    });
</script>

I will try to create more generic solution once i will done with this. i will update you.

Note : Please use method1 for better performance of your page.

like image 78
Jayesh Goyani Avatar answered Apr 10 '23 09:04

Jayesh Goyani


you can do this on change event.. or may be any other way.. I think this way is quite easy.. you can also find the option label instead of finding the first child..

$(document).ready(function() {
                    $("#products").kendoDropDownList({
                        dataTextField: "ProductName",
                         dataValueField: "ProductID",
                        optionLabel: "select",
                        change: function(e){
                            var listItem = $( "#products_listbox li:first-child" );
                            listItem.css( "background-color", "red" ) ;
                          },
                        dataSource: {
                            transport: {
                                read: {
                                    dataType: "jsonp",
                                    url: "http://demos.telerik.com/kendo-ui/service/Products",
                                }
                            }
                        }
                    });
                });
like image 32
A dev Avatar answered Apr 10 '23 10:04

A dev