Kendo DropDownList Shows DataValueField Prior to D

2020-08-01 06:09发布

问题:

The following code loads a Kendo DropDownList, but while the page renders it first shows the DataValueField prior to the DataTextField. It binds just fine to the DataTextField after a second, but I would not like to show the numeric value while it renders. Does anyone know of a way to make only the DataTextField value be shown and not the DataValueField for that first second while it renders?

@(Html.Kendo().DropDownList()
              .Name("SomeName")
              .DataTextField("SomeTextField")
              .DataValueField("SomeValueField")
              .DataSource(source => {
                  source.Read(read => {
                      read.Url(Url.ExtensionMethodThatReturnsURL("SomeAction", "SomeController"));
                  }).ServerFiltering(true);
              })
              .HtmlAttributes(new { @Class = "some-class" })
              .Value(businessKey.ToString())
              .Events(e => e.Change("Some.Javascript.onEventHandler"))
              .Deferred()
    )

回答1:

The problem is probably caused by the .Deferred() statement, which delays the widget initialization until the deferred scripts are rendered via

@Html.Kendo().DeferredScripts()

I assume there is something time consuming taking place between the rendering of the DropDownList textbox and the widget initialization. So you are seeing the numeric value inside the plain non-initialized textbox. I have two suggestions:

  • move the DeferredScripts() call closer to the DropDownList declaration, if possible.
  • if the above is not possible or does not yield the desired result, then hide the DropDownList temporarily until it is initialized.

For example:

DropDownList and JavaScript

@(Html.Kendo().DropDownList()
    .Name("products")
    .DataTextField("ProductName")
    .DataValueField("ProductID")
    .Filter("contains")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetProducts", "Home");
        })
        .ServerFiltering(true);
    })
    .Value("3")
    .Deferred()
    .HtmlAttributes(new { @class = "temporary-hidden" })
)

// ...Something else here...

@Html.Kendo().DeferredScripts()

<script>

    // place this script immediately after the DeferredScripts() call
    // and in a document.ready handler to ensure it is executed at the right time

    $(function () {
        $(".temporary-hidden").removeClass("temporary-hidden");
    })

</script>

CSS

.temporary-hidden
{
    visibility: hidden;
}

Contrary to display:none, the visibility:hidden style will make the DropDownList textbox occupy space on the screen even while hidden, so you will avoid flickering and layout readjustments.