Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1665 connectés 

  FORUM HardWare.fr
  Programmation
  HTML/CSS

  [js] compatibilité IE/Mozilla pour trier un <table> d'une page html

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[js] compatibilité IE/Mozilla pour trier un <table> d'une page html

n°698518
homerjay01
Is There Brain Inside ?
Posté le 13-04-2004 à 18:43:19  profilanswer
 

salut à tous, je rencontres quelques problemes de compatibilité entre IE et Mozilla  :pfff:  
le tri ne marche que sous IE  :heink:  
je vous post mon script JS...dsl un peu long  :jap:  
après ça je met <body> puis  

Code :
  1. <table border=1 width="447" cellpadding=0 cellspacing=2 bordercolordark=green bordercolorlight=teal name="rsTable" id=rsTable  cols=4>
  2.        <TR>
  3.        <TD><A href="javascript:sortTable(0, rsTable);"><B>Nom</B></A></TD>
  4.        <TD><A href="javascript:sortTable(1, rsTable);"><B>Numero</B></A></TD>
  5.        <TD><A href="javascript:sortTable(2, rsTable);"><B>Race</B></A></TD>
  6.        <TD><A href="javascript:sortTable(3, rsTable);"><B>niveau</B></A></TD>
  7.        </TR>..............................................


 
