0

I am using the 2016.3.914.440 version of kendo and have a question on the ComboBox. If my datasource only returns a single value, how can I assign that to the ComboBox so the user doesn't need to enter a value? I tried the .SelectIndex(0), but that works under all situations, so I thought in the .DataBound I would check for the record count and if = 1 then I wanted to assign to the ComboBox. The code I have is the following.

    @(Html.Kendo().ComboBox()
      .Name("FAList")
      .Placeholder("Select Fiscal Agency...")
      .DataTextField("Text")
      .DataValueField("Value")
      .HtmlAttributes(new { style = "width:50%;" })
      .Filter("startswith")
      .AutoBind(true)
      .MinLength(3)
      .DataSource(source =>
      {
          source.Read(read =>
          {
              read.Action("GetUserAgencyList", "Entities");
          })
          .ServerFiltering(true);
      })
          .Events(e => e
            .Change("onFAChange")
            .DataBound("onFADataBound")
            )

)

But I don't know how to finish the onFADataBound event

    function onFADataBound(e) {
    var dropdown = $("#FAList").data("kendoComboBox");
    var count = dropdown.dataSource.data().length

    alert('FA Count: ' + count)
}

So how would I find the text and value of the datasource record and assign that to the DataTextField and DataValueField.

Tom S
  • 227
  • 1
  • 6
  • 19

1 Answers1

1

You can select the first item via the select method.

function onFADataBound(e) {
    if (e.sender.dataSource.view().length == 1) {
        e.sender.select(1);
    }
}

Note that the selected item's index will be 1 if you have an optionLabel (placeholder) and 0 otherwise.

dimodi
  • 3,969
  • 1
  • 13
  • 23