javascript配合DataGrid操作常用函數

來源:互聯網
上載者:User
datagrid|javascript|函數 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;
        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;
        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;
        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].elements;
        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;
            }
        }
    }
}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.