【发布时间】:2019-03-28 06:29:28
【问题描述】:
我有一个表格,单元格可以放在其中:
- 没有背景颜色
- RGB(255, 0, 0)
- rgb(255, 165, 0)
- RGB(0, 128, 0)
这是我正在谈论的表格的一小部分:
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<table class="w3-table-all">
<tr><th class="w3-center" style="width: 16.7%;">Name</th><th class="w3-center" style="width: 16.7%;">Run-1</th><th class="w3-center" style="width: 16.7%;">Run-2</th><th class="w3-center" style="width: 16.7%;">Run-3</th><th class="w3-center" style="width: 16.7%;">Run-4</th><th class="w3-center" style="width: 16.7%;">Run-5</th></tr>
<tr><th>test1</th><td>9130.47</td><td>392478.721</td><td class="w3-center">-</td><td>44301.148</td><td>44301.148</td></tr>
<tr><th>test2</th><td>747505.0087</td><td style="background: rgb(0, 128, 0);">368460.1843</td><td style="background: rgb(0, 128, 0);">64204.7407</td><td style="background: rgb(0, 128, 0);">106400.8238</td><td style="background: rgb(0, 128, 0);">106400.8238</td></tr>
<tr><th>test3</th><td>65.01%</td><td style="background: rgb(255, 0, 0);">68.25%</td><td style="background: rgb(255, 0, 0);">69.17%</td><td style="background: rgb(255, 0, 0);">65.07%</td><td style="background: rgb(0, 128, 0);">65.07%</td></tr>
<tr><th>test4</th><td>67.53%</td><td>82.72%</td><td>69.67%</td><td>74.29%</td><td>74.29%</td></tr>
<tr><th>test5</th><td>-1906.877</td><td style="background: rgb(0, 128, 0);">-103.597</td><td style="background: rgb(0, 128, 0);">-87.119</td><td style="background: rgb(0, 128, 0);">-132.043</td><td style="background: rgb(0, 128, 0);">-132.043</td></tr>
<tr><th>test6</th><td>64.86%</td><td style="background: rgb(255, 0, 0);">68.09%</td><td style="background: rgb(255, 0, 0);">72.67%</td><td style="background: rgb(255, 165, 0);">65.56%</td><td style="background: rgb(255, 165, 0);">65.56%</td></tr>
</table>
我想通过单击任何标题对表格进行排序,它将根据该特定列中单元格的颜色对其进行排序。例如,当你点击“Run-4”时,我想按如下方式对表格进行排序:
- 先红细胞
- 橙色
- 绿色
- 没有颜色
当我再次单击“Run-4”时,它会执行相反的操作:
- 先无颜色
- 绿色
- 橙色
- 红色
我尝试引用几个 SO 问题,但没有成功实施:
【问题讨论】:
-
我明白了,但我不明白你想要排序的顺序,例如在run-2中单击时会发生什么?为什么在 run-4 中使用这种排序顺序?
-
当您单击 Run-2 时,应该会发生相同的排序。我只是提供了 Run-4 作为示例,因为它包含
<td>,所有 3 种颜色都没有颜色 -
因此,与单击的“运行”无关,它应该将整个表格从无颜色排序为红色,反之亦然,是吗?你有一些你已经尝试在你的问题中发布的 JS 吗?
-
另外,这个表是动态创建的吗?它以某种格式出现,如 json、对象或数组?或者它是静态的,直接在 HTML 中创建?
-
您可以创建一个带有排序键和颜色值的对象并对其进行排序。
标签: javascript jquery html css