Reputation: 89
My cshtml file receiving some bool values within a model (+data to build a webgrid). Depending on bool values some of webgrid columns must be shown and some hidden, for example i have:
Name
, Surname
, Job
columns in webgrid, but i have SurnameBool = 0
which means only Name
and Job
columns must be shown. I have tried to use code from other answers but unfortunately i am novice in JavaScript so please can you advice me how to add css display : none
property to columns within a webgrid, basing on @if(ColumnNameBool)
results.
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")
)
)
}
Upvotes: 0
Views: 713
Reputation: 3084
You should create grid with null source:
WebGrid grid = new WebGrid<Your item type>(null, canSort: false, rowsPerPage: 15);
Bind you grid with source:
grid.Bind(item2.ListOfWorkData, rowCount: <total row count>, autoSortAndPage: false);
Create your set of columns depending of ColumnNameBool value:
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(...);
}
Finally assign list of columns to grid:
@grid.GetHtml(<styles>, columns: gridColumns.ToArray());
Upvotes: 1