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

标签 javascript html html-table

我有一个包含 2 个标题行的 HTML 表格:

<!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 属性有关。

这样对吗?我如何调整他们的代码或我的代码来对跨行列进行排序?

谢谢。

最佳答案

你错过了一件事。

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

更新后的代码:

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;
	  }
	}
 }
}
<!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>

关于javascript - HTML:对稍微复杂的表格列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995594/

相关文章:

javascript - 3级嵌套中继器

javascript - 同时使用 onepage-scroll.js 和 scrollReveal.js

javascript - Html 表(XML 绑定(bind)),带有用于编辑、删除、创建的命令按钮

javascript - 根据特定事件在 HTML 表上使用 Javascript 动态更改 CSS

html - 与 Ember.js 一起使用时 Bootstrap 表的样式困惑

javascript - "SyntaxError: Unexpected token <"与 Ionic Angular JS

javascript - 在 d3 力布局中拖动和平移

javascript - 通过 AJAX 将 JSON 对象发布到 ASP.NET Core Web API

Jquery dd 选择

jquery - 如何为 div 制作固定的 webkit-mask?