简体   繁体   English

嵌套的WebGrid,MVC5

[英]Nested WebGrid, MVC5

Good day, I'm working with MVC 5, trying to create a nested WebGrid , following the tutorial did not work ( http://dotnetawesome.com/mvc/Nested-WebGrid-With-Expand-Collapse-in-MVC4 ) I managed to "update" the jquery code , however when I click on a row , it does not expand , it vanishes . 美好的一天,我正在使用MVC 5,尝试创建嵌套的WebGrid,但该教程不起作用( http://dotnetawesome.com/mvc/Nested-WebGrid-With-Expand-Collapse-in-MVC4 )我设法“更新”了jQuery代码,但是当我单击一行时,它没有展开,就消失了。 Follow my code: 按照我的代码:

Controller : Just select on a table and return shipping to the view. 控制器:只需在表格上选择,然后将运送返回视图即可。

VIEW: Jquery: 查看:jQuery:

$(document).ready(function () {
        var size = $("#DivGrid #result > thead > tr >th").size(); // get total column
        $("#DivGrid #result > thead > tr >th").last().remove(); // remove last column
        $("#DivGrid #result > thead > tr").prepend("<th></th>"); // add one column at first for collapsible column
        $("#DivGrid #result > tbody > tr").each(function (i, el) {
            $(this).prepend(
                    $("<td></td>")
                    .addClass("expand")
                    .addClass("hoverEff")
                    .attr('title',"click for show/hide")
                );

            //Now get sub table from last column and add this to the next new added row
            var table = $("table", this).parent().html();
            //add new row with this subtable
            $(this).after("<tr><td></td><td style='padding:5px; margin:0px;' colspan='" + (size - 1) + "'>" + table + "</td></tr>");
            $("table", this).parent().remove();
            // ADD CLICK EVENT FOR MAKE COLLAPSIBLE
            $("#DivGrid").on("click", "#result tbody tr", function () {
                console.log("HOVEREFF");
                $(this).parent().closest("tr").next().slideToggle(100);
                $(this).toggleClass("expand collapse");
            });
        });

        //by default make all subgrid in collapse mode
        $("#DivGrid #result > tbody > tr td.expand").each(function (i, el) {
            $(this).toggleClass("expand collapse");
            $(this).parent().closest("tr").next().slideToggle(100);
        });


HTML

 <div id="WebGrid">
            @{
                WebGrid grid = new WebGrid(Model, rowsPerPage: 100, ajaxUpdateContainerId: "result");
            }
            <div id="HeadTabela">
                <table class="tHead">
                    <tr>
                        <td class="Col2P">
                            <label>COD UNIDADE</label>
                        </td>
                        <td class="Col2P">
                            <label>COD GRUPO</label>
                        </td>
                        <td class="Col6P">
                            <label>DES GRUPO</label>
                        </td>
                        <td class="Col2P">
                            <label>ATIVO</label>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="DivGrid">
                @grid.GetHtml(
                        htmlAttributes: new { id = "result" },
                        tableStyle: "grid",
                        headerStyle: "head",
                        alternatingRowStyle: "alt",
                    columns: new[] {
                            grid.Column("DESCRIÇÃO GRUPO", canSort: false, format:
                            @<text>  <span>
                                         <label id="lblCodigo">@item.DES_ASSUNTO</label>
                                         <label id="lblGrupo">@item.COD_USUARIO_REMETENTE</label>
                                    <label id="lblDesGrupo">@item.COD_USUARIO_DESTINATARIO</label>
                                </span></text>, style: "Col6P"),
                            grid.Column(format:(a)=>{
                            WebGrid subGrid = new WebGrid(source: Model);
                            return subGrid.GetHtml(
                                htmlAttributes: new { id="subT" },
                                columns:subGrid.Columns(
                                        subGrid.Column("DESCRIÇÃO GRUPO", canSort: false, format:
                            @<text>  <span>
                                    <label id="lblDesGrupo">@a.DES_MENSAGEM</label>
                                </span></text>, style: "Col6P")
                                    )                    
                                );
                        })}
                    )
            </div>
        </div>
// ADD CLICK EVENT FOR MAKE COLLAPSIBLE
            $("#DivGrid").on("click", "#result tbody tr", function () {
                console.log("HOVEREFF");
                $(this).parent().closest("tr").next().slideToggle(100);

                $(this).toggleClass("expand collapse"); // Change name of style class "collapse", its being used by default css file.
            });
        });

        //by default make all subgrid in collapse mode
        $("#DivGrid #result > tbody > tr td.expand").each(function (i, el) {
            //$(this).toggleClass("expand collapse"); // Change name of style class "collapse", its being used by default css file.
            $(this).parent().closest("tr").next().slideToggle(100);
        });



Change name of style class "collapse", its being used by default css file.

Regards, Naveed.

Hi are you sure that the subGrid have data? 嗨,您确定subGrid包含数据吗? you are using the same Model for the main grid and subgrid. 您将相同的模型用于主网格和子网格。 That seems a little odd to me. 我觉得这有点奇怪。 Please post the rendered HTML that you have as result, to see if I can help you to find the error. 请发布结果所呈现的HTML,以查看我是否可以帮助您找到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM