Este artigo descreve o método de implementação da classificação do cabeçalho JS. Compartilhe para sua referência.
O método de implementação específico é o seguinte:
A cópia do código é a seguinte:
<script type = "text/javascript">
// se deve resolver
var isDescending = true;
/****************************************************************
* As linhas a serem classificadas devem ser colocadas na tag <tbody> </tbody>
* TableId: Classificar ID da tabela
* COLNO: O número da coluna classificada, ou seja, qual coluna começa de 0
* Startrowno: o número da linha de partida classificado, começando de 0
* Sortlength: o número de linhas para classificar,
* Tipo: o tipo da coluna de classificação
*/
Classificação da função (TableId, Colno, Startrowno, Sorngthngth, Type)
{
// Se o número de linhas a serem classificadas for 1 ou 0, nenhuma operação de classificação será realizada
if (SortLength <= 1) {
retornar;
}
varcurtable = document.getElementById (tabelaId);
var theHeader = currtable.outerhtml.substring (0, currtable.outerhtml.indexof ('<tbody>')+7)
var thefooter = currtable.outerhtml.substring (currtable.outerhtml.indexof ('</tbody>')-8);
// O número de linhas aqui é o número de linhas que removem a tabela de cabeçalho e as linhas da tabela.
var Therews = nova matriz (SortLength);
// recolhendo os dados na tabela
para (i = startrowno; i <sortlength+startrowno; i ++)
{
therows [i-starTrowno] = novo array (currtable.Rows [i] .Cells [colno] .innerText.TolowerCase (), currtable.Rows [i] .outerhtml);
}
if (type.TOUPPERCASE () == 'Número')
{
therows.sort (compareNumber);
}
caso contrário, if (type.TOUPPERCASE () == 'data')
Therews.sort (comparado);
caso contrário, if (type.TOUPPERCASE () == 'String')
therows.sort (Comparestring);
var tableInfo = ''
for (j = 0; j <therows.length; j ++)
{
tabelaInfo+= therows [j] [1];
}
isDescending =! IsDescending;
currtable.outerhtml = theHeader + tabelaInfo + theFooter;
retornar ;
}
// Compare números
função compareNumber (x, y)
{
// Converter dados de formato de moeda
a = x [0] .ExcludarChars (","). TRIM ();
b = y [0] .ExcludarChars (","). TRIM ();
if (a == "") {a = 0;}
if (b == "") {b = 0;}
If (IsDescending)
{
retornar parsefloat (b) - parsefloat (a);
}
outro
{
retornar parsefloat (a) - parsefloat (b);
}
}
// Compare strings
Função Comparestring (X, Y)
{
If (IsDescending)
{
if (x [0]> y [0]) retornar -1;
else if (x [0] <y [0]) retornar 1;
caso contrário, retorne 0;
}
outro
{
if (x [0] <y [0]) retornar -1;
else if (x [0]> y [0]) retornar 1;
caso contrário, retorne 0;
}
}
// Compare o tempo
função comparada (x, y) {
var arr = x [0] .split ("-");
var startTime = nova data (arr [0], arr [1], arr [2]);
var startTimes = startTime.getTime ();
var Arrrs = y [0] .Split ("-");
var lktime = nova data (ARRS [0], ARRS [1], ARRS [2]);
var lktimes = lktime.gettime ();
If (IsDescending)
{
Retornar LKTimes - StartTimes;
}
outro
{
retorno starttimes - lktimes;
}
}
// Remova todas as seqüências especificadas na string
String.prototype.excludechars = function (chars) {
var correspondente = novo regexp (chars, "g");
retornar este.Replace (correspondência, '');
}
</script>
Espero que este artigo seja útil para a programação JavaScript de todos.