set selected option of dropdownlist by value not by index

4 posts, 0 answers
  1. Henk Jelt
    Henk Jelt avatar
    19 posts
    Member since:
    Jun 2012

    Posted 21 Aug 2012 Link to this post


    UPDATE
    After two days of googling I found the solution in this example:
    http://jsfiddle.net/krustev/9qZnp/ 

    I adapted it to my own code, and it works perfectly.

    KR

    Henk Jelt

    END UPDATE

    Hi

    I have two dropdown lists.
    One contains a list of customers. 
    The second should contain a list of products for the selected customer.
    Both are populated by calls to json-webservices.
    The selection process works fine via postback (I cannot get cascading dropdownlists working...).

    It works like this:
    I select an option in the first dropdownlist, which is part of a form.
    Then I invoke a postback, retrieve the value of the selected item in the dropdownlist, and use this to dynamically adapt the json call to retrieve the data for the second dropdownlist.
    This works fine: the second dropdownlist is populated ok.

    The problem I have is that the selected item in the first dropdownlist gets lost.
    I want it to come back but have no clue how.

    I am working in an MVC environment, so I save the value in a viewdata-object, which I can re-use in the view.

    Which steps do I need to take to have the selected option in view?

    KR

    Henk Jelt Hoving
  2. Mk
    Mk avatar
    12 posts
    Member since:
    Aug 2012

    Posted 22 Oct 2012 Link to this post

    You can done it by using:-

    var dropdownlist = $("#dropdownlist").data("kendoDropDownList");

    // selects item if its text is equal to "test" using predicate function
    dropdownlist.select(function(dataItem) {
        return dataItem.text === "test";
    });

    // selects item if its value is equal to "test" using predicate function
    dropdownlist.select(function(dataItem) {
        return dataItem.value=== "test";
    });
  3. Gary Davis
    Gary Davis avatar
    71 posts
    Member since:
    Oct 2003

    Posted 26 Mar 2013 Link to this post

    Thanks - but use properties Text and Value instead of text and value.
  4. Graham
    Graham avatar
    35 posts
    Member since:
    Jul 2012

    Posted 10 Aug in reply to Mk Link to this post

    Thanks Mk.

     

    A "gotcha" - the datasource data item field name will not be called "text" if you have given a name to the DataTextField of the dropdown list - you will need to use the name you have given it instead.

Back to Top