按两列对HTML表格进行排序 [英] Sorting HTML table by two columns

查看:95
本文介绍了按两列对HTML表格进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格中有几列,例如A,B,C,D和E列,需要在HTML页面中显示.在某些页面中,我需要仅基于页面的一列显示排序结果,例如表的第三列"C"列.我可以使用以下代码来做到这一点:

I have several columns in a table such as column A,B,C,D and E which I need to show in my HTML page. In some pages I need to show sorted results based on only one column of page, such as for Column "C" which is 3rd column of table. I am able to do this using the below code:

function Ascending(a, b) {
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}
var rows = $('#table tr').not(':first').get();
$('#table tr').slice(1).remove();
rows.sort(function(rowA, rowB) {
  var keyA = $(rowA).children('td').eq(2).text().toUpperCase();
  var keyB = $(rowB).children('td').eq(2).text().toUpperCase();
  return Ascending(keyA, keyB);
});

但是我还有另一个要求,其中我需要显示基于两列的排序结果,即在上述情况下基于C列的排序,E列的结果也应该得到排序.例如:

But I have another requirement wherein I need to show the sorted results based on two columns i.e. based on the sorting of Column C in above case, results of column E should also get sorted. For example:

排序前:

Column C  Column E
2         Fish
1         Box
7         Cat
2         Dog
1         Apple
2         Box
2         Axe
7         Box
2         Answer
7         Apple
6         Year
2         Goat

仅对C列进行排序后:

Column C  Column E
1         Box
1         Apple
2         Dog
2         Fish
2         Box
2         Axe
2         Goat
2         Answer
6         Year
7         Box
7         Apple
7         Cat

对C列和E列进行排序后:

Column C  Column E
1         Apple
1         Box
2         Answer
2         Axe
2         Box
2         Dog
2         Fish
2         Goat
6         Year
7         Apple
7         Box
7         Cat

我无法实施.我该怎么办?

Which I am unable to implement. How can I do it?

推荐答案

要按多个列进行排序,您可以编写如下比较函数:

To sort by more than one column you write the comparison function like this:

(比较函数传递了两个行")

(Comparison function is passed two "rows")

  1. 比较第1行第1列和第2行第1列
    • 如果它们不同,则返回结果(+ ve或-ve数字)
  1. Compare row 1 column 1 with row 2 column 1
    • If they are different then return the result (a +ve or -ve number)
  • 如果它们不同,则返回结果(+ ve或-ve数字)

下面的示例演示如何编写按两列排序的比较函数.可以使用循环或递归按 n 列进行排序.

The following example shows how to write the compare function that sorts by two columns. It is possible to use a loop or recursion to sort by n columns.

$(function() {
  function sortByColumn3(row1, row2) {
    var v1, v2;
    v1 = $(row1).find("td:eq(2)").text();
    v2 = $(row2).find("td:eq(2)").text();
    // for numbers you can simply return a-b instead of checking greater/smaller/equal
    return v1 - v2;
  }

  function sortByColumn3And5(row1, row2) {
    var v1, v2, r;
    v1 = $(row1).find("td:eq(2)").text();
    v2 = $(row2).find("td:eq(2)").text();
    r = v1 - v2;
    if (r === 0) {
      // we have a tie in column 1 values, compare column 2 instead
      v1 = $(row1).find("td:eq(4)").text();
      v2 = $(row2).find("td:eq(4)").text();
      if (v1 < v2) {
        r = -1;
      } else if (v1 > v2) {
        r = 1;
      } else {
        r = 0;
      }
    }
    return r;
  }
  $("#button1, #button2").on("click", function() {
    var rows = $("#table1 tbody tr").detach().get();
    switch (this.id) {
      case "button1":
        rows.sort(sortByColumn3);
        break;
      case "button2":
        rows.sort(sortByColumn3And5);
        break;
    }
    $("#table1 tbody").append(rows);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<input type="button" id="button1" value="sortByColumn3">
<input type="button" id="button2" value="sortByColumn3And5">

<table id="table1" border="1" width="100%">
  <thead>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>x</td>
      <td>y</td>
      <td>2</td>
      <td>z</td>
      <td>Fish</td>
    </tr>
    <tr>
      <td>y</td>
      <td>z</td>
      <td>1</td>
      <td>x</td>
      <td>Box</td>
    </tr>
    <tr>
      <td>z</td>
      <td>x</td>
      <td>7</td>
      <td>y</td>
      <td>Cat</td>
    </tr>
    <tr>
      <td>x</td>
      <td>y</td>
      <td>2</td>
      <td>z</td>
      <td>Dog</td>
    </tr>
    <tr>
      <td>y</td>
      <td>z</td>
      <td>1</td>
      <td>x</td>
      <td>Apple</td>
    </tr>
    <tr>
      <td>z</td>
      <td>x</td>
      <td>2</td>
      <td>y</td>
      <td>Box</td>
    </tr>
    <tr>
      <td>x</td>
      <td>y</td>
      <td>2</td>
      <td>z</td>
      <td>Axe</td>
    </tr>
    <tr>
      <td>y</td>
      <td>z</td>
      <td>7</td>
      <td>x</td>
      <td>Box</td>
    </tr>
    <tr>
      <td>z</td>
      <td>x</td>
      <td>2</td>
      <td>y</td>
      <td>Answer</td>
    </tr>
    <tr>
      <td>x</td>
      <td>y</td>
      <td>7</td>
      <td>z</td>
      <td>Apple</td>
    </tr>
    <tr>
      <td>y</td>
      <td>z</td>
      <td>6</td>
      <td>x</td>
      <td>Year</td>
    </tr>
    <tr>
      <td>z</td>
      <td>x</td>
      <td>2</td>
      <td>y</td>
      <td>Goat</td>
    </tr>
  </tbody>
</table>

这篇关于按两列对HTML表格进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