Javascript hide/display ten thousand rows of table columns in 60 ms

Source: Internet
Author: User

To hide table columns, the most common method is as follows: CopyCode The Code is as follows: TD. style. Display = "NONE ";

This method is extremely inefficient. For example, hiding a column in a table of thousands of rows takes about 1.4 milliseconds to execute on my notebook (P4 M 4000g, M memory), which is intolerable. For example, the following code:Copy codeThe Code is as follows: <body>
<Input type = button onclick = hidecol (1) value = 'hide 2nd columns '>
<Input type = button onclick = showcol (1) value = 'display 2nd column'>
<Div id = tablebox> </div>
<SCRIPT type = "text/JavaScript"> <! --
//--------------------------------------------------------
// Convert time to timestamp (MS)
Function time2stamp () {var d = new date (); return date. parse (d) + d. getmilliseconds ();}

//--------------------------------------------------------
// Create a table
Function createtable (rowslen)
{
VaR STR = "<Table border = 1>" +
"<Thead>" +
"<Tr>" +
"<TH width = 100> col1 <\/Th>" +
"<TH width = 200> col2 <\/Th>" +
"<TH width = 50> col3 <\/Th>" +
"<\/Tr>" +
"<\/Thead>" +
"<Tbody> ";

VaR arr = [];
For (VAR I = 0; I <rowslen; I ++)
{
Arr [I] = "<tr> <TD>" + I + "1 <\/TD> <TD>" + I + "2 </TD> <TD>" + I + "3 <\/TD> </tr> ";
}
STR + = arr. Join ("") + "</tbody> <\/table>"; // you can use join () to quickly create strings.
Tablebox. innerhtml = STR; // generate a table
}

//--------------------------------------------------------
// Hide/show specified Columns
Function hidecol (codecomx) {hideorshowcol (codecomx, 0 );}
Function showcol (codecomx) {hideorshowcol (codecomx, 1 );}
//----------------------------
Function hideorshowcol (codecomx, isshow)
{
VaR T1 = time2stamp ();//
VaR table = tablebox. Children [0];
VaR rowslen = table. Rows. length;
VaR lasttr = table. Rows [0];
For (VAR I = 0; I <rowslen; I ++)
{
VaR TR = table. Rows [I];
Tr. Children [codecomx]. style. Display = isshow? "": "NONE ";
}

VaR t2 = time2stamp ();
Alert ("Time consumed:" + (T2-t1) + "millisecond ");
}

//--------------------------------------------------------
Createtable (1000); // create a thousand rows table
// --> </SCRIPT>

Unfortunately, Google uses this code to hide columns in JavaScript.
In fact, we can quickly hide columns by setting the TD or th width of the first row to 0.
Let's change the hideorshowcol () function to the following code:Copy codeThe Code is as follows: function hideorshowcol (codecomx, isshow)
{
VaR T1 = time2stamp ();//
VaR table = tablebox. Children [0];
VaR TR = table. Rows [0];
Tr. Children [codecomx]. style. width = isshow? 200: 0;

VaR t2 = time2stamp ();
Alert ("Time consumed:" + (T2-t1) + "millisecond ");
}

However, the hidden effect cannot be achieved only in this way. You also need to set the table and TD styles as follows:Copy codeThe Code is as follows: <style> <! --
Table
{
Border-collapse: collapse;
Table-layout: fixed;
Overflow: hidden;
}
TD
{
Overflow: hidden;
White-space: nowrap;
}
--> </Style> <style bogus = "1"> table
{
Border-collapse: collapse;
Table-layout: fixed;
Overflow: hidden;
}
TD
{
Overflow: hidden;
White-space: nowrap;
} </Style>

Test again, we found that hiding a column in a table with thousands of rows takes less than 15 milliseconds. In other words, createtable (10000) is used to create a ten-million-row table and then test the table. It only takes 60 milliseconds (the execution time in my notebook is used as a reference. In fact, most of you have a much higher computer configuration than my laptop, so the time is shorter.) The efficiency is very satisfactory.
Supplement:
According to impermanence's proposal, the code for colgroup processing is added. The strange thing is that, although the processing principle is the same, the processing time for colgroup is 140 milliseconds, Which is doubled. The reason is unclear.
Complete code: Copy code The Code is as follows: <style> <! --
Table
{
Border-collapse: collapse;
Table-layout: fixed;
Overflow: hidden;
}
TD
{
Overflow: hidden;
White-space: nowrap;
}
--> </Style> <style bogus = "1"> table
{
Border-collapse: collapse;
Table-layout: fixed;
Overflow: hidden;
}
TD
{
Overflow: hidden;
White-space: nowrap;
} </Style>
<Body>
<Input type = button onclick = createtable () value = 'create a table: Use thead'>
<Input type = button onclick = createtable (1) value = 'create a table: Use colgroup'>
<Br>
<Input type = button onclick = hidecol (1) value = 'hide 2nd columns '>
<Input type = button onclick = showcol (1) value = 'display 2nd column'>

