简体   繁体   English

如何使用 JavaScript 或 Jquery 选择在 GridView 中选择的行项的子项?

[英]How to select the sub-items of a row item selected in GridView by using JavaScript or Jquery?

I have a GridView in asp.net page like below:我在 asp.net 页面中有一个 GridView,如下所示:

Checkbox   Name          Link
[]         MainName      myLink
[]         -somename1    myLink1
[]         -somename2    myLink2
[]         --somename2.1 myLink2.1
[]         MainName2     mylink3
[]         -somename3    Mylink4

Note that in the naming pattern, the "Main Titles" do not include the dash character as a starting character, while the other titles have.请注意,在命名模式中,“主标题”不包括破折号字符作为起始字符,而其他标题则有。

What I want is that, when I click the checkbox near "MainName" I need all the sub rows that belongs to the selected MainName will also be selected automatically.我想要的是,当我单击“MainName”附近的复选框时,我需要自动选择属于所选 MainName 的所有子行。 Assuming I click the checkbox at the first row: MainName, then it should be seen:假设我点击了第一行的复选框:MainName,那么它应该被看到:

Checkbox          Name          Link
[checked]         MainName      myLink
[checked]         -somename1    myLink1
[checked]         -somename2    myLink2
[checked]         --somename2.1 myLink2.1
[]                MainName2     mylink3
[]                -somename3    Mylink4

I am managing it by using C# like below:我正在使用 C# 管理它,如下所示:

protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
    System.Web.UI.WebControls.CheckBox chk = sender as System.Web.UI.WebControls.CheckBox;
    GridViewRow row = (GridViewRow)chk.NamingContainer;
    bool checkVal = false;

    String cellText = row.Cells[2].Text;
    if (cellText.IndexOf('-') < 0)
    {
        foreach (GridViewRow r in dgMenuItems.Rows)
        {
            if (row == r)
            {
                checkVal = true;
            }

            if (checkVal)
            {
                if (r.Cells[2].Text.IndexOf('-') < 0)
                {
                    break;
                }
                else
                {
                    var cb = (System.Web.UI.WebControls.CheckBox)r.FindControl("chbSelect");
                    cb.Checked = true;
                }
            }

        }
    }
}

But I want to handle it in JavaScript or JQuery, not in the server side.但我想在 JavaScript 或 JQuery 中处理它,而不是在服务器端。 Any help or advice in this regard will be appreciated.在这方面的任何帮助或建议将不胜感激。

This is one way to do it.这是一种方法。

<script>
    $('.GridWithCheckBox input[type=checkbox]').change(function () {
        var $cb = $(this);

        //get the value of the cell to the right
        var $name = $cb.parent('td').next('td').html().trim();

        //check if the first value is not a '-'
        if ($name.substring(0, 1) !== '-') {

            //find all next rows and loop them
            $cb.closest('tr').nextAll().each(function () {

                //find the next cell value
                var $nextName = $(this).find('td:eq(1)').html().trim();

                //if it has a '-', check the checkbox
                if ($nextName.substring(0, 1) === '-') {
                    $(this).find('input[type=checkbox]').prop('checked', $cb.prop('checked'));
                } else {
                    //if not found stop the function
                    return false;
                }
            });
        }
    });
</script>

The GridView with source for demo带有演示源的 GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="GridWithCheckBox">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <%# Eval("Name") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Link">
            <ItemTemplate>
                <%# Eval("Link") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And the code behind以及背后的代码

DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Link", typeof(string));

table.Rows.Add(0, "MainName", "myLink");
table.Rows.Add(1, "-somename1", "myLink1");
table.Rows.Add(2, "-somename2", "myLink2");
table.Rows.Add(3, "--somename2.1", "myLink2.1");
table.Rows.Add(4, "MainName2", "mylink3");
table.Rows.Add(5, "-somename3", "Mylink4");
table.Rows.Add(6, "MainName3", "mylink5");
table.Rows.Add(7, "-somename4", "Mylink6");

GridView1.DataSource = table;
GridView1.DataBind();

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

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