如何按需使用Kendo Grid Ajax Read事件

时间:2012-10-22 21:19:56

标签: telerik telerik-grid telerik-mvc kendo-ui

我有DropDownList和Telerik的Kendo UI Grid Control页面。当第一次打开页面时,DropDownList中没有选择任何项目。当用户只选择DropDownList中的值时,Grid应该对服务器进行Ajax调用并加载相应的数据。

当用户选择DropDownList中的项目时,我的代码工作正常,但问题是第一次打开页面时DropDownList中没有值,Grid不应该调用服务器。

我的问题是,如果DropDowList中没有选择任何项目,如何防止网格不能调用服务器?

    <div>
@Html.Kendo().DropDownList().Name("broker").DataTextField("GrandParentName").DataValueField("Id").BindTo(Model).SelectedIndex(@selectedIndex).Events(e => e.Change("brokerChanged"))
    </div>

    @(Html.Kendo().Grid<OrderViewModel>()
          .Name("Orders")
          .HtmlAttributes(new {style = "height: 500"})
          .Columns(c =>
              {
                  c.Bound(p => p.Id)
                      .Width(50)
                      .Title("")
                      .Sortable(false)
                      .IncludeInMenu(false)
                      .ClientTemplate((@Html.ActionLink("Edit", "Index", "Splits", new {Id = "OrderId"}, null).ToHtmlString().Replace("OrderId", "#=Id#")));
                  c.Bound(p => p.TradeDate)
                      .Title("Trd Dt")
                      .Format("{0:d}")
                      .Width(90)
                      .HtmlAttributes(new {style = "text-align: right"});
                  c.Bound(p => p.Price)
                      .Title("Price")
                      .Format("{0:n}")
                      .Width(100)
                      .HtmlAttributes(new {style = "text-align: right"});
                  c.Bound(p => p.Status)
                      .Title("Status");
                  c.Bound(p => p.Notional)
                      .Title("Notional")
                      .Format("{0:n}")
                      .HtmlAttributes(new {style = "text-align: right"});
              })
          .Sortable()
          .Scrollable()
          .ColumnMenu()
          .Pageable(x =>
              {
                  x.Enabled(true);
                  x.PreviousNext(false);
                  x.PageSizes(false);
                  x.Info(true);
                  x.Input(false);
                  x.Numeric(false);
                  x.Refresh(true);
                  x.Messages(y => y.Display("{2} Order(s)"));
              })
          .Resizable(resize => resize.Columns(true))
          .Reorderable(reoder => reoder.Columns(true))
          .DataSource(ds => ds.Ajax()
                            .ServerOperation(false)
                            .Read(read => read.Action("Action", "MyController").Data("selectedBrokerId")))
          )

<script type="text/javascript">
    function brokerChanged() {
        var grid = $("#Orders").data("kendoGrid");
        grid.dataSource.read();
    }

    function selectedBrokerId() {
        var obj = { brokerId: $("#broker").data("kendoDropDownList").value() };

        return obj;
    }
</script>

非常感谢你的时间和帮助。

1 个答案:

答案 0 :(得分:4)

网格有autobind功能。您可以使用它来确定是否在首次加载页面时进行读取。这应该有效(假设selectedIndex确定是否选择了下拉值):

@(Html.Kendo().Grid<OrderViewModel>()
      .Name("Orders")
      .HtmlAttributes(new {style = "height: 500"})
      .AutoBind(selectedIndex > 0) 
      //rest of your grid declaration