【问题标题】:Add alternating row colors with column grouping colors-is it possible?使用列分组颜色添加交替的行颜色 - 可能吗?
【发布时间】:2013-08-16 19:25:39
【问题描述】:

是否可以将 GridView(与 jQuery 的 tablesorter 结合使用)设置为具有交替的行颜色,但更改每个列分组的颜色集?见下图:

我目前正在根据我创建的数组对单元格背景颜色进行硬编码,例如greenArray 是一个整数数组,设置为 (0,1,2,3),purpleArray 是 (4,5,6,7) 等等。但是,当我使用 tablesorter 插件时,显然单元格保持颜色,这弄乱了交替配色方案:

编辑:我目前正在 VB.NET 中添加背景颜色。以下函数定义了数组,然后调用了一个 ColorizeMe() 函数,该函数实际应用了样式:

Private Sub StyleTable(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvReport.RowDataBound

            'Define arrays to color the gridview, if cell index is in array, it will be colored
            Dim blueArray() As Integer = {0, 17, 18, 19, 20}
            Dim greenArray() As Integer = {1, 2, 3, 4}
            Dim purpleArray() As Integer = {5, 6, 7, 8}
            Dim pinkArray() As Integer = {9, 10, 11, 12}
            Dim yellowArray() As Integer = {13, 14, 15, 16}

            _packworks.ColorizeMe(blueArray, greenArray, purpleArray, pinkArray, yellowArray, e.Row)

End Sub

还有 ColorizeMe() 函数:

Public Sub ColorizeMe(ByVal blueArray() As Integer, ByVal greenArray() As Integer, _
                                 ByVal purpleArray() As Integer, ByVal pinkArray() As Integer, _
                                 ByVal yellowArray() As Integer, ByVal row As GridViewRow)
            Dim i As Integer = 0

            For Each cell As TableCell In row.Cells
                If Array.IndexOf(blueArray, i) <> -1 Then
                    If _isDark Then 'Color account column
                        cell.BackColor = ColorTranslator.FromHtml("#B0C4DE")
                    Else
                        cell.BackColor = ColorTranslator.FromHtml("#E6E6FA")
                    End If
                ElseIf Array.IndexOf(greenArray, i) <> -1 Then
                    If _isDark Then
                        cell.BackColor = ColorTranslator.FromHtml("#a4d5a8")
                    Else
                        cell.BackColor = ColorTranslator.FromHtml("#ddf5de")
                    End If
                ElseIf Array.IndexOf(purpleArray, i) <> -1 Then
                    If _isDark Then
                        cell.BackColor = ColorTranslator.FromHtml("#ada4d4")
                    Else
                        cell.BackColor = ColorTranslator.FromHtml("#c7c6f4")
                    End If
                ElseIf Array.IndexOf(pinkArray, i) <> -1 Then
                    If _isDark Then
                        cell.BackColor = ColorTranslator.FromHtml("#e3b3e0")
                    Else
                        cell.BackColor = ColorTranslator.FromHtml("#fae1fa")
                    End If
                ElseIf Array.IndexOf(yellowArray, i) <> -1 Then
                    If _isDark Then
                        cell.BackColor = ColorTranslator.FromHtml("#e0e3ab")
                    Else
                        cell.BackColor = ColorTranslator.FromHtml("#f5f8dd")
                    End If
                End If

                i += 1
            Next

            _isDark = Not _isDark
        End Sub

【问题讨论】:

  • 你能贴出适用的代码吗?这是可能的,但更容易看到你现在正在使用什么。
  • 我希望我能发布小提琴。我目前在服务器端的 .NET 中添加颜色背景,我只是好奇我是否可以在 jQuery 中完成相同的功能,但对语言知之甚少。但是,如果这有助于您深入了解我的想法,我确实将我的服务器端代码添加到描述中。我有几页需要着色的不同列,所以我认为这些数组是最好的选择。

标签: jquery css


【解决方案1】:

由于您的行是交替的明暗,您可以利用 alpha 透明背景颜色:

http://cssdeck.com/labs/6rgab657

<table>
  <colgroup class="a" span="4" />
  <colgroup class="b" span="4" />
  <colgroup class="c" span="4" />
  <colgroup class="d" span="4" />

  <tr>
    <td>A</td>
    <td>A</td>
    <td>A</td>
    <td>A</td>
    <td>B</td>
    <td>B</td>
    <td>B</td>
    <td>B</td>
    <td>C</td>
    <td>C</td>
    <td>C</td>
    <td>C</td>
    <td>D</td>
    <td>D</td>
    <td>D</td>
    <td>D</td>
  </tr>

  <tr>
    <td>A</td>
    <td>A</td>
    <td>A</td>
    <td>A</td>
    <td>B</td>
    <td>B</td>
    <td>B</td>
    <td>B</td>
    <td>C</td>
    <td>C</td>
    <td>C</td>
    <td>C</td>
    <td>D</td>
    <td>D</td>
    <td>D</td>
    <td>D</td>
  </tr>
</table>

CSS:

.a {
  background: blue;
}

.b {
  background: green;
}

.c {
  background: purple;
}

.d {
  background: red;
}

tr:nth-child(odd) {
  background: rgba(255, 255, 255, .70);
}

【讨论】:

  • 我开始实现这个。请记住,这是 .NET。我为每个TemplateFields 添加了ItemStyle-CssClass 属性,并将CSS 类设置为蓝色、绿色等,效果很好。但是,CSS 没有选择 tr:nth-child(odd) 属性。我想知道它是否无法覆盖我在TemplateFields 上设置的手动类
  • 这取决于颜色的应用方式。如果是通过style="background: red",则很难覆盖,如果可以,最好将其删除。如果只是带一个类,可以将选择器调整为tr:nth-child(odd) td,使其更具体。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2015-07-30
  • 1970-01-01
相关资源
最近更新 更多