[ sortTable ] Trier des colonnes par statut (checked)
Bonjour,
J'utilise un script pour trier un tableau sans recharger la page, en Javascript. Dans ce tableau, tout fonctionne bien (tri par int, string, date), sauf lorsque je veux trier par checkbox cochées ou non. Normal, le script de base ne gère pas le tri de checkbox.
J'ai donc tenté de modifier le script, mais ça ne fonctionne pas encore.
Voici la fonction qui s'occupe du tri :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| function sort2dFnc(a,b){
var col = intCol;
var dir = intDir;
var aCell = a.getElementsByTagName("td")[col].innerHTML;
var bCell = b.getElementsByTagName("td")[col].innerHTML;
switch (strMethod){
case "int":
aCell = parseInt(aCell);
bCell = parseInt(bCell);
break;
case "float":
aCell = parseFloat(aCell);
bCell = parseFloat(bCell);
break;
case "date":
parts = aCell.split("/");
var aCell = new Date(0);
aCell.setDate(parts[0]);
aCell.setMonth(parts[1]);
aCell.setFullYear(parts[2]);
aCell = new Date(aCell);
parts = bCell.split("/");
var bCell = new Date(0);
bCell.setDate(parts[0]);
bCell.setMonth(parts[1]);
bCell.setFullYear(parts[2]);
bCell = new Date(bCell);
break;
}
return (aCell>bCell)?dir:(aCell<bCell)?-dir:0;
} |
J'ai donc tenté de rajouter un case "input", ainsi :
Code:
1 2 3 4 5 6 7 8 9 10 11
| case "input":
aState = a.getElementsByTagName("td")[col].getElementsByTagName("input").checked;
if(aState == true)
aCell = 1;
else
aCell = 0;
bState = b.getElementsByTagName("td")[col].getElementsByTagName("input").checked;
if(bState == true)
bCell = 1;
else
bCell = 0; |
Mais ça ne fonctionne pas, le tri se fait de façon totalement aléatoire, et ne regroupe pas les checkbox cochées ensemble.
Des idées ?
Merci d'avance
Voici la totalité du fichier qui s'occupe du tri, sortTable.js, si ça peut aider :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| function sortableTable(tableIDx,intDef,sortProps){
var tableID = tableIDx;
var intCol = 0;
var intDir = 1;
var strMethod;
var arrHead = null;
var arrMethods = sortProps.split(",");
this.init = function(){
arrHead = document.getElementById(tableID).getElementsByTagName('thead')[0].getElementsByTagName('th');
for(var i=0;i<arrHead.length;i++){
arrHead[i].onclick = new Function(tableIDx + ".sortTable(" + i + ",'" + arrMethods[i] + "');");
}
this.sortTable(intDef,arrMethods[intDef]);
}
this.sortTable = function(intColx,strMethodx){
intCol = intColx;
strMethod = strMethodx;
var arrRows = document.getElementById(tableID).getElementsByTagName('tbody')[0].getElementsByTagName('tr');
intDir = (arrHead[intCol].className=="asc")?-1:1;
arrHead[intCol].className = (arrHead[intCol].className=="asc")?"des":"asc";
for(var i=0;i<arrHead.length;i++){
if(i!=intCol){arrHead[i].className="";}
}
var arrRowsSort = new Array();
for(var i=0;i<arrRows.length;i++){
arrRowsSort[i]=arrRows[i].cloneNode(true);
}
arrRowsSort.sort(sort2dFnc);
for(var i=0;i<arrRows.length;i++){
arrRows[i].parentNode.replaceChild(arrRowsSort[i],arrRows[i]);
arrRows[i].className = (i%2==0)?"":"alt";
}
}
function sort2dFnc(a,b){
var col = intCol;
var dir = intDir;
var aCell = a.getElementsByTagName("td")[col].innerHTML;
var bCell = b.getElementsByTagName("td")[col].innerHTML;
switch (strMethod){
case "int":
aCell = parseInt(aCell);
bCell = parseInt(bCell);
break;
case "float":
aCell = parseFloat(aCell);
bCell = parseFloat(bCell);
break;
case "date":
parts = aCell.split("/");
var aCell = new Date(0);
aCell.setDate(parts[0]);
aCell.setMonth(parts[1]);
aCell.setFullYear(parts[2]);
aCell = new Date(aCell);
parts = bCell.split("/");
var bCell = new Date(0);
bCell.setDate(parts[0]);
bCell.setMonth(parts[1]);
bCell.setFullYear(parts[2]);
bCell = new Date(bCell);
break;
case "input":
aState = a.getElementsByTagName("td")[col].getElementsByTagName("input").checked;
if(aState == true)
aCell = 1;
else
aCell = 0;
bState = b.getElementsByTagName("td")[col].getElementsByTagName("input").checked;
if(bState == true)
bCell = 1;
else
bCell = 0;
}
return (aCell>bCell)?dir:(aCell<bCell)?-dir:0;
}
}
// La même fonction, mais pour faire un tri DESC par défaut
function sortableTableDesc(tableIDx,intDef,sortProps){
var tableID = tableIDx;
var intCol = 0;
var intDir = 1;
var strMethod;
var arrHead = null;
var arrMethods = sortProps.split(",");
this.init = function(){
arrHead = document.getElementById(tableID).getElementsByTagName('thead')[0].getElementsByTagName('th');
for(var i=0;i<arrHead.length;i++){
arrHead[i].onclick = new Function(tableIDx + ".sortTable(" + i + ",'" + arrMethods[i] + "');");
}
this.sortTable(intDef,arrMethods[intDef]);
}
this.sortTable = function(intColx,strMethodx){
intCol = intColx;
strMethod = strMethodx;
var arrRows = document.getElementById(tableID).getElementsByTagName('tbody')[0].getElementsByTagName('tr');
intDir = (arrHead[intCol].className=="des")?1:-1;
arrHead[intCol].className = (arrHead[intCol].className=="des")?"asc":"des";
for(var i=0;i<arrHead.length;i++){
if(i!=intCol){arrHead[i].className="";}
}
var arrRowsSort = new Array();
for(var i=0;i<arrRows.length;i++){
arrRowsSort[i]=arrRows[i].cloneNode(true);
}
arrRowsSort.sort(sort2dFnc);
for(var i=0;i<arrRows.length;i++){
arrRows[i].parentNode.replaceChild(arrRowsSort[i],arrRows[i]);
arrRows[i].className = (i%2==0)?"":"alt";
}
}
function sort2dFnc(a,b){
var col = intCol;
var dir = intDir;
var aCell = a.getElementsByTagName("td")[col].innerHTML;
var bCell = b.getElementsByTagName("td")[col].innerHTML;
switch (strMethod){
case "int":
aCell = parseInt(aCell);
bCell = parseInt(bCell);
break;
case "float":
aCell = parseFloat(aCell);
bCell = parseFloat(bCell);
break;
case "date":
parts = aCell.split("/");
var aCell = new Date(0);
aCell.setDate(parts[0]);
aCell.setMonth(parts[1]);
aCell.setFullYear(parts[2]);
aCell = new Date(aCell);
parts = bCell.split("/");
var bCell = new Date(0);
bCell.setDate(parts[0]);
bCell.setMonth(parts[1]);
bCell.setFullYear(parts[2]);
bCell = new Date(bCell);
break;
}
return (aCell>bCell)?dir:(aCell<bCell)?-dir:0;
}
} |