简体   繁体   English

使用jQuery根据背景颜色对表行进行排序

[英]Sort table rows based on background-color using jquery

This is my FIDDLE 这是我的FIDDLE

<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中轻松完成此操作。

  1. Initiate an empty js object like this var colorRowmap = {} 启动一个空的js对象,例如var colorRowmap = {}
  2. Loop through all elements(tr elements) of table and get colorName from each tr. 循环遍历表的所有元素(tr元素)并从每个tr中获取colorName。 And if that color does not exist as a key of colorRowMap object do colorRowMap[colorName] = [currentTableRow] else do colorRowMap[colorName] = colorRowMap[colorName].push(currentTableRow) 如果该颜色不作为colorRowMap对象的键存在,则执行colorRowMap[colorName] = [currentTableRow]否则执行colorRowMap[colorName] = colorRowMap[colorName].push(currentTableRow)
  3. Empty the table. 清空桌子。
  4. Loop through all keys of colorRowMap and push the tr roows to table. 循环遍历colorRowMap的所有键,并将行推到表中。

Done. 完成。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM