如何给 kendo 组合框赋值

How to assign kendo combobox a value

我使用的是kendo的2016.3.914.440版本,对ComboBox有疑问。如果我的数据源只有 returns 一个值,我如何将它分配给 ComboBox 以便用户不需要输入值?我尝试了 .SelectIndex(0),但这在所有情况下都有效,所以我想在 .DataBound 中检查记录数,如果 = 1,则我想分配给 ComboBox。 我的代码如下。

    @(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")
            )

)

但是我不知道如何完成onFADataBound事件

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

    alert('FA Count: ' + count)
}

那么我如何找到数据源记录的文本和值并将其分配给 DataTextField 和 DataValueField。

您可以通过 select 方法 select 第一项。

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

请注意,如果您有 optionLabel(占位符),selected 项的索引将为 1,否则为 0