Dieser Artikel beschreibt die Implementierungsmethode der JS -Headersortierung. Teilen Sie es für Ihre Referenz.
Die spezifische Implementierungsmethode lautet wie folgt:
Die Codekopie lautet wie folgt:
<script type = "text/javaScript">
// ob sortieren soll
var isdescending = true;
/*************************************************************
* Die zu sortierten Zeilen müssen in das <tbody> </tbody> -Tag platziert werden
* TABLEID: TABLE ID sortieren
* Colno: Die sortierte Spaltennummer, dh welche Spalte, beginnt mit 0
* STARTORNO: Die Startlinie der Sortiernummer von 0 ab 0
* SortLength: Die Anzahl der Zeilen zu sortieren,
* Typ: Der Typ der Sortierspalte
*/
Funktionssortierung (TableId, Colno, Startero, SortLength, Typ)
{
// Wenn die Anzahl der zu sortierten Zeilen 1 oder 0 beträgt, wird kein Sortiervorgang durchgeführt
if (sortLength <= 1) {
zurückkehren;
}
var currtable = document.getElementById (tableId);
var theHeader = currtable.outerhtml.substring (0, currtable.outerhtml.indexof ('<tbody>')+7)
var thefoter = currtable.outerhtml.substring (currtable.outerhtml.indexof ('</tbody>')-8);
// Die Anzahl der Zeilen hier ist die Anzahl der Zeilen, die die Header -Tabelle und die Tabellenreihen entfernen.
var therows = new Array (SortLength);
// die Daten in der Tabelle zusammenbrechen
für (i = startero; i <sortLength+startero; i ++)
{
Therows [i-startrowno] = new Array (currtable.rows [i] .cells [colno] .InNerText.tolowerCase (), currtable.rows [i] .outerhtml);
}
if (type.touppercase () == 'Nummer'))
{
therows.sort (vergleiche);
}
else if (type.touppercase () == 'Datum')
therows.sort (verglichen);
else if (type.touppercase () == 'String')
therows.sort (Compusectring);
var tableinfo = '' ''
für (j = 0; j <therows.length; j ++)
{
tableInfo+= therows [j] [1];
}
isdescending =! isdescending;
currtable.outerhtml = TheHeader + tableInfo + theFoter;
zurückkehren ;
}
// Zahlen vergleiche
Funktion vergleiche (x, y)
{
// Daten des Währungsformates konvertieren
a = x [0] .Excludechars (","). trim ();
b = y [0] .Excludechars (","). trim ();
if (a == "") {a = 0;}
if (b == "") {b = 0;}
if (isdescending)
{
Rückkehr Parsefloat (B) - Parsefloat (a);
}
anders
{
Rückkehr Parsefloat (a) - Parsefloat (b);
}
}
// Vergleiche Saiten
Funktionenvergleich (x, y)
{
if (isdescending)
{
if (x [0]> y [0]) return -1;
sonst wenn (x [0] <y [0]) return 1;
sonst return 0;
}
anders
{
if (x [0] <y [0]) return -1;
sonst wenn (x [0]> y [0]) zurücksender 1;
sonst return 0;
}
}
// Zeit vergleichen
Funktion im Vergleich (x, y) {
var arr = x [0] .Split ("-");
var startTime = neues Datum (arr [0], arr [1], arr [2]);
var startTimes = startTime.getTime ();
var arrrs = y [0] .Split ("-");
var lktime = neues Datum (arrs [0], arrs [1], arrs [2]);
var lktimes = lktime.getTime ();
if (isdescending)
{
return lktimes - starttimes;
}
anders
{
Return Starttimes - lktimes;
}
}
// Entfernen Sie alle angegebenen Zeichenfolgen in der Zeichenfolge
String.prototype.Excludechars = function (chars) {
var matching = new regexp (chars, "g");
kehre diesen.replace zurück (Matching, '');
}
</script>
Ich hoffe, dieser Artikel wird für JavaScript -Programme aller hilfreich sein.