Web网格控件,允许至少3个级别

时间:2017-07-11 18:40:37

标签: javascript jquery gridview html-table htmltable-control

我已经在网上看到了我认为存在的东西,但我找不到它......

我想在网页上使用标准的javascript / JSON / JQuery类型技术创建以下内容:

enter image description here

注意:3个细节级别,而不仅仅是2

我正在寻找一个让我这样做的网格控件 - (它不需要管理总计/总计)

赞赏资源的任何帮助或指示。

2 个答案:

答案 0 :(得分:1)

您可以使用jquery树表(http://ludo.cubicphuse.nl/jquery-treetable/

答案 1 :(得分:0)

试试这个:

.Aspx代码

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="CustomerID">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:ImageButton ID="imgOrdersShow" runat="server" OnClick="Show_Hide_OrdersGrid" ImageUrl="~/images/plus.png"
                CommandArgument="Show" />
            <asp:Panel ID="pnlOrders" runat="server" Visible="false" Style="position: relative">
                <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" PageSize="5"
                    AllowPaging="true" OnPageIndexChanging="OnOrdersGrid_PageIndexChanging" CssClass="ChildGrid"
                    DataKeyNames="OrderId">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton ID="imgProductsShow" runat="server" OnClick="Show_Hide_ProductsGrid" ImageUrl="~/images/plus.png"
                                    CommandArgument="Show" />
                                <asp:Panel ID="pnlProducts" runat="server" Visible="false" Style="position: relative">
                                    <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" PageSize="2"
                                        AllowPaging="true" OnPageIndexChanging="OnProductsGrid_PageIndexChanging" CssClass="Nested_ChildGrid">
                                        <Columns>
                                            <asp:BoundField ItemStyle-Width="150px" DataField="ProductId" HeaderText="Product Id" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="ProductName" HeaderText="Product Name" />
                                        </Columns>
                                    </asp:GridView>
                                </asp:Panel>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
                    </Columns>
                </asp:GridView>
            </asp:Panel>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
    <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
</Columns>

.cs代码:1级

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
    gvCustomers.DataSource = GetData("select top 10 * from Customers");
    gvCustomers.DataBind();
}
}

private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = query;
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
    }
}
}

.cs代码:第2级

protected void Show_Hide_OrdersGrid(object sender, EventArgs e)
{
ImageButton imgShowHide = (sender as ImageButton);
GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
if (imgShowHide.CommandArgument == "Show")
{
    row.FindControl("pnlOrders").Visible = true;
    imgShowHide.CommandArgument = "Hide";
    imgShowHide.ImageUrl = "~/images/minus.png";
    string customerId = gvCustomers.DataKeys[row.RowIndex].Value.ToString();
    GridView gvOrders = row.FindControl("gvOrders") as GridView;
    BindOrders(customerId, gvOrders);
}
else
{
    row.FindControl("pnlOrders").Visible = false;
    imgShowHide.CommandArgument = "Show";
    imgShowHide.ImageUrl = "~/images/plus.png";
}
}

private void BindOrders(string customerId, GridView gvOrders)
{
gvOrders.ToolTip = customerId;
gvOrders.DataSource = GetData(string.Format("select * from Orders where CustomerId='{0}'", customerId));
gvOrders.DataBind();
}

protected void OnOrdersGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvOrders = (sender as GridView);
gvOrders.PageIndex = e.NewPageIndex;
BindOrders(gvOrders.ToolTip, gvOrders);
}

.cs代码:第3级

protected void Show_Hide_ProductsGrid(object sender, EventArgs e)
{
ImageButton imgShowHide = (sender as ImageButton);
GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
if (imgShowHide.CommandArgument == "Show")
{
    row.FindControl("pnlProducts").Visible = true;
    imgShowHide.CommandArgument = "Hide";
    imgShowHide.ImageUrl = "~/images/minus.png";
    int orderId = Convert.ToInt32( (row.NamingContainer as GridView).DataKeys[row.RowIndex].Value);
    GridView gvProducts = row.FindControl("gvProducts") as GridView;
    BindProducts(orderId, gvProducts);
}
else
{
    row.FindControl("pnlProducts").Visible = false;
    imgShowHide.CommandArgument = "Show";
    imgShowHide.ImageUrl = "~/images/plus.png";
}
}

private void BindProducts(int orderId, GridView gvProducts)
{
gvProducts.ToolTip = orderId.ToString();
gvProducts.DataSource = GetData(string.Format("SELECT ProductId, ProductName FROM Products WHERE ProductId IN (SELECT ProductId FROM [Order Details] WHERE OrderId = '{0}')", orderId));
gvProducts.DataBind();
}

protected void OnProductsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvProducts = (sender as GridView);
gvProducts.PageIndex = e.NewPageIndex;
BindProducts(int.Parse(gvProducts.ToolTip), gvProducts);
}

使用jQuery的脚本:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    $("[id*=imgOrdersShow]").each(function () {
        if ($(this)[0].src.indexOf("minus") != -1) {
            $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
            $(this).next().remove();
        }
    });
    $("[id*=imgProductsShow]").each(function () {
        if ($(this)[0].src.indexOf("minus") != -1) {
            $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
            $(this).next().remove();
        }
    });
});
</script>

CSS样式:

<style type="text/css">
.Grid td
{
    background-color: #A1DCF2;
    color: black;
    font-size: 10pt;
    line-height: 200%;
}
.Grid th
{
    background-color: #3AC0F2;
    color: White;
    font-size: 10pt;
    line-height: 200%;
}
.ChildGrid td
{
    background-color: #eee !important;
    color: black;
    font-size: 10pt;
    line-height: 200%;
}
.ChildGrid th
{
    background-color: #6C6C6C !important;
    color: White;
    font-size: 10pt;
    line-height: 200%;
}
.Nested_ChildGrid td
{
    background-color: #fff !important;
    color: black;
    font-size: 10pt;
    line-height: 200%;
}
.Nested_ChildGrid th
{
    background-color: #2B579A !important;
    color: White;
    font-size: 10pt;
    line-height: 200%;
}
</style>

链接1: https://www.aspsnippets.com/Articles/Multilevel-N-Level-Nested-GridView-GridView-inside-GridView-in-ASPNet-with-Paging.aspx

链接2: http://www.aspforums.net/Threads/194011/Mulitlevel-nested-GridView-with-expand-and-collapse-using-jQuery-in-ASPNet/