在MVC(子标题)中使用WebGrid对列进行分组

时间:2013-09-09 10:53:22

标签: asp.net-mvc asp.net-mvc-3 razor webgrid

在MVC中使用WebGrid时是否可以对列进行分组? 我能找到的只是一个使用子网格的例子,但它将重复每个单元格的子标题,如Razor Nested WebGrid

@{
    var data = Enumerable.Range(0, 10).Select(i => new { Index = i, SubItems = new object[] { new { A = "A" + i, B = "B" + (i * i) } } }).ToArray();
    WebGrid topGrid = new WebGrid(data);
}

@topGrid.GetHtml(columns:
    topGrid.Columns(
        topGrid.Column("Index"),
        topGrid.Column("SubItems", format: (item) =>
        {
            WebGrid subGrid = subGrid = new WebGrid(item.SubItems);
            return subGrid.GetHtml(
                    columns: subGrid.Columns(
                        subGrid.Column("A"),
                        subGrid.Column("B")
                    )
                );
        })
    )
)

这会产生类似这样的东西

____________________
| index |  sub item|
|_______|__________|
|    0  |  A    B  |
|       |  1    2  |
|_______|__________|
|    1  |  A    B  |
|       |  1    2  |
|_______|__________|
|    2  |  A    B  |
|       |  1    2  |
|_______|__________|

但我正在寻找的是:

____________________
| index |  sub item|
|_______|__________|
|       |  A |  B  |
|_______|____|_____|
|   0   |  1 |  2  |
|   1   |  1 |  2  |
|   2   |  1 |  2  |
|_______|____|_____|

1 个答案:

答案 0 :(得分:0)

我不知道我的解决方案有多优雅,但它有效......

您的项目需要HTMLAgilityPack(从此处获取:http://htmlagilitypack.codeplex.com/

然后创建一个Web网格,并将Html字符串存储在这样的变量中,例如。请务必设置displayHeader:false,因为我们将创建自己的自定义标头:

@{
        WebGrid additionalTrainingGrid = new WebGrid(source: AdditionalMasterPlanOptimizationStatistics);
         var additionalTrainingGridHTML = additionalTrainingGrid.GetHtml(displayHeader:false,
            columns: additionalTrainingGrid.Columns(
                additionalTrainingGrid.Column("MasterPlanName", "MasterPlanName", style: "P1MasterPlanName"),
                additionalTrainingGrid.Column("P1ScheduledRequests", "P1 Scheduled Requests", style: "P1ScheduledRequests"),
                additionalTrainingGrid.Column("P1UnscheduledRequests", "P1 Unscheduled Requests", style: "P1UnscheduledRequests"),

                additionalTrainingGrid.Column("P2ScheduledRequests", "P2 Scheduled Requests", style: "P2ScheduledRequests"),
                additionalTrainingGrid.Column("P2UnscheduledRequests", "P2 Unscheduled Requests", style: "P2UnscheduledRequests"),

                additionalTrainingGrid.Column("P3ScheduledRequests", "P3 Scheduled Requests", style: "P3ScheduledRequests"),
                additionalTrainingGrid.Column("P3UnscheduledRequests", "P3 Unscheduled Requests", style: "P3UnscheduledRequests")
            )
        );
    }

使用纯HTML创建所需的标头,并使用HtmlAgilityPack从WebGrid中提取所有元素:

<table>
    <thead>
        <tr>
            <th colspan="1" rowspan="2" class="MasterPlanName">Master Plan Name</th>
            <th colspan="2">Plan 1</th>
            <th colspan="2">Plan 2</th>
            <th colspan="2">Plan 3</th>
        </tr>
        <tr class="sub-header">
            <th class="ScheduledRequests plan1">Scheduled Requests</th>
            <th class="UnscheduledRequests plan1">Unscheduled Requests</th>

            <th class="ScheduledRequests plan2">Scheduled Requests</th>
            <th class="UnscheduledRequests plan2">Unscheduled Requests</th>

            <th class="ScheduledRequests plan3">Scheduled Requests</th>
            <th class="UnscheduledRequests plan3">Unscheduled Requests</th>
        </tr>
    </thead>
    <tbody>
        @{ 
            HtmlDocument additionalTrainingGridDOC = new HtmlDocument();
            additionalTrainingGridDOC.LoadHtml(additionalTrainingGridHTML.ToString());

            var AdditioanlTrs = additionalTrainingGridDOC.DocumentNode.SelectNodes("//tr");
            if (AdditioanlTrs != null)
            {   
                foreach (var item in AdditioanlTrs)
                {
                @Html.Raw(item.OuterHtml);
                }
            }
        }

    </tbody>
</table>​