Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Below is my code for the same.
I am using a telerik controls.

What i want do is ,when i search in the combo ,if only one result exists for the search
then the combo should auto select it.Currently i have to select it.
below is my snippet....
<div class="form-combo" style="float: left">
                       @(Html.Kendo().ComboBoxFor(model => model.CardCode)//ComboBoxFor(model => model.CardCode)
                             .Name("CardCode")

                             .DataTextField("CustomerNameWithCode")
                             .DataValueField("CustomerId")
                             .Filter("contains")
                             .HtmlAttributes(new {style = "width:250px;"})
                             .AutoBind(true)
                             .MinLength(2)
                             .Events(ev =>
                             {
                                 ev.Select("SelectCustomer");
                                 ev.Change("ChangeCustomer");
                                 ev.Close("CloseCombo");

                             }

                             )
                             .DataSource(source =>
                             {
                                 source.Read(read =>
                                 {
                                     read.Action("GetAllCustomers", "Order").Data("additionalDataCustomer");
                                 });
                                 source.ServerFiltering(true);
                             })
                             )
                       <a style="cursor: pointer" onclick=" OpenCustomerPopUp() "><img src="~/Images/search.png" /></a>&nbsp;
                   </div>


function SelectCustomer() {
    document.getElementById('divAddNew').style.display = "block";
    //alert($('#CardCode').data('kendoComboBox').value());
    comboValue = $('#CardCode').data('kendoComboBox').value();
}

function ChangeCustomer() {


    comboValue = $('#CardCode').data('kendoComboBox').value();
    SaveCustomerToSessionForSelect();
}

function CloseCombo() {

    if ($('#CardCode').data('kendoComboBox').text() == 'No matching customers') {
        $('#CardCode').data('kendoComboBox').text('');
    }
}

function additionalDataCustomer() {
    try {
        //alert($('#CardCode').data('kendoComboBox').input.val());
        var str = $('#CardCode').data('kendoComboBox').input.val();//"Hello World!";
        var n = str.length;
        //alert(n);
        if (str.length >= 2) {
            return { searchValue: $('#CardCode').data('kendoComboBox').input.val() };
        }
        return { searchValue: "" };

    } catch (e) {
        alert(e);
    }

}


I am probably stuck as to what i should i write in order to do the auto select....
any help would be appreciated.....
Posted
Updated 14-Dec-15 19:54pm
v3
Comments
Sinisa Hajnal 8-Dec-15 5:16am    
Why not check if there is only one before selecting all and filling the combo? You could also select first one regardless of their number - then the user can change the selection, but initial show will be for the first one.
Amit Karyekar 8-Dec-15 22:31pm    
i have kendo combo box where user can type customer id and search ,so when it has one result it should auto select that customer that is what the requirement is.I dont require the first selection by default.
Sinisa Hajnal 9-Dec-15 2:30am    
Then I don't know. Check if there is an event that tracks selections. If not, use keydown, keypress or keyup events to check the content of suggest box and if is showing only one entry, select it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900