简体   繁体   English

更改Kendo UI网格中的列

[英]Change a column in Kendo UI Grid

I have an ASP.NET MVC 5 application. 我有一个ASP.NET MVC 5应用程序。 Kendo UI grids calls to Web API controller to retrieve the data. Kendo UI将对Web API控制器的调用网格化以检索数据。 I have a requirement to change the contents of a column depending on what Fuel type car is. 我需要根据什么是Fuel类型的汽车来更改列的内容。 So on my model for the page I created a string for Fuel Type. 因此,在页面的模型上,我为Fuel Type创建了一个字符串。 In MVC controller this is then set as below: 然后在MVC控制器中将其设置如下:

model.FuelType = "Diesel"; //just hardcoded for testing in reality will be dynamically set by User in a Previous screen - could be set to Petrol in which case I need to change column data

On a partial view I have my Kendo UI Grid defined as below - also on the PartialView I have a html.hiddenfor to store the value of FuelType 在局部视图上,我的Kendo UI网格如下定义-在PartialView上,我还有一个html.hiddenfor来存储FuelType的值

  @(Html.Kendo().Grid<Car.Web.Models.CarViewModel>()
                          .Name("CarViewGrid")
                          .Columns(columns =>
                          {
                              columns.Bound(c => c.Make).Width(180);
                              columns.Bound(c => c.ModelNo).Width(280);
                              columns.Bound(c => c.EngineSize).Width(120);
                              columns.Bound(c => c.Colour).Width(65);
                              columns.Bound(c => c.MPG).Width(65);

                              columns.Bound(c => c.CarId)
                                  .Title("Include in Report")
                                  .ClientTemplate("<input type=\"checkbox\" name=\"selectedIds\" value=\"#=CarId#\" class=\"checkboxClass\" />")
                                      .Width(90)
                                      .Sortable(false); 
                          })
                          .Events(builder => builder.DataBound("gridDataBound"))
                          .HtmlAttributes(new { style = "height: 420px;" })
                          .Scrollable()
                          .Sortable(sortable => sortable
                              .AllowUnsort(true)
                              .SortMode(GridSortMode.MultipleColumn))
                          .Pageable(pageable => pageable
                              .PageSizes(true)
                              .ButtonCount(5))
                          .DataSource(dataSource => dataSource
                              .Ajax()
                             .Read(read => read.Url("api/Car/GetCarDetails").Type(HttpVerbs.Get).Data("GetCustId"))
                          )
                          )

I have left a line blank in my column and that is where I need to include a different column - if the FuelTyle is Petrol I need to have 我在列中保留了一行空白,因此我需要在其中添加其他列-如果FuelTyle是Petrol,则需要

columns.Bound(c => c.NumberSparkPlugs).Width(65);

but if the FuelType is Diesel I need to have 但是如果FuelType是Diesel,我需要

columns.Bound(c => c.Torque).Width(65);

Both of these are string properties on my CarViewModel. 这两个都是我的CarViewModel上的字符串属性。

Has anyone done something similar with Kendo UI Grid or can advise in the best approach to achieve this? 有没有人用Kendo UI Grid做过类似的事情,或者可以建议实现此目的的最佳方法?

I don't have a Kendo MVC project in VisualStudio to test this with, but can't you just use an if statement: 我在Visual Studio中没有Kendo MVC项目可以对此进行测试,但是您不能只使用if语句:

                      .Columns(columns =>
                      {
                          columns.Bound(c => c.Make).Width(180);
                          columns.Bound(c => c.ModelNo).Width(280);
                          columns.Bound(c => c.EngineSize).Width(120);
                          columns.Bound(c => c.Colour).Width(65);
                          columns.Bound(c => c.MPG).Width(65);

                          columns.Bound(c => c.CarId)
                              .Title("Include in Report")
                              .ClientTemplate("<input type=\"checkbox\" name=\"selectedIds\" value=\"#=CarId#\" class=\"checkboxClass\" />")
                                  .Width(90)
                                  .Sortable(false); 

// added code here...
                          if(Model.FuelType == "Petrol")
                              columns.Bound(c => c.NumberSparkPlugs).Width(65);
                          else
                              columns.Bound(c => c.Torque).Width(65);
                      })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM