ASP.NET mvc hiding webgrid columns
本文关键字:webgrid columns hiding mvc NET ASP | 更新日期: 2023-09-27 18:08:57
我的cshtml文件接收模型内的一些bool值(+数据构建webgrid)。根据bool值,一些webgrid列必须显示和隐藏,例如我有:Name
, Surname
, Job
列在webgrid,但我有SurnameBool = 0
,这意味着只有Name
和Job
列必须显示。我曾尝试使用其他答案的代码,但不幸的是,我是JavaScript新手,所以请告诉我如何在webgrid中添加css display : none
属性到列,基于@if(ColumnNameBool)
结果。
WebGrid grid = new WebGrid(item2.ListOfWorkData, canSort: false, rowsPerPage: 15);
<div id="divWebGrid" class="row">
@if (item2.ListOfWorkData.Any())
{
@grid.GetHtml(
tableStyle: "table",
headerStyle: "table_HeaderStyle",
footerStyle: "table_PagerStyle",
rowStyle: "table_RowStyle",
alternatingRowStyle: "table_AlternatingRowStyle",
selectedRowStyle: "table_SelectedRowStyle",
columns: grid.Columns(
grid.Column("ProjectName", @Resources.Localization.project, format: @<text>
@if (Model.ColumnsNeeded.ProjectNameBool)
{
<span class="display-mode"><label id="ProjectNameLabel">@item.ProjectName</label></span>
}
</text>,style : "hidden-column"),
grid.Column("Activity", @Resources.Localization.activity, format: @<text>
@if (Model.ColumnsNeeded.ActivityBool)
{
<span class="display-mode"><label id="ActivityLabel">@item.Activity</label></span>
}
</text>, style: "p60"),
grid.Column("ProjectEndDate", @Resources.Localization.start_date, format: @<text>
@if (Model.ColumnsNeeded.ProjectStartDateBool)
{
<span class="display-mode"><label id="ProjectStartDate">@item.ProjectStartDate</label></span>
}
</text>, style: "p60"),
grid.Column("ProjectEndDate", @Resources.Localization.end_date, format: @<text>
@if (Model.ColumnsNeeded.ProjectEndDateBool)
{
<span class="display-mode"><label id="ProjectEndDate">@item.ProjectEndDate</label></span>
}
</text>, style: "p60")
)
)
}
你应该创建一个null来源的网格:
WebGrid grid = new WebGrid<Your item type>(null, canSort: false, rowsPerPage: 15);
用source:
绑定你的网格grid.Bind(item2.ListOfWorkData, rowCount: <total row count>, autoSortAndPage: false);
根据columnnamepool值创建列集:
var gridColumns = new List<WebGridColumn>();
@if(ColumnNameBool)
{
gridColumns.Add(grid.Column("ProjectName",
@Resources.Localization.project, format: @<text>
@if (Model.ColumnsNeeded.ProjectNameBool)
{
<span class="display-mode">
<label id="ProjectNameLabel">
@item.ProjectName</label>
</span>
}
</text>,style : "hidden-column"));
)
//... add other required columns here
}
else
{
//create here another list of columns that required
gridColumns.Add(...);
}
最后将列列表分配给grid:
@grid.GetHtml(<styles>, columns: gridColumns.ToArray());