我的cshtml文件在模型中接收一些bool值(+数据来构建webgrid)。根据bool值,必须显示一些webgrid列,并隐藏一些,例如我有:
webgrid中的Name
,Surname
,Job
列,但我有SurnameBool = 0
,这意味着只能显示Name
和Job
列。我试图使用其他答案中的代码,但遗憾的是我是JavaScript的新手,所以请您建议我如何基于display : none
结果将css @if(ColumnNameBool)
属性添加到webgrid中的列。
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")
)
)
}
答案 0 :(得分:1)
您应该使用null source创建网格:
WebGrid grid = new WebGrid<Your item type>(null, canSort: false, rowsPerPage: 15);
使用来源绑定网格:
grid.Bind(item2.ListOfWorkData, rowCount: <total row count>, autoSortAndPage: false);
根据ColumnNameBool值创建您的列集:
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.GetHtml(<styles>, columns: gridColumns.ToArray());