A line of js Code! Implement a simple table

Source: Internet
Author: User
I started school on Monday, and I went back to retake the logistics test. If you don't talk much about it, just go to the code. Well, there are four lines, but you can see that it is still one line .. [Javascript] varmyTablenewTable ({title: & amp; #39; University for four years & amp; #39;, data: data,... syntaxHi started school on Monday and went back to retake logistics.


If you don't talk much about it, just go to the code. Well, there are four lines, but you can see that it is still one line ..

[Javascript] var myTable = new Table ({
Title: 'four years of university ',
Data: data,
Thead: thead
});



 

Data is the data in the table. We don't need to say anything about thead.
Data is the data in the table. We don't need to say anything about thead.

 

[Javascript] var data = [

['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world']
]
Var thead = ['heading 1', 'heading 2', 'heading 3', 'heading 4', 'heading 5', 'heading 6']
Var data = [

['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world']
]
Var thead = ['heading 1', 'heading 2', 'heading 3', 'heading 4', 'heading 5', 'heading 6']

 

Of course, every time you want to create such a table in a row, you need to encapsulate it. The Code is as follows. It is very random to write, without careful scrutiny, comments, and error tolerance, come on.

The Ext table that is obviously imitated.

[Javascript]
 
 
[Javascript] myExt = {};
MyExt. util = {};
MyExt.util.css = {};
MyExt.util.css. applyCss = function (src ){
Var link = document. createElement ('link ')
Link. rel = "stylesheet ";
Link. type = 'text/css ';
Link. href = src;
Document. getElementsByTagName ('head') [0]. appendChild (link );
};

Function Table (config ){
This. title = config. title;
This. data = config. data;
This. thead = config. thead;
This. show ();
}
Table. prototype. show = function (){
Var table = document. createElement ('table ');
Var tbody = document. createElement ('tbody ');
Var cap = document. createElement ('caption ');
Cap. appendChild (document. createTextNode (this. title ));
Table. appendChild (cap );
Table. appendChild (tbody );
Var thead = document. createElement ('tr ');
Tbody. appendChild (thead );
For (var m = 0; this. thead [m]; m ++ ){
Var th = document. createElement ('th ');
Th. appendChild (document. createTextNode (this. thead [m]);
Thead. appendChild (th );
}
Var rows = this. data. length;
Var cols = this. data [0]. length;
For (var I = 0; I Var tr = document. createElement ('tr ');
Tbody. appendChild (tr );
For (var j = 0; j If (j ){
Var td = document. createElement ('td ');
Td. appendChild (document. createTextNode (this. data [I] [j]);
Tr. appendChild (td );
} Else {
Var td = document. createElement ('th ');
Td. appendChild (document. createTextNode (this. data [I] [j]);
Tr. appendChild (td );
}
}
}
MyExt.util.css.applyCss('table.css ');
Document. body. appendChild (table );
}

Var data = [

['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world']
]
Var thead = ['heading 1', 'heading 2', 'heading 3', 'heading 4', 'heading 5', 'heading 6']


Var myTable = new Table ({
Title: 'four years of university ',
Data: data,
Thead: thead
});
MyExt = {};
MyExt. util = {};
MyExt.util.css = {};
MyExt.util.css. applyCss = function (src ){
Var link = document. createElement ('link ')
Link. rel = "stylesheet ";
Link. type = 'text/css ';
Link. href = src;
Document. getElementsByTagName ('head') [0]. appendChild (link );
};
 
Function Table (config ){
This. title = config. title;
This. data = config. data;
This. thead = config. thead;
This. show ();
}
Table. prototype. show = function (){
Var table = document. createElement ('table ');
Var tbody = document. createElement ('tbody ');
Var cap = document. createElement ('caption ');
Cap. appendChild (document. createTextNode (this. title ));
Table. appendChild (cap );
Table. appendChild (tbody );
Var thead = document. createElement ('tr ');
Tbody. appendChild (thead );
For (var m = 0; this. thead [m]; m ++ ){
Var th = document. createElement ('th ');
Th. appendChild (document. createTextNode (this. thead [m]);
Thead. appendChild (th );
}
Var rows = this. data. length;
Var cols = this. data [0]. length;
For (var I = 0; I Var tr = document. createElement ('tr ');
Tbody. appendChild (tr );
For (var j = 0; j If (j ){
Var td = document. createElement ('td ');
Td. appendChild (document. createTextNode (this. data [I] [j]);
Tr. appendChild (td );
} Else {
Var td = document. createElement ('th ');
Td. appendChild (document. createTextNode (this. data [I] [j]);
Tr. appendChild (td );
}
}
}
MyExt.util.css.applyCss('table.css ');
Document. body. appendChild (table );
}
 
Var data = [

['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world'],
['Sad ', 'Eat', 'Sleep ', 'playing play', 'learn Java', 'Watch this world']
]
Var thead = ['heading 1', 'heading 2', 'heading 3', 'heading 4', 'heading 5', 'heading 6']
 
 
Var myTable = new Table ({
Title: 'four years of university ',
Data: data,
Thead: thead
}); [Javascript]
[Javascript]
[Javascript]

  
 
Finally, I will give you a canvas pie chart of pure js and forget the flash.

 

From songzheng_741


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.