如何根据 kendo 网格中的条件隐藏一列

How to hide one column based on condition in kendo grid

下面是我的 kendo 网格,我需要有条件地隐藏案例编号列,这意味着 if(admin == true) 我需要显示此列,否则我需要隐藏此列我该怎么办这个

@(Html.Kendo().Grid(Model.GiIncidentReportList)
.Name("IRGrid").Columns(columns => {
  columns.Bound(r => r.IncidentReport).Title("Case Number");
  columns.Bound(r => r.IncidentCreatedByName).Title("Created By");
  columns.Bound(r => r.IncidentCreatedDateTime).Title("Created Date");
  columns.Bound(r => r.IncidentUpdatedByName).Title("Updated By");
  columns.Bound(r => r.IncidentUpdatedDateTime).Title("Updated Date");
  columns.Template(p => 
    @Html.ActionLink("Delete","DeleteIncidentReport","IncidentReport",
                     new { incidentReportId = p.IncidentReport.IR_IncidentID, dlLogId = p.IncidentReport.DL_LogID, incidentType = p.IncidentReport.IT_IncidentType }, 
                     new { @class = "k-button k-button-icontext", onclick = "return confirm('Are you sure you wish to delete this report?')" }).ToHtmlString()

    );
  })
)

我试过的

if(admin == true){
  var grdView = $('#IRGrid').data('kendoGrid');
  grdView.hideColumn("IncidentReport"); //By Using Columns Name.
}

它正在工作,但我只想在 columns.bound 处理显示和隐藏,而不是使用 if 条件。

你可以通过@Viewbag传递Value,然后给出这样的条件

@(Html.Kendo().Grid(Model.GiIncidentReportList)
.Name("IRGrid").Columns(columns => {
 if (@ViewBag.admin == "True")
 {
  columns.Bound(r => r.IncidentReport).Title("Case Number");
  }
  columns.Bound(r => r.IncidentCreatedByName).Title("Created By");
  columns.Bound(r => r.IncidentCreatedDateTime).Title("Created Date");
  columns.Bound(r => r.IncidentUpdatedByName).Title("Updated By");
  columns.Bound(r => r.IncidentUpdatedDateTime).Title("Updated Date");
  columns.Template(p => 
    @Html.ActionLink("Delete","DeleteIncidentReport","IncidentReport",
                     new { incidentReportId = p.IncidentReport.IR_IncidentID, dlLogId = p.IncidentReport.DL_LogID, incidentType = p.IncidentReport.IT_IncidentType }, 
                     new { @class = "k-button k-button-icontext", onclick = "return confirm('Are you sure you wish to delete this report?')" }).ToHtmlString()

    );
  })
)

让您的管理员 属性 在您的模型中并使用 .Hidden(@Model.admin) 属性 显示隐藏列

@(Html.Kendo().Grid(Model.GiIncidentReportList)
.Name("IRGrid").Columns(columns => {
  columns.Bound(r => r.IncidentReport).Title("Case Number").Hidden(@Model.admin);
  columns.Bound(r => r.IncidentCreatedByName).Title("Created By");
  columns.Bound(r => r.IncidentCreatedDateTime).Title("Created Date");
  columns.Bound(r => r.IncidentUpdatedByName).Title("Updated By");
  columns.Bound(r => r.IncidentUpdatedDateTime).Title("Updated Date");
  columns.Template(p => 
    @Html.ActionLink("Delete","DeleteIncidentReport","IncidentReport",
                     new { incidentReportId = p.IncidentReport.IR_IncidentID, dlLogId = p.IncidentReport.DL_LogID, incidentType = p.IncidentReport.IT_IncidentType }, 
                     new { @class = "k-button k-button-icontext", onclick = "return confirm('Are you sure you wish to delete this report?')" }).ToHtmlString()

    );
  })
)