【问题标题】:How to update KendoGrid from combobox如何从组合框更新 KendoGrid
【发布时间】:2015-08-05 07:58:31
【问题描述】:

我正在使用 Kendo UI 在 ASP.NET MVC 中编写一个 Web 应用程序。我正在将 Kendo Grid 中的数据可视化如下:

 @(Html.Kendo().Grid<MyModel>()
  .Name("grid")
  .DataSource(dataSource => dataSource // Configure the grid data source
      .Ajax() // Specify that ajax binding is used
      .Read(read => read.Action("ReadAction", "MyController", new { /*route values*/ }))
    )
  .Columns(columns =>
  {
      columns.Bound(n => n.Month).Title("Month").ClientTemplate("<input type='hidden' value='#=Month#' id='hfMonth'/>").Hidden();
      columns.AutoGenerate(true);          

  })
  .Pageable()
  .Sortable()

现在我需要根据&lt;select&gt; 的更改事件触发网格更新。我怎样才能做到这一点?我从昨天开始尝试了几种可能性,但都没有成功。

【问题讨论】:

  • 应用更改时。在 JS 函数中调用它$('#Grid').data('kendoGrid').dataSource.read();

标签: c# asp.net asp.net-mvc kendo-grid kendo-asp.net-mvc


【解决方案1】:

如果没有看到您的组合框代码,我会执行以下操作:

查看

@(Html.Kendo().ComboBox()
      .Name("combo")
      .DataTextField("Text")
      .DataValueField("Value")
      .BindTo(new List<SelectListItem>() {
          new SelectListItem() {
            Text = "Foo", Value = "1"   
          },
          new SelectListItem() {
            Text = "Bar", Value = "2"   
          },
          new SelectListItem() {
            Text = "Baz", Value = "3"   
          }
      })
  .Events(events =>
  {
    events.Change("onChange");
  })
)

JavaScript

function onChange(e) {

  var grid = $("#grid").data("kendoGrid");
  // if the selected value is not needed
  grid.dataSource.read();

  // if the selected value is needed use below instead
  // changing the route parameter to match yours
  var selectedValue = this.Value();
  grid.dataSource.read({ id : selectedValue });
}

更新

根据@PierpaoloIlConteParis cmets:

我没有在 read.Action 方法中直接指定参数,而是使用了 telerik.com/forums/… 这篇文章中的处理函数,现在当更改组合框值时,动作会使用正确的参数触发

【讨论】:

  • 非常感谢。数据源的动作需要一些参数。如何判断“操作需要具有此名称的参数”?现在我有这样的东西: .Read(read => read.Action("YearAggregation", "MyController", new { [at]year = 2015, [at]productId = 1 })) 2015 和 1 是默认值。我必须写 [at] 因为 Stackoverflow 不喜欢“at”符号。
  • @PierpaoloIlConteParis 没有问题。这里是这部分:grid.dataSource.read({ id : selectedValue }); 在这个例子中路由值是id,只需将其更改为您的参数名称。
  • 好的,一个变通方法就像一个魅力。我没有直接在 read.Action 方法中指定参数,而是使用了像这篇文章中的处理函数telerik.com/forums/… 现在,当更改组合框值时,操作会使用正确的参数触发。非常感谢。
  • @PierpaoloIlConteParis 不错,我会用你的 cmets 更新我的答案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多