简体   繁体   English

使用jQuery按升序对行进行排序

[英]Sort rows based in ascending order using jQuery

This code works but checks only the first column. 此代码有效,但只检查第一列。 I want to check the 2nd column instead with the points. 我想用点来检查第二列。 How do I alter it? 我该如何改变它?

HTML Table: HTML表格:

<table width="100%">
    <thead>
        <tr>
            <th width="60%">Name</th>
            <th width="20%">School</th>
            <th width="20%">Points</th>
        </tr>
    </thead>
    <tbody>
      <tr>
        <td rowspan="4"><h1>Event 1</h1></td>
          <td>School1</td>
          <td>74</td>
      </tr>
       <tr>
          <td>School2</td>
           <td>69</td>
       </tr>
      <tr>
          <td>School3</td>
           <td>71</td>
      </tr>
      <tr>
          <td>School4</td>
           <td>11</td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
      </tr>
      <tr>
        <td rowspan="4"><h1>Event 2</h1></td>
          <td>School1</td>
          <td>34</td>
      </tr>
       <tr>
          <td>School5</td>
           <td>29</td>
       </tr>
      <tr>
          <td>School3</td>
           <td>62</td>
      </tr>
      <tr>
          <td>School7</td>
           <td>15</td>
      </tr>
   </tbody>
</table>

jQuery: jQuery的:

 var $tbody = $('#caltbl tbody');
            $tbody.find('tr').sort(function (a, b) {
                var tda = $(a).find('td:eq(0)').text();
                var tdb = $(b).find('td:eq(0)').text();
                // if a < b return 1
                return tda > tdb ? 1
                       // else if a > b return -1
                       : tda < tdb ? -1
                       // else they are equal - return 0    
                       : 0;
            }).appendTo($tbody);

How do I got about this? 我怎么得到这个?

JSFIDDLE 的jsfiddle

EDIT: I'm sorry guys but my live code is different with rowspan being used. 编辑:我很抱歉,但我的实时代码与使用的rowspan不同。 Is there a possibility to have this in ascending order so that the events are sorted differently? 是否有可能按升序排列,以便对事件进行不同的排序?

eq(0) means you are using the first index. eq(0)表示您正在使用第一个索引。 Change it to eq(1) so that it can consider the second index. 将其更改为eq(1),以便它可以考虑第二个索引。

var tda = $(a).find('td:eq(1)').text();
var tdb = $(b).find('td:eq(1)').text();

You can sort the trs. 你可以对trs进行排序。 Detach the td and insert to an array. 分离td并插入数组。 Then append them back to each row 然后将它们追加到每一行

Add some class to simplify coding. 添加一些类以简化编码。

JSFIDDLE 的jsfiddle

HTML HTML

 <table width="100%">
    <thead>
        <tr>
            <th width="60%">Name</th>
            <th width="20%">School</th>
            <th width="20%">Points</th>
        </tr>
    </thead>
    <tbody>
      <tr class="event1">
        <td rowspan="4"><h1>Event 1</h1></td>
          <td>School1</td>
          <td class="point">74</td>
      </tr>
       <tr class="event1">
          <td>School2</td>
           <td class="point">69</td>
       </tr>
      <tr class="event1">
          <td>School3</td>
           <td class="point">71</td>
      </tr>
      <tr class="event1">
          <td>School4</td>
           <td class="point">11</td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
      </tr>
      <tr>
        <td rowspan="4"><h1>Event 2</h1></td>
          <td>School1</td>
          <td>34</td>
      </tr>
       <tr>
          <td>School5</td>
           <td>29</td>
       </tr>
      <tr>
          <td>School3</td>
           <td>62</td>
      </tr>
      <tr>
          <td>School7</td>
           <td>15</td>
      </tr>
   </tbody>
</table>

JavaScript JavaScript的

var $tbody = $(' tbody');
var array = [];
$tbody.find('.event1').sort(function (a, b) {
    var tda = $(a).find('.point').text();
    var tdb = $(b).find('.point').text();
    return tda - tdb;
}).each(function (idx, tr) {
  array.push($(tr).children('td').not('[rowspan]').detach());
});
$.each(array, function (idx, obj) {
    $(obj).appendTo($('.event1:eq(' + idx + ')'));
});

The JavaScript only applies to event1. JavaScript仅适用于event1。 You can simply modify it for arbitrary events. 您可以简单地修改任意事件。


Change the index as Mayank Pandey said. 改变指数,正如Mayank Pandey所说。 And.. 和..

Since your second column is number, you can just return their difference. 由于您的第二列是数字,您可以返回它们的差异。

var $tbody = $('#caltbl tbody');
$tbody.find('tr').sort(function (a, b) {
    var tda = parseInt($(a).find('td:eq(1)').text(), 10); // always use the base number
    var tdb = parseInt($(b).find('td:eq(1)').text(), 10);
    return tda - tdb;
}).appendTo($tbody);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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