Create a webing table for the table object on the web page

Source: Internet
Author: User

The table in the HTML object is a commonly used webpage element. In DHTML programming, we can use its rows and cells Methods to conveniently access each cell in the table object, andTable object(Table)Unit row(TR) and eachCell(TD) has its own rowindex and cellindex attributes respectively.

Using the table object feature mentioned above, it seems very simple to browse cells horizontally or vertically in a table. For example, to browse cells in a column vertically, we only use:

< Script Language = "JavaScript" >
Function Browercolumn (TBL, col)
{
For ( VaR I = 0 ; I < TBL. Rows. length; ++ I)
{
VaRCell=TBL. Rows (I). cells (COL );
//Do something with cell
}
}
</ Script >

You can simply use the nextsibling and previussibling attributes of TD.

After talking about it for a long time, it seems that we haven't talked about how to create a ing for the table. The table cell browsing mentioned above is indeed feasible, but it can only solve table objects with rules, what is a rule table object? That is to say, the colspan and rowspan of each cell in the table must be the same. Of course, the default value is 1. Otherwise, the vertical browsing will crash.

We know that the way to merge cells in the table is to use xxxspan, then the cell in the merge direction will be removed from the xxxSpan-1 (xxxspan> 1) Based on the merged width. This is all clear. (If you do not know, describe this article.ArticleIt is not suitable for you to read), but after merging on the line, the cellindex value of the cell is still continuous, which leads to a problem, for example, the cellindex of each cell in the following table is actually the value @_@:

If we use the article againCodeBrowsing the cells in this table vertically will become ugly. So we can first create a matrix ing from cell to colcount * rowcount In the table, and then it is very easy to browse the cells in the two-dimensional matrix, whether vertical or horizontal (however, the xxxsibling attribute is recommended for horizontal browsing ). Matrix Effect


In this way, we can easily perform vertical browsing on this tablemap.

Why do we need to browse the cells in the table horizontally ?? When we are doing highlighting in some tables, This is a frequently used method, of course, the vertical and horizontal browsing of this table is the basis of the question 'use the arrow keys to navigate naturally in the input box matrix.

Code for the table cell ing matrix:

< Script Language = "JavaScript" >
Function Createtablemapping (TBL)
{
If ( ! TBL. rowcount)
{
VaR Tablemap = [];
VaR Colcount =   0 ;
For ( VaR I = 0 ; I < TBL. Rows ( 0 ). Cells. length; ++ I)
{
Colcount+ =TBL. Rows (0). Cells (I). colspan;
}
TBL. colcount = Colcount;
TBL. rowcount = TBL. Rows. length;
For ( VaR I = 0 ; I < TBL. Rows. length; ++ I)
{
VaR Ary =   New Array (colcount );
For ( VaR J = 0 ; J < Colcount; ++ J)
{
Ary [J]= True;
}
Tablemap. Push (ary );
}
For ( VaR I = 0 ; I < TBL. rowcount; ++ I)
{
VaR Cellindex =   0 ;
For ( VaR J = 0 ; J < Colcount; j + = Currentcell. colspan)
{
If (Tablemap [I] [J])
{
VaR Currentcell = TBL. Rows (I). cells (cellindex ++ );
For ( VaR M = I; m < I + Currentcell. rowspan; ++ M)
{
For ( VaR N = J; n < J + Currentcell. colspan; ++ N)
{
Tablemap [m] [N]= False;
}
}
Tablemap [I] [J] = Currentcell;
}
}
}
TBL. tablemap = Tablemap;
}
}
</ Script >

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.