简体   繁体   English

在同一行中选中复选框时,请选中DataGrid-Row中的所有复选框

[英]Check All checkbox in datagrid-row when a checkbox is checked within same row

I am developing a webpage for changing Rights(Add/View/Delete/Edit/All) for a subadministrator for all modules in Project.Say, there are 5 modules.我正在为项目中所有模块的子管理员开发一个用于更改权限(添加/查看/删除/编辑/全部)的网页。比如说,有 5 个模块。 I have loaded current rights in datagrid for each module(in datagrid there are 5 rows(modules) and for that module 5 columns of rights in checkbox).我已经在 datagrid 中为每个模块加载了当前权限(在 datagrid 中有 5 行(模块),并且该模块在复选框中有 5 列权限)。 I need functionality like, when i check 'ALL' checkbox, for that (module), other checkbox for Add,View,Edit,Delete should be checked and when i uncheck 'ALL' checkbox, other checkbox should be unchecked.我需要这样的功能,当我选中“全部”复选框时,对于那个(模块),应该选中添加、查看、编辑、删除的其他复选框,当我取消选中“全部”复选框时,应该取消选中其他复选框。 Any solution accepted for javascript or any event. javascript 或任何事件接受的任何解决方案。 I tried with checkedchanged event, but it checkes/unchecks all the checkboxes of entire datagrid instead for that module(row).我尝试使用checkedchanged事件,但它检查/取消选中整个数据网格的所有复选框而不是该模块(行)。 In checkedchanged event i wrote this code..在checkedchanged事件中我写了这段代码..

foreach (DataGridItem dgitem in dg_rights.Items)
            {
                CheckBox chkall = (CheckBox)dgitem.FindControl("chk_all");
                CheckBox chkadd = (CheckBox)dgitem.FindControl("chk_add");
                CheckBox chkedit = (CheckBox)dgitem.FindControl("chk_edit");
                CheckBox chkview = (CheckBox)dgitem.FindControl("chk_view");
                CheckBox chkdelete = (CheckBox)dgitem.FindControl("chk_delete");
                if (chkall.Checked)
                {
                    chkadd.Checked = true;
                    chkedit.Checked = chkview.Checked = chkedit.Checked = chkdelete.Checked = true;
                }
                else
                {
                    chkadd.Checked = false;
                    chkedit.Checked = chkview.Checked = chkedit.Checked = chkdelete.Checked = false;
                }
            }

Looks like a am missing some basic thing or making a mistake.看起来我错过了一些基本的东西或犯了一个错误。 Any idea.任何想法。 Thanks Anish谢谢阿尼什

You can this jQuery:您可以使用此 jQuery:

$('#<%= YourGrid.ClientID %> input[id$=_chkAll]').click(
    function () {
        var all_checked = $(this).attr('checked') == 'checked';
        $(this).parent().parent().find('input').each(
            function (i,o) {
                o.checked = all_checked;
            }
        )
    }
)

http://jsfiddle.net/4u6gw/ http://jsfiddle.net/4u6gw/

For an ASP.NET server side solution, you have to do this:对于 ASP.NET 服务器端解决方案,您必须这样做:

  • set the chkall check box to autopostback将 chkall 复选框设置为自动回发
  • hook onto ItemsGrid_Command(Object sender, DataGridCommandEventArgs e) and use e to get the datarow and.FindControl to get the other check boxes挂钩 ItemsGrid_Command(Object sender, DataGridCommandEventArgs e) 并使用 e 获取数据行,使用.FindControl 获取其他复选框

I called function abcd with 'this' argument on 'onclick' for chk_all checkbox and i ended up with this and its working.我在 chk_all 复选框的“onclick”上使用“this”参数调用 function abcd,我最终得到了这个及其工作。 Here chk_ is ids of all checkboxes starting with chk_n all will be checked.这里 chk_ 是所有以 chk_n 开头的复选框的 id,所有将被检查。

function abcd(checkbox) {
            var cba = $("#" + checkbox.id);
            if (cba.is(':checked')) {
                //alert("check all")
                var cb = $("#" + checkbox.id);
                var td = cb.parent("td");
                var tr = td.parent("tr");
                tr.children().children("input:checkbox[id*=chk_]").attr('checked', cb.attr('checked'));
            }
            else {
                //alert("unchecked");
                var cb = $("#" + checkbox.id);
                var td = cb.parent("td");
                var tr = td.parent("tr");
                tr.children().children("input:checkbox[id*=chk_]").removeAttr('checked');
            }
        }

Thanks.谢谢。

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

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