[英]Show or Hide MVC WebGrid Column with Jquery
I have a MVC Webgrid with columns that I want to Hide or Show dynamically based on a javascript function after the grid is rendered. 我有一个MVC Webgrid,其中包含要在渲染网格后基于javascript函数动态隐藏或显示的列。
So after the WebGrid is displayed I want to call a javascript function and pass in a ColumnName to Show or hide. 因此,在显示WebGrid之后,我想调用一个javascript函数并传递一个ColumnName来显示或隐藏。 Is this possible? 这可能吗?
Thanks, 谢谢,
This is how my grid is setup: 这是我的网格的设置方式:
@{
var gridColumns = new List<WebGridColumn>();
gridColumns.Add(grid.Column("Id", "Test ID"));
gridColumns.Add(grid.Column("Description", "Test Description"));
gridColumns.Add(grid.Column("ProjectDate", "Test Date", format: @<text> <span>@item.ProjectDate.ToString("d/M/yyyy")</span></text>));
}
<div id="divWebGrid" class="row">
@grid.GetHtml(
fillEmptyRows: false,
tableStyle: "webgrid",
alternatingRowStyle: "webgrid-alternating-row",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
mode: WebGridPagerModes.All,
columns: grid.Columns(gridColumns.ToArray())
)
</div>
And this would be my Javascript code: 这就是我的Javascript代码:
function ToggleColumn(columnName)
{
//Some Code to Show or Hide the Column in the WebGrid based on the columnName parameter.
}
Is this possible? 这可能吗?
Here is the rendered code of my grid: 这是我的网格的渲染代码:
<table class="webgrid" data-swhgajax="true" data-swhgcontainer="divWebGrid" data-swhgcallback="">
<thead>
<tr class="webgrid-header">
<th scope="col">
<a data-swhglnk="true" href="/Home/Index?gridItemsort=Id&gridItemsortdir=ASC">Test ID</a> </th>
<th scope="col">
<a data-swhglnk="true" href="/Home/Index?gridItemsort=Description&gridItemsortdir=ASC">Test Description</a> </th>
<th scope="col">
<a data-swhglnk="true" href="/Home/Index?gridItemsort=ProjectDate&gridItemsortdir=ASC">Test Date</a> </th>
</tr>
</thead>
<tfoot>
<tr class="webgrid-footer">
<td colspan="3">1 <a data-swhglnk="true" href="/Home/Index?gridItempage=2">2</a> <a data-swhglnk="true" href="/Home/Index?gridItempage=3">3</a> <a data-swhglnk="true" href="/Home/Index?gridItempage=2">></a> <a data-swhglnk="true" href="/Home/Index?gridItempage=3">>></a></td>
</tr>
</tfoot>
<tbody>
<tr class="webgrid-row-style">
<td>1</td>
<td>test 1</td>
<td> <span>22/7/2016</span></td>
</tr>
<tr class="webgrid-alternating-row">
<td>2</td>
<td>test 2</td>
<td> <span>22/7/2016</span></td>
</tr>
<tr class="webgrid-row-style">
<td>3</td>
<td>test 3</td>
<td> <span>22/7/2016</span></td>
</tr>
</tbody>
</table>
I faced the similar problem in my Grid. 我在网格中遇到了类似的问题。 And I solved it with the help of css class added on WebGrid columns. 然后我借助WebGrid列上添加的CSS类解决了该问题。
First, I add a class to all those columns which needs to be hidden. 首先,我向所有需要隐藏的列添加一个类。
gridColumns.Add(grid.Column("ID", "Test ID", null, "hidden-column"));
Added hide property on class 'hidden-column' 在类“ hidden-column”上添加了hide属性
<style>
.hidden-column {
display:none;
}
</style>
It will hide all the columns (td elements) having this class set (in this case, it will hide "ID" column). 它将隐藏设置了该类的所有列(td元素)(在这种情况下,它将隐藏“ ID”列)。
To hide Grid headers as well, I called IIFE function on document ready. 为了隐藏网格标题,我在准备好文档时调用了IIFE函数。
(function () {
$('#divWebGrid tbody tr:first').find('td.hidden-column').each(function (i, td) {
var indexOfHiddenColumn = $(td).index();
$('#divWebGrid thead th').eq(indexOfHiddenColumn).addClass('hidden-column');
});
}());
Hope this post will help you to resolve your issue. 希望这篇文章能帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.