Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [jquery] problem z sortowaniem wedlug daty
trucksweb
post
Post #1





Grupa: Zarejestrowani
Postów: 1 199
Pomógł: 31
Dołączył: 22.03.2004
Skąd: Warszawa

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


pisze skrypt w jquery umozliwiajacy sortowanie tabeli i mam maly problem, gdyz do sortowania daty w formacie YYYY-MM-DD
przerobilem jakis inny skrypt jquery i cos sie popierniczylo, ze nie sortuje:

  1. <?php
  2. $.fn.alternateRowColors = function() {
  3.    $('tbody tr:odd', this).removeClass('even').addClass('odd');
  4.    $('tbody tr:even', this).removeClass('odd').addClass('even');
  5.    return this;
  6. };
  7.  
  8. $(document).ready(function() {
  9.  
  10. var alternateRowColors = function($table) {
  11.    $('tbody tr:odd', $table).removeClass('even').addClass('odd');
  12.    $('tbody tr:even', $table).removeClass('odd').addClass('even');
  13.  };
  14.  
  15.  $('table.sortable').each(function() {
  16.    var $table = $(this);
  17.    $table.alternateRowColors($table);
  18.    
  19.    $('th', $table).each(function(column) {
  20.      var findSortKey;
  21.      
  22.      if($(this).is('.sort-alpha')) {
  23.      
  24.        findSortKey = function($cell) {
  25.          return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();
  26.        };
  27.       }
  28.      else if($(this).is('.sort-numeric')) {
  29.        findSortKey = function($cell) {
  30.          var key = parseFloat($cell.text().replace(/^[^d.]*/, ''));
  31.          return isNaN(key) ? 0 : key;        
  32.        };
  33.       }  
  34.      else if($(this).is('.sort-date')) {
  35.        findSortKey = function($cell) {
  36.          var dateTest = [
  37.                        { regExp:/^(0?[1-9]|1[012])([- /.])(0?[1-9]|[12][0-9]|3[01])([- /.])((dd)?dd)$/, d:3, m:1, y:5 },  // mdy
  38.                        { regExp:/^(0?[1-9]|[12][0-9]|3[01])([- /.])(0?[1-9]|1[012])([- /.])((dd)?dd)$/, d:1, m:3, y:5 },  // dmy
  39.                        { regExp:/^(dddd)([- /.])(0?[1-9]|1[012])([- /.])(0?[1-9]|[12][0-9]|3[01])$/, d:5, m:3, y:1 }      // ymd
  40.                        ];
  41.                var start, cnt = 0, numFormats = dateTest.length;
  42.                while(cnt < numFormats) {
  43.                        start = (cnt + (favourDMY ? numFormats + 1 : numFormats)) % numFormats;
  44.                        if(dateIn.match(dateTest[start].regExp)) {
  45.                                res = dateIn.match(dateTest[start].regExp);
  46.                                y = res[dateTest[start].y];
  47.                                m = res[dateTest[start].m];
  48.                                d = res[dateTest[start].d];
  49.                                if(m.length == 1) m = "0" + String(m);
  50.                                if(d.length == 1) d = "0" + String(d);
  51.                                if(y.length != 4) y = (parseInt(y) < 50) ? "20" + String(y) : "19" + String(y);
  52.  
  53.                                return Date.parse(y+String(m)+d;
  54.                        };
  55.                        cnt++;
  56.                };
  57.                return 0;  
  58.        };
  59.      }
  60.      
  61.       if(findSortKey) {
  62.        $(this).addClass ('clickable').hover(function() {
  63.          $(this).addClass('hover');
  64.        }, function() {
  65.          $(this).removeClass('hover');
  66.        }).click(function() {
  67.          var rows = $table.find('tbody > tr').get();
  68.          
  69.          $.each(rows, function(index, row) {
  70.            row.sortKey = findSortKey($(row).children('td').eq(column));
  71.          });
  72.          
  73.          rows.sort(function(a, b) {                    
  74.            if(a.sortKey < b.sortKey) return -1;
  75.            if(a.sortKey > b.sortKey) return 1;  
  76.            return 0;        
  77.          });
  78.          $.each(rows, function(index, row) {
  79.            $table.children('tbody').append(row);
  80.            row.sortKey = null;
  81.          });
  82.          $table.alternateRowColors($table);
  83.        });
  84.      }
  85.    });
  86.  });
  87. });
  88. ?>



moze ktos zauwazy w czym problem ? (chodzi wylacznie o sortowanie wg daty)
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: 22.12.2025 - 20:30