javascript配合DataGrid操作常用函数

本文介绍了一种使用JavaScript实现在DataGrid中的Checkbox列全选、反选及取消选择的功能。通过具体示例代码展示了如何操作DOM元素来更新Checkbox状态,并保持与全选按钮的一致性。
摘要由CSDN通过智能技术生成
javascript实现datagrid客户端checkbox列的全选,反选
作者:未知
来源:网络
/**//* 分页
    参数说明:
    prefix:前缀;chkAll:全选框;chkSingle:单选框ID
   
    使用方法:
    if(e.Item.ItemType == ListItemType.Header)
    {
        ((CheckBox)e.Item.Cells[1].FindControl("chkAll")).Attributes.Add("onclick","CheckAll('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle');");
    }
*/
function CheckAll(prefix,chkAll,chkSingle)
{
    var indexChkAll;
    if(prefix.length != 0)
    {
        indexChkAll        = prefix + "__ctl2_" + chkAll;
    }
    else
    {
        indexChkAll        = chkAll;
    }
    var objChkAll = do***ent.getElementById(indexChkAll);
    var tempObj;
    for(var i=0;i<do***ent.forms[0].elements.length;i++)
    {   
        tempObj = do***ent.forms[0].elements Idea;
        if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
        {
            tempObj.checked = objChkAll.checked;
        }
    }
}
/**//* 分页
    参数说明:
    prefix:前缀;chkAll:全选框;chkSingle:单选框ID
   
    使用方法:
    if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        ((CheckBox)e.Item.Cells[1].FindControl("chkSingle")).Attributes.Add("onclick","CheckSingle('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle');");
    }
*/
function CheckSingle(prefix,chkAll,chkSingle)
{
    var indexChkAll;
    if(prefix.length != 0)
    {
        indexChkAll        = prefix + "__ctl2_" + chkAll;
    }
    else
    {
        indexChkAll        = chkAll;
    }
    var objChkAll = do***ent.getElementById(indexChkAll);
    var tempObj;
    var allCount    = 0;
    var checkCount    = 0;
    for(var i=0;i<do***ent.forms[0].elements.length;i++)
    {   
        tempObj = do***ent.forms[0].elements Idea;
        if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
        {
            if(tempObj.checked)
            {
                checkCount++;
            }
            else
            {
                objChkAll.checked = false;
                //break;
            }
            allCount++;
        }
    }
    if(checkCount != allCount)
    {
        objChkAll.checked = false;
    }
    else
    {
        if(allCount != 0)
        {
            objChkAll.checked = true;
        }
    }
}
/**//*
    参数说明:
    prefix:前缀;chkAll:全选框;chkSingle:单选框ID
    type:1【全选】,2【反选】,3【取消】
   
    使用方法:
    this.btnSelectAll.Attributes.Add("onClick","CheckType('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle',1);");
    this.btnUnSelectAll.Attributes.Add("onClick","CheckType('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle',2);");
    this.btnCancelSelect.Attributes.Add("onClick","CheckType('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle',3);");
*/
function CheckType(prefix,chkAll,chkSingle,type)
{
    var indexChkAll;
    if(prefix.length != 0)
    {
        indexChkAll        = prefix + "__ctl2_" + chkAll;
    }
    else
    {
        indexChkAll        = chkAll;
    }
    var objChkAll = do***ent.getElementById(indexChkAll);
    var tempObj;
    var allCount    = 0;
    var checkCount    = 0;
    for(var i=0;i<do***ent.forms[0].elements.length;i++)
    {   
        tempObj = do***ent.forms[0].elements Idea;
        if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
        {
            switch(type)
            {
                case 1:
                    tempObj.checked = true;
                    break;
                case 2:
                    tempObj.checked = !tempObj.checked;
                    break;
                case 3:
                    tempObj.checked = false;
                    break;
            }
            if(tempObj.checked)
            {
                checkCount++;
            }
            allCount++;
        }
    }
    if(checkCount != allCount)
    {
        objChkAll.checked = false;
    }
    else
    {
        if(allCount != 0)
        {
            objChkAll.checked = true;
        }
    }
    window.event.returnValue = false;
    return false;
}

/**//*
    参数说明:
    prefix:前缀;chkAll:全选框;chkSingle:单选框ID

    使用方法:
    this.btnDelete.Attributes.Add("onClick","SubmitCheckBox('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle');");
*/
function SubmitCheckBox(prefix,chkAll,chkSingle,msg)
{
    var indexChkAll;
    if(prefix.length != 0)
    {
        indexChkAll        = prefix + "__ctl2_" + chkAll;
    }
    else
    {
        indexChkAll        = chkAll;
    }
    var objChkAll = do***ent.getElementById(indexChkAll);
   
    var tempObj;
    var allCount    = 0;
    var checkCount    = 0;
    for(var i=0;i<do***ent.forms[0].elements.length;i++)
    {   
        tempObj = do***ent.forms[0].elementsIdea;
        if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
        {
            if(tempObj.checked)
            {
                checkCount++;
            }
            allCount++;
        }
    }
    if(allCount == 0) //没有数据
    {
        window.alert("当前没有" + msg + "可供删除");
        window.event.returnValue = false;
        return false;
    }
    else
    {
        if(checkCount == 0)
        {
            window.alert("没有选中要删除的" + msg + "");
            window.event.returnValue = false;
            return false;
        }
        else
        {
            //if(window.confirm("确定要删除当前选中的【" + checkCount.toString() + "】项吗?") == false)
            if(window.confirm("确定要删除当前选中的" + msg + "吗?") == false)
            {
                window.event.returnValue = false;
                return false;
            }
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值