mon script en javascript... :ouch: (j'y connais pas grand chose...j'ai repompé sur un site que j'ai trouvé  :whistle: )

Code :
  1. <SCRIPT LANGUAGE="JavaScript">
  2. <!-- This script and many more are available free online at -->
  3. <!-- The JavaScript Source!! http://javascript.internet.com -->
  4. <!-- Begin
  5. function setDataType(cValue)
  6.   {
  7.     // THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY
  8.     // SORTING WHEN IN THE SORT FUNCTION
  9.     var isDate = new Date(cValue);
  10.     if (isDate == "NaN" )
  11.       {
  12.         if (isNaN(cValue))
  13.           {
  14.             // THE VALUE IS A STRING, MAKE ALL CHARACTERS IN
  15.             // STRING UPPER CASE TO ASSURE PROPER A-Z SORT
  16.             cValue = cValue.toUpperCase();
  17.             return cValue;
  18.           }
  19.         else
  20.           {
  21.             // VALUE IS A NUMBER, TO PREVENT STRING SORTING OF A NUMBER
  22.             // ADD AN ADDITIONAL DIGIT THAT IS THE + TO THE LENGTH OF
  23.             // THE NUMBER WHEN IT IS A STRING
  24.             var myNum;
  25.             myNum = String.fromCharCode(48 + cValue.length) + cValue;
  26.             return myNum;
  27.           }
  28.         }
  29.   else
  30.       {
  31.         // VALUE TO SORT IS A DATE, REMOVE ALL OF THE PUNCTUATION AND
  32.         // AND RETURN THE STRING NUMBER
  33.         //BUG - STRING AND NOT NUMERICAL SORT .....
  34.         // ( 1 - 10 - 11 - 2 - 3 - 4 - 41 - 5  etc.)
  35.         var myDate = new String();
  36.         myDate = isDate.getFullYear() + " " ;
  37.         myDate = myDate + isDate.getMonth() + " ";
  38.         myDate = myDate + isDate.getDate(); + " ";
  39.         myDate = myDate + isDate.getHours(); + " ";
  40.         myDate = myDate + isDate.getMinutes(); + " ";
  41.         myDate = myDate + isDate.getSeconds();
  42.         //myDate = String.fromCharCode(48 + myDate.length) + myDate;
  43.         return myDate ;
  44.       }
  45.   }
  46. function sortTable(col, tableToSort)
  47.   {
  48.     var iCurCell = col + tableToSort.cols;
  49.     var totalRows = tableToSort.rows.length;
  50.     var bSort = 0;
  51.     var colArray = new Array();
  52.     var oldIndex = new Array();
  53.     var indexArray = new Array();
  54.     var bArray = new Array();
  55.     var newRow;
  56.     var newCell;
  57.     var i;
  58.     var c;
  59.     var j;
  60.     // ** POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
  61.     for (i=1; i < tableToSort.rows.length; i++)
  62.       {
  63.         colArray[i - 1] = setDataType(tableToSort.cells(iCurCell).innerText);
  64.         iCurCell = iCurCell + tableToSort.cols;
  65.       }
  66.     // ** COPY ARRAY FOR COMPARISON AFTER SORT
  67.     for (i=0; i < colArray.length; i++)
  68.       {
  69.         bArray[i] = colArray[i];
  70.       }
  71.     // ** SORT THE COLUMN ITEMS
  72.     //alert ( colArray );
  73.     colArray.sort();
  74.     //alert ( colArray );
  75.     for (i=0; i < colArray.length; i++)
  76.       { // LOOP THROUGH THE NEW SORTED ARRAY
  77.         indexArray[i] = (i+1);
  78.         for(j=0; j < bArray.length; j++)
  79.           { // LOOP THROUGH THE OLD ARRAY
  80.             if (colArray[i] == bArray[j])
  81.               {  // WHEN THE ITEM IN THE OLD AND NEW MATCH, PLACE THE
  82.                 // CURRENT ROW NUMBER IN THE PROPER POSITION IN THE
  83.                 // NEW ORDER ARRAY SO ROWS CAN BE MOVED ....
  84.                 // MAKE SURE CURRENT ROW NUMBER IS NOT ALREADY IN THE
  85.                 // NEW ORDER ARRAY
  86.                 for (c=0; c<i; c++)
  87.                   {
  88.                     if ( oldIndex[c] == (j+1) )
  89.                     {
  90.                       bSort = 1;
  91.                     }
  92.                       }
  93.                       if (bSort == 0)
  94.                         {
  95.                           oldIndex[i] = (j+1);
  96.                         }
  97.                           bSort = 0;
  98.                         }
  99.           }
  100.     }
  101.   // ** SORTING COMPLETE, ADD NEW ROWS TO BASE OF TABLE ....
  102.   for (i=0; i<oldIndex.length; i++)
  103.     {
  104.       newRow = tableToSort.insertRow();
  105.       for (c=0; c<tableToSort.cols; c++)
  106.         {
  107.           newCell = newRow.insertCell();
  108.           newCell.innerHTML = tableToSort.rows(oldIndex[i]).cells(c).innerHTML;
  109.         }
  110.       }
  111.   //MOVE NEW ROWS TO TOP OF TABLE ....
  112.   for (i=1; i<totalRows; i++)
  113.     {
  114.       tableToSort.moveRow((tableToSort.rows.length -1),1);
  115.     }
  116.   //DELETE THE OLD ROWS FROM THE BOTTOM OF THE TABLE ....
  117.   for (i=1; i<totalRows; i++)
  118.     {
  119.       tableToSort.deleteRow();
  120.     }
  121.   }
  122. //  End -->
  123. </script>


Message édité par homerjay01 le 14-04-2004 à 11:40:29
mood
Publicité
Posté le 13-04-2004 à 18:43:19  profilanswer
 

n°698899
homerjay01
Is There Brain Inside ?
Posté le 14-04-2004 à 10:18:45  profilanswer
 

:hello:  
y'as pas de super balaises en JS qui sont debouts ? :sleep:


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  HTML/CSS

  [js] compatibilité IE/Mozilla pour trier un <table> d'une page html

 

Sujets relatifs
[XHTML/CSS] Demande d'avis sur cette page[VB] Clicker automatiquement sur un bouton de page web
C et page webRecuperer une page externe
[PHP] Probleme pour afficher un champ (bd) dans une page PHP[HTML] Place d'une image
[HTML]Besoin d'aide d'un mec qui touche un peu...Verifier une table MySQL ... Et les résultats ...
chargement d'un lien dans un cadre a l'ouverture de la page ???création d'un fichier excel en html (ancre nommé en bas de page??)
Plus de sujets relatifs à : [js] compatibilité IE/Mozilla pour trier un <table> d'une page html


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR