Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [jQuery]Sortowanie tabeli względem jednego wiersza.
S_Olewniczak
post
Post #1





Grupa: Zarejestrowani
Postów: 189
Pomógł: 1
Dołączył: 28.01.2008

Ostrzeżenie: (0%)
-----


Napisałem prosty kod do sortowania tabeli względem jednego wiersza, którego indeks znamy. Wygląda on tak:
Kod
//now sort table
if(sort_td_index!=undefined) {
     $(table + ' tr:gt(1)').each(function() {
        act_tr_1 = this;
        act_td_1 = $('td:eq(' + sort_td_index + ')' ,this).html().toLowerCase();
        $(table + ' tr:gt(1)').each(function() {
            act_td_2 = $('td:eq(' + sort_td_index + ')' ,this).html().toLowerCase();
            if ((act_td_2 > act_td_1 && sort_opt=='asc') || (act_td_2 < act_td_1 && sort_opt=='desc')) {
                $(act_tr_1).insertBefore(this);
            }
        });
        
     });
}//end if

Jednak nie działa jak należy. Co robię źle?

Ten post edytował S_Olewniczak 6.07.2009, 09:40:15
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
S_Olewniczak
post
Post #2





Grupa: Zarejestrowani
Postów: 189
Pomógł: 1
Dołączył: 28.01.2008

Ostrzeżenie: (0%)
-----


Działa! Po trzech dniach walki. Tu trochę poprawiony kod, poprawnie sortujący liczby. Może się komuś przyda.
Kod
/*
  * Natural Sort algorithm for Javascript
  *  Version 0.2
  * Author: Jim Palmer (based on chunking idea from Dave Koelle)
  * Released under MIT license.
  */
function naturalSort (a, b) {
         // setup temp-scope variables for comparison evauluation
         var x = a.toString().toLowerCase() || '', y = b.toString().toLowerCase() || '',
                 nC = String.fromCharCode(0),
                 xN = x.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
                 yN = y.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
                 xD = (new Date(x)).getTime(), yD = (new Date(y)).getTime();
         // natural sorting of dates
         if ( xD && yD && xD < yD )
                 return -1;
         else if ( xD && yD && xD > yD )
                 return 1;
         // natural sorting through split numeric strings and default strings
         for ( var cLoc=0, numS = Math.max( xN.length, yN.length ); cLoc < numS; cLoc++ )
                 if ( ( parseFloat( xN[cLoc] ) || xN[cLoc] ) < ( parseFloat( yN[cLoc] ) || yN[cLoc] ) )
                         return -1;
                 else if ( ( parseFloat( xN[cLoc] ) || xN[cLoc] ) > ( parseFloat( yN[cLoc] ) || yN[cLoc] ) )
                         return 1;
         return 0;
}
finction sortTable(sort_td_index, table='#id_of_table') {
var rows = $(table).find('tbody tr').get();
rows.sort(function(a, b) {
    var keyA = $(a).children('td').eq(sort_td_index ).text();
    var keyB = $(b).children('td').eq(sort_td_index ).text();
return naturalSort(keyA, keyB);
  
});
$.each(rows, function(index, row) {
   $(table).children('tbody').append(row);
});
}


Ten post edytował S_Olewniczak 7.07.2009, 12:06:04
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 02:11