我正在制作一个显示帐户交易的视图,并且我想对交易类型/状态进行颜色编码。我还想展示一个解释颜色代码的图例。
我想要一个结构如下的最终结果:
HTML
<table id="transactions">
<thead>
<tr>
<th colspan="2">
Transactions
</th>
</tr>
</thead>
<tbody>
<tr class="credit">
<td>A credit</td>
<td>$1.00</td>
</tr>
<tr class="debit paid">
<td>A paid debit</td>
<td>($2.00)</td>
</tr>
<tr class="debit unpaid">
<td>An unpaid debit</td>
<td>($3.00)</td>
</tr>
</tbody>
</table>
<hr/>
<table id="legend">
<thead>
<tr>
<th colspan="3">
Legend
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="credit">Credit</td>
<td class="debit paid">Paid</td>
<td class="debit unpaid">Unpaid</td>
</tr>
</tbody>
</table>
CSS
table#transactions > tbody > tr.credit {
color: limegreen;
}
table#legend > tbody > tr > td.credit {
color: limegreen;
}
table#transactions > tbody > tr.debit.paid {
color: goldenrod;
}
table#legend > tbody > tr > td.debit.paid {
color: goldenrod;
}
table#transactions > tbody > tr.debit.unpaid {
color: crimson;
}
table#legend > tbody > tr > td.debit.unpaid {
color: crimson;
}
请注意,“借方”使用两个类别名称,以将它们与贷方区分开来。
显然那里有一些冗余,我试图将其重构为这个(无效的)LESS 代码:
.transaction-status(@class, @color) {
table#transactions > tbody > tr@{class} {
color: @color;
}
table#legend > tbody > tr > td@{class} {
color: @color;
}
}
.transaction-status(.credit, limegreen);
.transaction-status(.debit.paid, goldenrod);
.transaction-status(.debit.unpaid, crimson);
一种可能的解决方法是重新调整事物,以便不同的事务类型具有一个唯一的类名称,但这感觉就像时间旅行到 IE6 时代。即我知道,但想避免这个有效的 LESS,它看起来如此接近,但到目前为止:
.transaction-status(@class, @color) {
table#transactions > tbody > tr.@{class} {
color: @color;
}
table#legend > tbody > tr > td.@{class} {
color: @color;
}
}
.transaction-status(credit, limegreen);
.transaction-status(debit-paid, goldenrod);
.transaction-status(debit-unpaid, crimson);
我尝试引用类名,但即使这使第一个 LESS 示例编译,引号也会传递给输出 CSS。那么,有没有办法将“标识符”以外的东西作为参数传递给LESS mixin,并让它在选择器插值中正确工作?