<Input type = button onclick = hidecol_fast (1) value = 'hide 2nd columns quickly '>
<Input type = button onclick = showcol_fast (1) value = 'show 2nd columns quickly '>
<Div id = tablebox> </div>
<SCRIPT type = "text/JavaScript"> <! --
VaR tablerowslen = 10000; // create a ten-Row Table

//--------------------------------------------------------
// Convert time to timestamp (MS)
Function time2stamp () {var d = new date (); return date. parse (d) + d. getmilliseconds ();}

//--------------------------------------------------------
// Create a table
Function createtable (isusecolgroup)
{
If (isusecolgroup) // use the colgroup label
{
VaR STR = "<Table border = 1>" +
"<Colgroup>" +
"<Col width = 100/>" +
"<Col width = 200/>" +
"<Col width = 50/>" +
"<\/Colgroup>" +
"<Tbody> ";
}
Else
{
// Use thead label
VaR STR = "<Table border = 1>" +
"<Thead>" +
"<Tr>" +
"<TH width = 100> col1 <\/Th>" +
"<TH width = 200> col2 <\/Th>" +
"<TH width = 50> col3 <\/Th>" +
"<\/Tr>" +
"<\/Thead>" +
"<Tbody> ";
}

VaR arr = [];
For (VAR I = 0; I <tablerowslen; I ++)
{
Arr [I] = "<tr> <TD>" + I + "1 <\/TD> <TD>" + I + "2 </TD> <TD>" + I + "3 <\/TD> </tr> ";
}
STR + = arr. Join ("") + "</tbody> <\/table>"; // you can use join () to quickly create strings.
Tablebox. innerhtml = STR; // generate a table
}

//--------------------------------------------------------
// Hide/show specified Columns
Function hidecol (codecomx) {hideorshowcol (codecomx, 0 );}
Function showcol (codecomx) {hideorshowcol (codecomx, 1 );}
//----------------------------
Function hideorshowcol (codecomx, isshow)
{
VaR T1 = time2stamp ();//
VaR table = tablebox. Children [0];
VaR rowslen = table. Rows. length;
VaR lasttr = table. Rows [0];

If (rowslen> 1001)
{
If (! Confirm ("operations on tables with more than 1000 rows will be performed, which will be very time-consuming (or even cause the browser to die ). \ N are you sure you want to continue? "))
Return;
}

For (VAR I = 0; I <rowslen; I ++)
{
VaR TR = table. Rows [I];
Tr. Children [codecomx]. style. Display = isshow? "": "NONE ";
}

VaR t2 = time2stamp ();
Alert ("Time consumed:" + (T2-t1) + "millisecond ");
}

//--------------------------------------------------------
// Hide/show specified columns-fast
Function hidecol_fast (codecomx) {hideorshowcol_fast (codecomx, 0 );}
Function showcol_fast (codecomx) {hideorshowcol_fast (codecomx, 1 );}
//----------------------------
Function hideorshowcol_fast (codecomx, isshow)
{
VaR T1 = time2stamp ();//
VaR table = tablebox. Children [0];
VaR thead = table. Children [0]; // It may be thead, tbody, or colgroup.
If (thead. tagname. tolowercase () = "colgroup") // special processing for colgroup
{
VaR TD = thead. Children [codecomx];
}
Else
{
// Note: If the table does not have the thead and tbody labels, the table. Children [0] Is tbody.
VaR TR = thead. Children [0];
VaR TD = tr. Children [codecomx];
}
TD. style. width = isshow? 200: 0;

VaR t2 = time2stamp ();
Alert ("Time consumed:" + (T2-t1) + "millisecond ");
}

//--------------------------------------------------------
Createtable ();
// --> </SCRIPT>

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.