如何通过其标头值移动表中的列

时间:2017-02-24 05:04:22

标签: javascript jquery html html-table

我想要成年人'列移动到表格中的最后一个位置(右侧),但表格是交互式的,列数不固定,有时候,没有成人'柱。

请帮忙

这是表格:



table.tableizer-table {
		font-size: 12px;
		border: 1px solid #CCC; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	.tableizer-table td {
		padding: 4px;
		margin: 3px;
		border: 1px solid #CCC;
	}
	.tableizer-table th {
		background-color: #104E8B; 
		color: #FFF;
		font-weight: bold;
	}

<table class="tableizer-table">
 <thead><tr class="tableizer-firstrow"><th></th><th>adult</th><th>embryo</th><th>lava</th><th>pupa</th></tr></thead>
 <tbody>
  <tr><td>AAEL006466-RA</td><td>ns</td><td>ns</td><td>**</td><td>ns</td></tr>
  <tr><td>AAEL006466-S2</td><td>***</td><td>ns</td><td>ns</td><td>ns</td></tr>
  <tr><td>AAEL006466-S4</td><td>***</td><td>ns</td><td>*</td><td>ns</td></tr>
 </tbody>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

&#13;
&#13;
$(function() {
 let firstColumnHeader;
 let findPosToMove = $("table").find('tr')[0].cells.length - 2;
 jQuery.each($("table tr"), function(index, value) {
  if(index === 0){
   firstColumnHeader = ($(this).children(":eq(1)")[index].innerText);
  }
  if(firstColumnHeader == 'adult'){
   for(var i = 0; i < findPosToMove; i++){
    $(this).children(":eq(1)").before($(this).children(":eq("+(findPosToMove+1)+")"));
   }
  }
 });
});
&#13;
table.tableizer-table {
  font-size: 12px;
  border: 1px solid #CCC; 
  font-family: Arial, Helvetica, sans-serif;
} 
.tableizer-table td {
  padding: 4px;
  margin: 3px;
  border: 1px solid #CCC;
}
.tableizer-table th {
  background-color: #104E8B; 
  color: #FFF;
  font-weight: bold;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tableizer-table">
  <thead><tr class="tableizer-firstrow"><th></th><th>adult</th><th>embryo</th><th>lava</th><th>pupa</th></tr></thead>
   <tbody>
    <tr><td>AAEL006466-RA</td><td>ns</td><td>ns</td><td>**</td><td>ns</td></tr>
    <tr><td>AAEL006466-S2</td><td>***</td><td>ns</td><td>ns</td><td>ns</td></tr>
    <tr><td>AAEL006466-S4</td><td>***</td><td>ns</td><td>*</td><td>ns</td></tr>
  </tbody>
</table>
&#13;
&#13;
&#13;