테이블을 sort 하는 자바스크립트를 만들어봤습니다.
제가 이번에 만든 스크립트는 (언제나 그렇지만) 인라인스타일, 인라인스크립트를 일체 사용하지 않습니다.
특정 class의 태이블을 오름차순 혹은 내림차순으로 정렬합니다.
| 순번 |
제목 |
작가 |
출판사 |
| 1 |
감독부적격 |
Moyoco Anno |
대원씨아이 |
| 2 |
수수께끼 그녀 X 1 |
Ushiba Riichi |
학산문화사 |
| 3 |
수수께끼 그녀 X 2 |
Ushiba Riichi |
학산문화사 |
| 4 |
수수께끼 그녀 X 3 |
Ushiba Riichi |
학산문화사 |
| 5 |
미나미가 1 |
Coharu Sakuraba |
북박스 |
| 6 |
미나미가 2 |
Coharu Sakuraba |
북박스 |
| 7 |
미나미가 3 |
Coharu Sakuraba |
북박스 |
| 8 |
미나미가 4 |
Coharu Sakuraba |
북박스 |
| 9 |
미나미가 5 |
Coharu Sakuraba |
북박스 |
| 10 |
극락청춘 하키부 1 |
Ai Morinaga |
대원씨아이 |
| 11 |
극락청춘 하키부 2 |
Ai Morinaga |
대원씨아이 |
| 12 |
극락청춘 하키부 3 |
Ai Morinaga |
대원씨아이 |
| 13 |
극락청춘 하키부 4 |
Ai Morinaga |
대원씨아이 |
| 14 |
극락청춘 하키부 5 |
Ai Morinaga |
대원씨아이 |
| 15 |
극락청춘 하키부 6 |
Ai Morinaga |
대원씨아이 |
| 16 |
극락청춘 하키부 7 |
Ai Morinaga |
대원씨아이 |
| 17 |
극락청춘 하키부 8 |
Ai Morinaga |
대원씨아이 |
| 18 |
현시연 1 |
Kio Shimoku |
북박스 |
| 19 |
현시연 2 |
Kio Shimoku |
북박스 |
| 20 |
현시연 3 |
Kio Shimoku |
북박스 |
| 21 |
현시연 4 |
Kio Shimoku |
북박스 |
| 22 |
현시연 5 |
Kio Shimoku |
북박스 |
| 23 |
현시연 6 |
Kio Shimoku |
북박스 |
| 24 |
현시연 7 |
Kio Shimoku |
북박스 |
| 25 |
현시연 8 |
Kio Shimoku |
북박스 |
| 26 |
현시연 9 |
Kio Shimoku |
북박스 |
Download
table_sort.zip
Code
javascript
- function addLoadEvent(func) {
- var oldonLoad = window.onload;
- if (typeof window.onload != 'function') {
- window.onload = func;
- }else {
- window.onload = function(){
- oldonLoad();
- func();
- }
- }
- }
- function getElementsByClassName(clsName,parentNode) {
- var arr = new Array();
- if (parentNode == null) {
- var elems = document.getElementsByTagName("*");
- } else {
- var elems = parentNode.getElementsByTagName("*");
- }
- for ( var cls, i = 0; ( elem = elems[i] ); i++ ) {
- if ( elem.className == clsName ) {
- arr[arr.length] = elem;
- }
- }
- return arr;
- }
- addLoadEvent(sortTable);
- function sortTable(){
- var body_el = document.getElementsByTagName("body")[0];
- if (!getElementsByClassName("datatable",body_el))return false;
- var table_el = getElementsByClassName("datatable",body_el);
- for (i=0;i<table_el.length;i++){
- table_el[i]._tr = table_el[i].getElementsByTagName("tr");
- if (table_el[i]._tr[0].parentNode.nodeName!="THEAD")return false;
- for (j=0;j<table_el[i]._tr[0].childNodes.length;j++){
- if(table_el[i]._tr[0].childNodes[j].childNodes[0]){
- var a_el = document.createElement("a");
- a_el.href="#";
- table_el[i]._tr[0].childNodes[j].appendChild(a_el);
- a_el.appendChild(table_el[i]._tr[0].childNodes[j].childNodes[0]);
- table_el[i]._tr[0].childNodes[j].className = "button";
- table_el[i]._tr[0].childNodes[j]._number = j;
- table_el[i]._tr[0].childNodes[j]._switch = false;
- table_el[i]._tr[0].childNodes[j].onclick = function () {
- for (i=0;i<this.parentNode.childNodes.length;i++){
- this.parentNode.childNodes[i].className = "button";
- }
- if (this._switch == false){
- this._switch = true;
- sortTableABC(this._number,this.parentNode.parentNode.parentNode);
- this.className = "button up";
- return false;
- } else {
- this._switch= false;
- sortTableCBA(this._number,this.parentNode.parentNode.parentNode);
- this.className = "button down";
- return false;
- }
- }
- }
- }
- }
- }
- function sortTableABC(number,table_el){
- var tr_el = table_el.getElementsByTagName("tr");
- var txt = new Array();
- var txt_int = new Array();
- var sum = 0;
- for (i=1;i<tr_el.length;i++){
- txt[i] = tr_el[i].childNodes[number].childNodes[0].nodeValue;
txt_int[i] = parseInt(txt[i]);
- }
- for (i=1;i<txt_int.length;i++){
- sum = sum + txt_int[i];
- }
- txt = txt.sort();
- if (sum>0) {
- for (i=1;i<txt_int.length;i++){
- for (j=1;j<txt_int.length;j++){
- if (txt_int[i]<txt_int[j]) {
- var temp = txt_int[i];
- txt_int[i] = txt_int[j];
- txt_int[j] = temp;
- }
- }
- }
- txt = txt_int;
- }
- for (i=1;i<txt.length;i++){
- for (j=1;j<tr_el.length;j++) {
- if (txt[i]==tr_el[j].childNodes[number].childNodes[0].nodeValue){
- tr_el[i].parentNode.appendChild(tr_el[j]);
- }
- }
- }
- }
- function sortTableCBA(number,table_el){
- var tr_el = table_el.getElementsByTagName("tr");
- var txt = new Array();
- var txt_int = new Array();
- var sum = 0;
- for (i=1;i<tr_el.length;i++){
- txt[i] = tr_el[i].childNodes[number].childNodes[0].nodeValue;
- txt_int[i] = parseInt(txt[i]);
}
- for (i=1;i<txt_int.length;i++){
- sum = sum + txt_int[i];
- }
- txt = txt.sort();
- if (sum>0) {
- for (i=1;i<txt_int.length;i++){
- for (j=1;j<txt_int.length;j++){
- if (txt_int[i]<txt_int[j]) {
- var temp = txt_int[i];
- txt_int[i] = txt_int[j];
- txt_int[j] = temp;
- }
- }
- }
- txt = txt_int;
- }
- var txtCBA = new Array();
- for (i=1;i<txt.length;i++){
- txtCBA[i] = txt[txt.length-i-1];
- }
- for (i=1;i<txtCBA.length;i++){
- for (j=1;j<tr_el.length;j++) {
- if (txtCBA[i]==tr_el[j].childNodes[number].childNodes[0].nodeValue){
- tr_el[i].parentNode.appendChild(tr_el[j]);
- }
- }
- }
- }
stylesheet
- #content_main table.datatable {
- border:3px solid #f00;
- border-collapse:collapse;
- }
- #content_main table.datatable td, #content_main table.datatable th {
- border:1px solid #000;
- padding:5px;
- }
- #content_main table.datatable th{
- background:#ffa;
- }
- #content_main table.datatable th a{
- color:#600;
- }
- #content_main table.datatable tr td:nth-child(even){
- background:#fee;
- }
- #content_main table.datatable tr:nth-child(even) td{
- background:#eee;
- }
- #content_main table.datatable tr:nth-child(even) td:nth-child(even){
- background:#ecc;
- }
- #content_main table.datatable tr:nth-child(even) th{
- background:#dd9
- }
- #content_main table.datatable tbody tr:hover th{
- background:#550;
- color:#fff;
- }
- #content_main table.datatable tbody tr:hover td, #content_main table.datatable tbody tr:hover td:nth-child(even){
- background:#990;
- color:#fff;
- }
- #content_main table.datatable thead th {
- background:#faa;
- }
- #content_main table.datatable th.button {
- cursor:pointer;
- }
- #content_main table.datatable th.button:hover {
-
- background:#fcc;
- }
- #content_main table.datatable th.up{
- background:url(images/icon09_up_arrow.gif) right center no-repeat;
- padding-right:13px;
- background-color:#cfc;
- }
- #content_main table.datatable th.up:hover{
- background:url(images/icon09_up_arrow.gif) right center no-repeat;
- background-color:#efe;
- }
- #content_main table.datatable th.down{
- background:url(images/icon10_down_arrow.gif) right center no-repeat;
- background-color:#ccf;
- padding-right:13px;
- }
- #content_main table.datatable th.down:hover{
- background:url(images/icon10_down_arrow.gif) right center no-repeat;
- background-color:#eef;
- }