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

Update Dropdownlist item text on Open event

3 Answers 561 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Anamika
Top achievements
Rank 1
Anamika asked on 04 Jun 2014, 01:55 PM
Hello ,

I have a Kendo dropdownlist which has items like "Hello Mr. %1 ", "Welcome %1" etc. I Display in kendowindow a TextBox for Username Input and then this Dropdown. When i enter a Name in username field and then cloick on Dropdown i should replace %1 with username in the Open Event so Dropdown Displays Hello Mr. Username. I would like to achieve this in JQuery.
$("#Briefanrede").data("kendoDropDownList").bind("open", function () {
 
});

Thanks

Anamika

3 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 06 Jun 2014, 08:26 AM
Hello Anamika,

In order to achieve this you have to modify the dataSource records using the set method of the model. Please check the following example:
$("#dropdown").kendoDropDownList({
    optionLabel: "Select Greeting",
    dataTextField: "text",
    dataValueField: "value",
    dataSource: {
        data: [
            { text: "Hello %1", value: "hello" },
            { text: "Welcome %1", value: "welcome" },
        ]
    }
});
 
$("#dropdown").data("kendoDropDownList").one("open", function() {
    var name = "Jack",
        data = this.dataSource.data(); //dropdown's data
 
    for (var i = 0; i < data.length; i++) {
        data[i].set("text", data[i].text.replace("%1", name)); //set the new text
    }
});


Note that the set method will force the DropDownList to refresh, the solution is not recommended in case you have a very long list.

I hope this will help.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Anamika
Top achievements
Rank 1
answered on 10 Jun 2014, 06:59 AM
Hello Alexander,

Thank you for the post and it works perfectly. But it just updates the Combobox text once, not evertime i open. I did Change the Event like this one to bind, Looks like evrytime i open Dropdown it fires the evnt but the text does not get updated with changed text of input
$("#dropdown").data("kendoDropDownList").bind("open", function() {  
var name = $('input:text[name=txtLastName]').val();
 
data = this.dataSource.data(); //dropdown's data     
for (var i = 0; i < data.length; i++) {        
data[i].set("text", data[i].text.replace("%1", name)); //set the new text    
}});

I Need to refresh text everytime i open the Combobox, not just once

Thanks

Anamika
0
Alexander Valchev
Telerik team
answered on 10 Jun 2014, 08:12 AM
Hi Anamika,

Your code looks OK. Could you please provide a small but runnable trykendoui sample page that demonstrates the issue so I can review it and advice you accordingly?

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
DropDownList
Asked by
Anamika
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Anamika
Top achievements
Rank 1
Share this question
or