HTML:对稍微复杂的表列进行排序

时间:2017-05-16 07:57:55

标签: javascript html html-table

我有一个HTML表格,其中包含2个标题行:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="styles.css">
        </head>
        <body>
            <table id="res-table">
                <tr>
                    <th rowspan="2">H1</th>
                    <th rowspan="2" onclick="sortTable(1)">H2</th>
                    <th colspan="2">H3</th>
                    <th colspan="2">H4</th>
                    <th rowspan="2">H5</th>
                </tr>
                <tr>
                    <th>A1</th>
                    <th>A2</th>
                    <th>A3</th>
                    <th>A4</th>
                </tr>
                <tr>
                    <td>1</th>
                    <td>2</th>
                    <td>3</th>
                    <td>4</th>
                    <td>5</th>
                    <td>6</th>
                    <td>7</th>
                </tr>
                <tr>
                  <td>11</th>
                  <td>21</th>
                  <td>31</th>
                  <td>41</th>
                  <td>51</th>
                  <td>61</th>
                  <td>71</th>
                </tr>
            </table>

            <script>
            function sortTable(n) {
              var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
              table = document.getElementById("res-table");
              switching = true;
              //Set the sorting direction to ascending:
              dir = "asc"; 
              /*Make a loop that will continue until
              no switching has been done:*/
              while (switching) {
                //start by saying: no switching is done:
                switching = false;
                rows = table.getElementsByTagName("tr");
                /*Loop through all table rows (except the
                first, which contains table headers):*/
                for (i = 1; i < (rows.length - 1); i++) {
                  //start by saying there should be no switching:
                  shouldSwitch = false;
                  /*Get the two elements you want to compare,
                  one from current row and one from the next:*/
                  x = rows[i].getElementsByTagName("td")[n];
                  y = rows[i + 1].getElementsByTagName("td")[n];
                  /*check if the two rows should switch place,
                  based on the direction, asc or desc:*/
                  if (dir == "asc") {
                    if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
                      //if so, mark as a switch and break the loop:
                      shouldSwitch= true;
                      break;
                    }
                  } else if (dir == "desc") {
                    if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
                      //if so, mark as a switch and break the loop:
                      shouldSwitch= true;
                      break;
                    }
                  }
                }
                if (shouldSwitch) {
                  /*If a switch has been marked, make the switch
                  and mark that a switch has been done:*/
                  rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
                  switching = true;
                  //Each time a switch is done, increase this count by 1:
                  switchcount ++;      
                } else {
                  /*If no switching has been done AND the direction is "asc",
                  set the direction to "desc" and run the while loop again.*/
                  if (switchcount == 0 && dir == "asc") {
                    dir = "desc";
                    switching = true;
                  }
                }
              }
            }
            </script>           
        </body>
    </html>

我需要通过点击标题对几列进行排序。例如,第二个和最后一个。

所以,我从以下代码示例:https://www.w3schools.com/howto/howto_js_sort_table.asp

该脚本适用于简单表,就像他们的示例一样。但它对我不起作用。我猜它与rowspan属性有关。

这是对的吗?我如何调整他们的代码,或者我可以对行跨越的列进行排序?

感谢。

2 个答案:

答案 0 :(得分:2)

你错过了一件事。

i=2开始循环,(按照评论),因为我们必须跳过第1行和第2行,它们都是标题。

更新后的代码:

&#13;
&#13;
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable");
  switching = true;
  //Set the sorting direction to ascending:
  dir = "asc"; 
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
	//start by saying: no switching is done:
	switching = false;
	rows = table.getElementsByTagName("tr");
	/* *********** Loop through all table rows (except the
	first, which contains table headers): *********** */
	
	<!-- BUT HERE WE NEED TO SKIP THE SECOND ROW TOO -->
	<!-- make i=2 -->
	for (i = 2; i < (rows.length - 1); i++) {
	  //start by saying there should be no switching:
	  shouldSwitch = false;
	  /*Get the two elements you want to compare,
	  one from current row and one from the next:*/
	  x = rows[i].getElementsByTagName("TD")[n];
	  y = rows[i + 1].getElementsByTagName("TD")[n];
	  /*check if the two rows should switch place,
	  based on the direction, asc or desc:*/
	  if (dir == "asc") {
		if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
		  //if so, mark as a switch and break the loop:
		  shouldSwitch= true;
		  break;
		}
	  } else if (dir == "desc") {
		if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
		  //if so, mark as a switch and break the loop:
		  shouldSwitch= true;
		  break;
		}
	  }
	}
	if (shouldSwitch) {
	  /*If a switch has been marked, make the switch
	  and mark that a switch has been done:*/
	  rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
	  switching = true;
	  //Each time a switch is done, increase this count by 1:
	  switchcount ++;      
	} else {
	  /*If no switching has been done AND the direction is "asc",
	  set the direction to "desc" and run the while loop again.*/
	  if (switchcount == 0 && dir == "asc") {
		dir = "desc";
		switching = true;
	  }
	}
 }
}
&#13;
<!DOCTYPE html>
<html>
	<head>
	</head>
	<body>
		<table border="1px" id="myTable">
		   <tr>
				<th rowspan="2" onclick="sortTable(0)">H1</th>
				<th rowspan="2" onclick="sortTable(1)">H2</th>
				<th colspan="2">H3</th>
				<th colspan="2">H4</th>
				<th rowspan="2" onclick="sortTable(6)">H5</th>
			</tr>
			<tr>
				<th onclick="sortTable(2)">A1</th>
				<th onclick="sortTable(3)">A2</th>
				<th onclick="sortTable(4)">A3</th>
				<th onclick="sortTable(5)">A4</th>
			</tr>
			
			<!--<tr>
				 <th onclick="sortTable(0)">H2</th>
				  <th>H2</th>
				  <th>H2</th>
				  <th>H2</th>
				  <th>H2</th>
				  <th>H2</th>
				  <th>H2</th>
			</tr>-->
			<tr>
				<td>1</th>
				<td>2</th>
				<td>3</th>
				<td>4</th>
				<td>5</th>
				<td>6</th>
				<td>7</th>
			</tr>
			<tr>
			  <td>11</th>
			  <td>21</th>
			  <td>31</th>
			  <td>41</th>
			  <td>51</th>
			  <td>61</th>
			  <td>71</th>
			</tr>
		</table>
	</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我最终使用tablesorter。虽然这是我第一次触及jQuery,但使用它还是小菜一碟! 谢谢大家!