单击时,突出显示表列

时间:2013-11-01 18:05:13

标签: javascript php jquery html

使用我的表,用户可以对结果进行升序和降序排序。我需要将它设置为当他们点击标题按钮对该列进行排序时,它会更改所有列的背景(直接位于该标题列下方)以更改背景颜色...这是我的代码到目前为止: 这是CSS:

<style type="text/css">
.move_right {
    padding-left: 5px;
}
.center-text {
    padding-right: 5px;
}
.bottom_center_text {
    padding-left: 2px;
}
#nav_bottom {
    height: 0px; !important
}
#portfolio_header {
    Background-color: #5E90B3;
    color: white;
    border: 1px solid #CCCCCC;
    font-weight: bold;
    font-size: 14px;
    margin-bottom: 0px;
    padding: 10px;
}
table#bin, table#fallen, table#growth, table#turn { margin:10px 0; border:1px solid #ccc; }
th, td { padding:10px 7px; }
tr th { background:#ededed; color:#545454; font-weight:bold; cursor:pointer;}
#bin tr.even td { background:#e1eff1; }
#turn tr.even td { background:#f7f2d8; }
#fallen tr.even td { background:#f2dcbd; }
#growth tr.even td { background:#deefdc; }
td.title a { text-decoration:none; display:block; text-transform:uppercase; font-weight:bold;}
#bin td.title { background:#5198a0; }
#fallen td.title { background:#e6a850; }
#turn td.title { background:#ebd870; }
#growth td.title { background:#6ab065; }
#bin td.title a { color:#182c2e; font-size:13px;}
#fallen td.title a { color:#352713;  font-size:13px;}

#turn td.title a { color:#37321a;  font-size:13px; }
#growth td.title a { color:#233d21;  font-size:13px;}
hr { border:2px dotted #ccc; border-bottom:none; }
#tooltip { position:absolute; z-index:3000; border:1px solid #111; background-color:#eee; padding:5px; }
#tooltip h3, #tooltip div, #tooltip p { margin:0; }


/*#bin tr.even td.click, th.click,*/ td.click, th.click
{
    background-color: #000000;
}
/*#bin tr.even td.hover, tr.hover,*/ td.hover, tr.hover
{
    background-color: #CCC;
}
/*#bin tr.even th.hover, tfoot td.hover,*/ th.hover, tfoot td.hover
{
    background-color: ivory;
}
/*#bin tr.even td.hovercell, th.hovercell,*/ td.hovercell, th.hovercell
{
    background-color: #abc;
}
/*#bin tr.even td.hoverrow, th.hoverrow,*/ td.hoverrow, th.hoverrow
{
    background-color: #6df;
}
</style>

这是HTML:

        <table cellspacing="0" cellpadding="0" id="bin" width="100%">
        <thead>
            <tr>
                <th style="text-align:left; padding-top: 20px;" width="10%" title="Sort by Symbol" id="row-1" data-column="1">Symbol <img src="/images/sort-arrow-up.jpg" title="Sort by Symbol" alt="Sort by Symbol" class="sort-right move-left bottom-image" id="image1"/></th>
                <th style="text-align:left;" width="20%" title="Sort by Company Name" id="row-2" data-column="2">Company<br><span class="move_right">Name</span> <img src="/images/sort-arrow-up.jpg" title="Sort by Company Name" alt="Sort by Company Name" class="sort-right move-left" id="image2"/></th>
                <th style="text-align:center;" width="12%" title="Sort by Buy Date" id="row-3" data-column="3"><span class="center-text">Buy</span><br>Date <img title="Sort by Buy Date" src="/images/sort-arrow.jpg" alt="Sort by Buy Date" id="image3"/></th>
                <th style="text-align:center;" width="10%" title="Sort by Buy Price" id="row-4" data-column="4"><span class="center-text">Buy</span><br>Price &nbsp;<img title="Sort by Buy Price" src="/images/sort-arrow.jpg" alt="Sort by Buy Price" id="image4"/></th>
                <th style="text-align:center;" width="9%" title="Sort by Closed Price" id="row-5" data-column="5"><span class="center-text">Closed</span><br>Price &nbsp;<img title="Sort by Closed Price" src="/images/sort-arrow.jpg" alt="Sort by Closed Price" id="image5"/></th>
                <th style="text-align:center;" width="9%" title="Sort by Closed Date" id="row-6" data-column="6"><span class="center-text">Closed</span><br>Date &nbsp;<img title="Sort by Closed Date" src="/images/sort-arrow.jpg" alt="Sort by Closed Date" id="image6"/></th>
                <th style="text-align:center;" width="10%" title="Sort by Current Return" id="row-7" data-column="7"><span class="center-text">Total</span><br>Return &nbsp;<img title="Sort by Current Return" src="/images/sort-arrow.jpg" alt="Sort by Current Return" id="image7"/></th>
            </tr>
        </thead>
        <tbody>
<?php
    foreach($buylist as $a) {
        $bg = ($c % 2) ? ' class="even"' : '';
        $direction = (is_numeric($a['creturn']) && $a['creturn'] >= 0) ? 'up_green' : 'down_red';
        $tick = (is_numeric($a['creturn']) && $a['creturn'] >= 0) ? '<img src="/images/icon_up.gif">' : '<img src="/images/icon_down.gif">';
        $tick2 = (is_numeric($a['cchange']) && $a['cchange'] >= 0) ? '<img src="/images/icon_up.gif">' : '<img src="/images/icon_down.gif">';   
        $tick3 = (is_numeric($a['final_return_pct']) && $a['final_return_pct'] >= 0) ? '<img src="/images/icon_up.gif">' : '<img src="/images/icon_down.gif">'; 
        $type = '';
        $entry_price = (is_numeric($a['buyprice'])) ? '$'.$a['buyprice'] : '&ndash;';
        $sold_price = (is_numeric($a['sold_price'])) ? '$'.$a['sold_price'] : '&ndash;';
        $total_return= sprintf("%.02f", (($a['sold_price'] - $a['buyprice'])/$a['buyprice']) * 100);
?>
            <tr<?=$bg;?>>
                <td data-column="1"><b><a href="/gamechangers/getaquote/?symbolsearch=<?php echo $a['symbol']; ?>"><?=$a['symbol'];?></a></b><?=$type;?></td>
                <td data-column="2"><?=$a['name'];?></td>
                <td align="center" data-column="3"><?=$a['buydate'];?></td>
                <td align="center" data-column="4"><?=$entry_price;?></td>
                <td align="center" data-column="5"><?php echo $sold_price; ?></td>
                <td align="center" data-column="6"><?=$a['sold_date'];?></td>
                <td align="center" data-column="7"><?php echo $total_return; ?>%</td>
            </tr>
<?php
        $c++;
    }
?>
        </tbody>
    </table>

我尝试过使用插件,但这无济于事,因为我从数据库中获取PHP内容并将其直接输入到表中。旁注:当PHP从数据库中抓取内容时,它会将表中行的颜色从白色交替为蓝色。

非常感谢任何和所有帮助。

2 个答案:

答案 0 :(得分:2)

我会为每个<td><th>分配一个data-column属性,指明它是哪一列。然后单击<th>,获取标题的data-column属性并使用jQuery(或类似的库)更改具有该data-column属性的所有表格单元格的颜色。

示例表标记:

<table id="table">
  <thead>
    <tr>
      <th data-column="1">Column 1</th>
      <th data-column="2">Column 2</th> // etc ....
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-column="1">contents</td>
      <td data-column="2">contents</td> // etc...
    </tr>
  </tbody>
</table>

示例jQuery:

$('body').on('click', '#table th', function(){

  // Grab the column number
  var column = $(this).attr('data-column');

  // Reset any highlighted columns
  $('#table td').removeClass('highlighted'); 

  // Set all columns with the same column number to have the highlight class
  $('#table td[data-column='+column+']').addClass('highlighted');

})

希望这有帮助!

最佳, 安德鲁

答案 1 :(得分:0)

您应该可以使用.index():eq()轻松地使用JQuery执行此操作,而无需更改当前的HTML。

单击进行排序后,在对所有内容进行排序后,使用以下方法获取行中单击的<th>的位置:

var colNum = $(this).parent().children().index($(this));

。 。 。但是,假设click事件绑定到<th>本身。如果它绑定到较低级别的元素,则必须稍微修改一下代码:

var parentTH = $(this).closest("th");
var colNum = parentTH.parent().children().index(parentTH);

如果您拥有行中<th>的索引,则可以选择位于其行的相同位置的所有<td>标记,如下所示:

var selectedColCells = $("#bin tr").find("td:eq(" + colNum + ")");

一旦你有了这些,你可以直接为每一个添加一个类或更改样式(我建议使用类:))。您还希望每次都清除表中所有<td>标记的类/样式,以便它重置为默认的“未排序”外观。

另外注意,如果您在表格中使用<colgroup>,每列都使用<col>,则可以使用相同的逻辑,但您只需更新所选的{{1}使用类/样式,而不是列中的每个<col>

编辑修正了逻辑中的一个小问题,当点击事件与<td>级别以下的元素相关联时,会找到“colNum”值。