[英]Sort table rows based on background-color using jquery
<table>
<tr>
<td>
<div style="background:blue;color:white">hello</div>
</td>
</tr>
<tr>
<td>
<div style="background:pink;color:white">hello</div>
</td>
</tr>
<tr>
<td>
<div style="background:blue;color:white">hello</div>
</td>
</tr>
<tr>
<td>
<div style="background:green;color:white">hello</div>
</td>
</tr>
<tr>
<td>
<div style="background:pink;color:white">hello</div>
</td>
</tr>
<tr>
<td>
<div style="background:green;color:white">hello</div>
</td>
</tr>
</table>
How do i rearrange the html table rows based on color? 如何根据颜色重新排列html表行? I want to group html table rows based on the background color of the rows.
我想基于行的背景颜色对html表行进行分组。
Use sort()
to sorting array of tr
elements. 使用
sort()
对tr
元素数组进行排序。 You can get backgroud-color
of element in function of sort and set arrangement of every element. 您可以通过排序和设置每个元素的排列功能来获取元素的
backgroud-color
。
$("table tr").sort(function (a, b){ return $("div", b).css("background") < $("div", a).css("background") ? 1 : -1; }).appendTo('table');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <div style="background:blue">hello</div> </td> </tr> <tr> <td> <div style="background:pink">hello</div> </td> </tr> <tr> <td> <div style="background:blue">hello</div> </td> </tr> <tr> <td> <div style="background:green">hello</div> </td> </tr> <tr> <td> <div style="background:pink">hello</div> </td> </tr> <tr> <td> <div style="background:green">hello</div> </td> </tr> </table>
I reviewed your question, I think you want to differentiate each tr by there color, adding html, style and script for you here. 我查看了您的问题,我想您希望通过颜色来区分每个tr,在这里为您添加html,样式和脚本。
Here is the Html 这是HTML
<table>
</tbody>
<tr><td>123</td></tr>
<tr><td>123</td></tr>
<tr><td>123</td></tr>
<tr><td>123</td></tr>
<tr><td>123</td></tr>
</table>
Do add this script, by this function all your tr will have unique classes. 请添加此脚本,通过此功能,您所有的tr都将具有唯一的类。 you can add there background colors etc style on the base of class
您可以在课堂的基础上添加背景颜色等样式
<script>
// please do add jQuery file to prevent error
function adddClass() {
for(i=1; i<=6; i++) {
alert("");
jQuery('table tr:nth-child('+ i +')').addClass("color"+i);
}
}
adddClass();
</script>
Here is the style for background color of each table row tr 这是每个表格行tr的背景色样式
<style>
.color1{background-color:orange;}
.color2{background-color:teal;}
.color3{background-color:red;}
.color4{background-color:#717171;}
.color5{background-color:khaki;}
.color6{background-color:lightgray;}
tr, table, body{width:100%;}
</style>
Hope this will help, Thanks.! 希望这会有所帮助,谢谢!
You can easily do it in javascript. 您可以在javascript中轻松完成此操作。
var colorRowmap = {}
var colorRowmap = {}
colorRowMap[colorName] = [currentTableRow]
else do colorRowMap[colorName] = colorRowMap[colorName].push(currentTableRow)
colorRowMap[colorName] = [currentTableRow]
否则执行colorRowMap[colorName] = colorRowMap[colorName].push(currentTableRow)
Done. 完成。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.