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

Set DropDownList SelectedName with AngularJS

2 Answers 68 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Jayme
Top achievements
Rank 1
Jayme asked on 09 Nov 2015, 06:17 PM

I have a drop down list that gets populated with a CompanyID and CompanyName:

HTML:

  <select id="MerchantList" kendo-drop-down-list="MerchantList" ng-model="MerchantID" k-options="populateCompanies" class="form-control" required validationmessage="Please select a company"></select>​​

 AngularJS:

 //populate the kendo drop down control
    $scope.populateCompanies = {
        dataSource: merchantDataSource,
        dataTextField: "CompanyName",
        dataValueField: "MerchantID",
        //optionLabel: "Please Select Your Company",
        filter: "contains",
        minLength: 3
    };

I also have a user record that contains the CompanyName: $scope.user.CompanyName

How do I select the CompanyName in the drop down list?  

Many thanks,

Jayme

 ​

2 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 10 Nov 2015, 11:41 AM
Hi Jayme,

The requirement that you have could not be easily achieved, because the selected will be performed based on the value of the DropDownList items and not the text. Nevertheless, you can use the approach from the following dojo example to manually find and select an item by its text:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Jayme
Top achievements
Rank 1
answered on 11 Nov 2015, 10:17 AM

Thank you for taking the time to reply. This doesn't quite fit the scenario where I want to preset the drop down to the text on initial load, however.  If you remove the ProductID and set the productnametoselect initially, it isn't selected.

This can be accomplished in both JQUERY and .NET code, so I was assuming there would be something simliar built into the Kendo Drop DownList.  Examples:

.NET:
ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));) 

JQUERY:
var theText = "Apples";
$("#ddlFruit option").each(function() {
if($(this).text() == theText) {
            $(this).attr('selected', 'selected');            
          }                        
});

I appreicate you letting me know this isn't really built into the kendo drop down control. I thought I was missing something easy.  For now, I display the current company for the user and prompt the user to switch to a different company in the drop down box if they want to change it.  Not ideal, but it works.

 

 

Tags
DropDownList
Asked by
Jayme
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Jayme
Top achievements
Rank 1
Share this question
or