围绕网格视图或具有子网格视图的主题有很多问题。我考虑过这种方法但对我的目的来说太过分了。我能找到的最接近的问题就是这个问题:Grouped Gridview
不幸的是,尽管对于如何创建分组行有一些建议,但它并没有使它们可折叠。
我的要求是我希望用户看到分隔行的gridview,例如
- 第1组
数据1 |数据2 |数据3
数据1 |数据2 |数据3
数据1 |数据2 |数据3
- GROUP 2
数据1 |数据2 |数据3
数据1 |数据2 |数据3
- 第3组
数据1 |数据2 |数据3
数据1 |数据2 |数据3
数据1 |数据2 |数据3
数据1 |数据2 |数据 3个
如果用户想要这样的观点,那么用户就可以:
+ GROUP 1
- GROUP 2
数据1 |数据2 |数据3
数据1 |数据2 |数据3
- 第3组
数据1 |数据2 |数据3
数据1 |数据2 |数据3
数据1 |数据2 |数据3
数据1 |数据2 |数据 3个
或者这个:
+ GROUP 1
+ GROUP 2
+ GROUP 3
基本上所有分组行中都包含该组的标题。它们甚至不是真正合适的Gridview行。实际的行是gridview,并且不需要任何进一步的向下钻取功能。
我希望我的解决方案是可行的客户端,我有限制,我可以使用javascript或jQuery(包括jQuery-ui 1.8.8),但不能随意扩展我正在使用的AJAX工具包的数量。我宁愿不必通过多组扩展回发来不断管理页面状态。
这是可以实现的吗?有人能指出我可能会给我一个轻推的资源方向吗?
编辑:哦,是的,我忘了提。基础gridview的行偶尔会有控件,包括但不限于:按钮,文本框,复选框和下拉列表。答案 0 :(得分:8)
由于您未提供实际代码,因此我根据this other question汇总了一个如何完成所需内容的示例。
另一个问题只是将文件放在服务器的驱动器C上,并按网格中的降序按创建时间对它们进行分组。所以这是转发器标记:
<asp:HiddenField ID="dataGroups" runat="server" />
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_RowDataBound" >
<ItemTemplate>
<!-- Bind to your specific properties i.e. Invoice #, file type, etc. -->
<table id="tableItem" runat="server">
<tr>
<td style="width: 200px;">
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblDirName" runat="server" Text='<%#Eval("DirectoryName") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblCreationTime" runat="server" Text='<%#Eval("CreationTime") %>'></asp:Label>
</td>
<td style="50px">
<asp:Button ID="btnAction" runat="server" Text="Hit me" OnClick="btnAction_Click"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
以下是OnRowDataBound
事件背后的代码;用C#编写,因为你使用的是:
private int month = -1;
private int year = -1;
protected void rpt_RowDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Binding to FileInfo objects.
//Since we are grouping by CreationTime we need to check if it's time to create a new "group"
//Is current month and year different from the value previously stored on the month and year variables?
if (month != (e.Item.DataItem as FileInfo).CreationTime.Month || year != (e.Item.DataItem as FileInfo).CreationTime.Year)
{
month = (e.Item.DataItem as FileInfo).CreationTime.Month;
year = (e.Item.DataItem as FileInfo).CreationTime.Year;
//append the current group to the hidden variable "dataGroups" which will tell us quickly how many groups we have in total
dataGroups.Value += (e.Item.DataItem as FileInfo).CreationTime.ToString("yyyMM") + ",";
}
//for every row; "stamp it" with this attribute since we'll use it on the client side with jQuery
(e.Item.FindControl("tableItem") as HtmlTable).Attributes.Add("data-group", (e.Item.DataItem as FileInfo).CreationTime.ToString("yyyMM"));
}
}
现在在客户端;我们需要做一些jQuery魔术来构建可折叠面板。
<link href="css/flick/jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.8.22.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
//asp hidden element containing the list of groups separated by commas.
//Check code behind of RowDataBound to see where is this populated
var dataGroups = $('#<%=dataGroups.ClientID%>').val().split(',');
for (var i = 0; i < dataGroups.length; i++) {
//split() doesn't have an option to ignore empty strings so we'll just ignore it
if (dataGroups[i] != '') {
//select all table elements with the data-group value matching the
//current group we are iterating over and enclose them all
//inside a div; effectively creating a "group"
$('table').filter(function (inputs) {
return $(this).data('group') == dataGroups[i];
}).wrapAll("<div class='accordion'>");
}
}
var accordions = $('.accordion');
//now, for every div enclosing the groups, create a Handle that will work as the element that
//collapses or expands the group
$(accordions).wrapInner("<div>").prepend('<h3><a href="#">Handle</a></h3>');
//Now replace the word "Handle" above for the actual group number/name or what have you
for (var i = 0; i < accordions.length; i++) {
$(accordions[i]).find('h3 a').text("Group " + $(accordions[i]).find('table:first').data('group'));
}
//finally call jQuery.accordion to create the accordions on every group
$('.accordion').accordion({ collapsible: true, autoHeight: false });
});
</script>
现在,这些代码行产生了这个: