Javascript: select all the checkbox columns of the datagrid client. deselect _ Javascript tutorial

Source: Internet
Author: User
Javascript: select all the checkbox columns of the datagrid client, invert selection, Javascript tutorial

Simplest format: This is a clever method, but it is not universal. The premise is that this page has only one datagrid and only the datagrid has a checkbox. This is more convenient. The main idea is to search for the checkbox of the entire page and select all or invert them.
// Select all
Function allCheck ()
{
For (var I = 0; I {
Var e = Form1.elements [I];
If (e. type = 'checkbox ')
E. checked = true;
}

}
// Invert Selection
Function revCheck ()
{
For (var I = 0; I {
Var e = Form1.elements [I];
If (e. type = 'checkbox ')
E. checked =! E. checked;
}
}


Common simple format
Because the checkbox in the datagrid is generated on the asp.net page and its ID needs to be changed, we can find this control accurately by looking for their rules, in this way, you can select and unselect all resources,
Parameter description:
Prefix: prefix; s: Select box ID; chk: Select box ID;


Function getObj (objID)
{
Return document. getElementById (objID );
}

// Select all
Function _ SelectAll (prefix, s, chk)
{
Var oArr = _ GetColl (prefix, s, chk );
For (var o in oArr)
{
OArr [o]. checked = true;
}
}
// Invert Selection
Function _ RevSelect (prefix, s, chk)
{
Var oArr = _ GetColl (prefix, s, chk );
For (var o in oArr)
{
OArr [o]. checked =! OArr [o]. checked;
}
}

// Obtain the value
Function _ GetColl (prefix, s, chk)
{
Var I = s;
Var oArr = new Array ();
While (true)
{
Var o = getObj (prefix + '_ ctl' + I + '_' + chk );
If (o! = Null)
{
OArr. push (o );
}
Else
{
Break;
}
I ++;
}
 
Return oArr;
}
// Check whether selected
Function _ CheckSelect (prefix, s, chk)
{
Var oArr = _ GetColl (prefix, s, chk );
For (var o in oArr)
{
If (oArr [o]. checked)
{
Return true;
}
}
Return false;
 
}

Recommended General Format: http://www.cnblogs.com/ghd258/archive/2005/11/07/270449.html#Post
/* Paging
2 parameter description:
3 prefix: prefix; chkAll: select all; chkSingle: single Region ID
4
5 usage:
6 if (e. Item. ItemType = ListItemType. Header)
7 {
8 (CheckBox) e. item. cells [1]. findControl ("chkAll ")). attributes. add ("onclick", "CheckAll ('" + this. dg. clientID. toString () + "', 'chall', 'chksing ');");
9}
10 */
11 function CheckAll (prefix, chkAll, chkSingle)
12 {
13 var indexChkAll;
14 if (prefix. length! = 0)
15 {
16 indexChkAll = prefix + "_ ctl2 _" + chkAll;
17}
18 else
19 {
20 indexChkAll = chkAll;
21}
22 var objChkAll = document. getElementById (indexChkAll );
23 var tempObj;
24 for (var I = 0; I 25 {
26 tempObj = document. forms [0]. elements [I];
27 if (tempObj. type = "checkbox" & tempObj. id! = IndexChkAll & tempObj. id. indexOf (chkSingle )! =-1)
28 {
29 tempObj. checked = objChkAll. checked;
30}
31}
32}
33/** // * Page
34 parameter description:
35 prefix: prefix; chkAll: select all; chkSingle: single Region ID
36
37 usage:
38 if (e. Item. ItemType = ListItemType. AlternatingItem | e. Item. ItemType = ListItemType. Item)
39 {
40 (CheckBox) e. item. cells [1]. findControl ("chkSingle ")). attributes. add ("onclick", "CheckSingle ('" + this. dg. clientID. toString () + "', 'chall', 'chksing ');");
41}
42 */
43 function CheckSingle (prefix, chkAll, chkSingle)
44 {
45 var indexChkAll;
46 if (prefix. length! = 0)
47 {
48 indexChkAll = prefix + "_ ctl2 _" + chkAll;
49}
50 else
51 {
52 indexChkAll = chkAll;
53}
54 var objChkAll = document. getElementById (indexChkAll );
55 var tempObj;
56 var allCount = 0;
57 var checkCount = 0;
58 for (var I = 0; I 59 {
60 tempObj = document. forms [0]. elements [I];
61 if (tempObj. type = "checkbox" & tempObj. id! = IndexChkAll & tempObj. id. indexOf (chkSingle )! =-1)
62 {
63 if (tempObj. checked)
64 {
65 checkCount ++;
66}
67 else
68 {
69 objChkAll. checked = false;
70 // break;
71}
72 allCount ++;
73}
74}
75 if (checkCount! = AllCount)
76 {
77 objChkAll. checked = false;
78}
79 else
80 {
81 if (allCount! = 0)
82 {
83 objChkAll. checked = true;
84}
85}
86}
87 /**//*

[1] [2] Next page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.