简体   繁体   中英

Jquery set tr with empty td lower than with text in td

I have html, and jquery for sorting my table (also there is non-standart sorting (with multi-tbody)).

My code could be found here: http://jsfiddle.net/GuRxj/

As you can see there, td with prices (on russian Цена) are sorted ascending (but tech-as not!? why? (it a question too))... But as you see, i need to send this tr's with prices to top of this tbody (now there are in the bottom), while empty-price-tr send to bottom... How to do this?

part of js:

$('.prcol').click(function(e) {
      var $sort = this;
      var $table = $('#articles-table');
      var $rows = $('tbody.analogs_art > tr',$table);
      $rows.sort(function(a, b){
          var keyA = $('td:eq(3)',a).text();
          var keyB = $('td:eq(3)',b).text();
            if (keyA.length > 0 && keyB.length > 0)
            {
              if($($sort).hasClass('asc')){
                  console.log("bbb");
                  return (keyA > keyB) ? 1 : 0;
              } else {
                console.log(keyA+"-"+keyB);
                  return (keyA > keyB) ? 1 : 0;
              }
            }
      });
      $.each($rows, function(index, row){
        //console.log(row);
        $table.append(row);
        //$("table.123").append(row);
      });
      e.preventDefault();
    });

Fixed Demo http://jsfiddle.net/RwW9q/3/ or http://jsfiddle.net/tcZNH/

Rest code should be clear to understand,

Надеюсь, что это FIDS ваши потребности :) (hope it fits your need,)

Code

jQuery(function($) {
    var table = $('table');
    $(document).ready(function() {
        $('.prcol').click(function(e) {
            var $sort = this;
            var $table = $('#articles-table');
            var $rows = $('tbody.analogs_art > tr', $table);
            $rows.sort(function(a, b) {
                var keyA = $('td:eq(3)', a).text().toUpperCase();;
                var keyB = $('td:eq(3)', b).text().toUpperCase();;

                if (keyA.length > 0 && isNaN(parseFloat($('td:eq(3)', b).text()))) return Ascending(keyA, keyB);
            });
            $.each($rows, function(index, row) {
                //console.log(row);
                $table.append(row);
                //$("table.123").append(row);
            });
            e.preventDefault();
        });
    });
});

function Ascending(a, b) {
    if (a > b) return -1;
    if (a < b) return 1;
    return 0;
}​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM