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

Select default item from the dropdownlist if Item count is 1

4 Answers 2489 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Amol
Top achievements
Rank 1
Amol asked on 29 Jul 2013, 01:49 PM
Hi,

I have a cascading dropdownlist,  and if the Item count for the dropdown getting populated is 1 I want to keep the item it default selected instead of showing OptionLabel and the item. Or else remove the Option label in that case. below of the my dropdown which gets populated on change of dropdown 'DeliveryDestinationID'. How do i do this.

@(Html.Kendo().DropDownList()
                                      .Name("DeliveryNameID")
                                      .HtmlAttributes(new { style = "width:235px" })
                                      .OptionLabel("Select Delivery Name...")
                                      
                                      .DataTextField("Text")
                                      .DataValueField("Value")
                                     .DataSource(source =>
                                      {
                                          source.Read(read =>
                                          {
                                              read.Action("GetDeliveryNames", "DeliveryQueue")
                                                  .Data("filterAsPerServiceProvider");
                                          })
                                          .ServerFiltering(true);
                                      })
                                      .Enable(false)
                                      .AutoBind(false)
                                      .CascadeFrom("DeliveryDestinationID")
                                    )



4 Answers, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 31 Jul 2013, 06:31 AM
Hello Amol,


This is a quote from my answer in the support thread:

To achieve this you could bind to the dataBound event of the DropDownList and check the length of the dataSource data. If there is only one item, you could select it.
E.g.
.Events(e => e.DataBound("dataBound"))

function dataBound(e) {
    var ds = this.dataSource.data();
    if (ds.length == 1) {
        this.select(1);
    }
}



Please post your questions only once, so we could provide you the best possible assistance. I wish you a great day!

 

Regards,
Dimiter Madjarov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Luiz
Top achievements
Rank 1
answered on 12 Jan 2017, 10:52 AM

Or you could simply do:

if (this.dataSource.total() > 0)
    this.select(0);

 

0
Magendran
Top achievements
Rank 1
answered on 11 Jan 2021, 12:51 PM

Hi, 

How to achieve the same in Kendo with Angular?

0
Neli
Telerik team
answered on 13 Jan 2021, 09:20 AM

Hello Magendran,

If you need to achieve the same behavior in AngularJS scenario, you could use similar approach to the one described in the previous replies. BElow is an example:

 dataBound: function(e) {            
            var ds = e.sender.dataSource.view();
            if (ds.length == 1) {
                   this.select(1);
            }
}

Here is a Dojo example with cascading DropDownLists in AngularJS scenario where the above is implemented.

However, if you need help in achieving the same result in Angular 2+ environment please note, that the Kendo UI for jQuery widgets are not officially supported in the Angular 2+ environment. You could read more in the article linked below:

- https://docs.telerik.com/kendo-ui/third-party/angular2

I would recommend asking on the Kendo UI for Angular forums: https://www.telerik.com/forums/kendo-angular-ui. This way you could get some suggestions and possible workarounds from community members that have faced similar scenarios.

I hope you will find the provided information helpful.

Regards,
Neli
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DropDownList
Asked by
Amol
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Luiz
Top achievements
Rank 1
Magendran
Top achievements
Rank 1
Neli
Telerik team
Share this question
or