Making a Table Row clickable

前端 未结 14 988
悲&欢浪女
悲&欢浪女 2020-12-24 12:49

I wonder what the best way to make an entire tr clickable would be?

The most common (and only?) solution seems to be using JavaScript, by using onclick=\"javascript:

相关标签:
14条回答
  • 2020-12-24 13:12

    I had that same problem, I solved it by using CSS only. I think it was the best solution for me, because I was using it in JSF also.

    Just assign the style class to the table and you are good to go....

    Here it goes:

    CSS:

    .myDataTable {
        background: 444;
        width: 100%;
    }
    
    .myDataTable thead tr {
        background-image: url('../img/tableHeader.jpg');
    }
    
    .myDataTable thead tr th {
        height: 28px;
        font-size: 14px;
        font-family: tahoma, helvetica, arial, sans-serif;
        padding-left: 5px;
    }
    
    .myDataTable thead tr th img {
        padding-right: 5px;
        padding-top: 1px;
    }
    
    .myDataTable thead tr td {
        height: 15px;
        font-size: 11px;
        font-weight: bold;
        font-family: tahoma, helvetica, arial, sans-serif;
        padding-left: 5px;
    }
    
    .myDataTable tbody {
        background: #f2f5f9;
    }
    
    .myDataTable tbody tr:nth-child(even) td,tbody tr.even td {
        background: #e2ebf4;
        font-size: 12px;
        padding-left: 5px;
        height: 14px;
    }
    
    .myDataTable tbody tr:nth-child(odd) td,tbody tr.odd td {
        background: #f7faff;
        font-size: 12px;
        padding-left: 5px;
        height: 14px;
    }
    
    .myDataTable tbody tr:hover td {
        background-color: #e7e7e7;
    }
    
    .myDataTable tbody tr td {
        height: 14px;
        padding-left: 5px;
        font-size: 12px;
    }
    
    .myDataTable tbody tr td a {
        color: black;
        text-decoration: none;
        font-size: 12px;
        display: block;
    }
    
    .myDataTable thead tr th a {
        color: black;
        text-decoration: none;
        font-size: 12px;
        display: inline;
    }
    

    Your table structure should be:

    <table class="myDataTable">
     <thead>
      <tr>
       <th>Heading 1</th>
       <th>Heading 2</th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td><a href="#">Data    1 </a></td>
       <td><a href="#">Data    2 </a></td>
      </tr>
     </tbody>
    </table>
    
    0 讨论(0)
  • 2020-12-24 13:15

    You might also try wrapping the content of your row's cells in an href and using CSS to push the href height/width to the internal bounds of each cell. The row itself wouldn't be clickable (unless you added additional html to the row) but most of the content space of the row would act like a normal link (cursor, status bar, etc). I can't remember off hand exactly how I did this before but I was reasonably successful getting this to work.

    Edit: A comment asked for more details and they were covered by a later post from another user but I didn't realize that until I looked further into this suggestion and tested it.

    If you add "display: block" CSS style tag to the anchor objects in the cells that you want to be clickable it will make the entire cell (minus any padding) act like a button. The cursor is displayed correctly and it previews the link destination in the status bar. This is all done with zero javascript. Good luck.

    0 讨论(0)
  • 2020-12-24 13:17

    If your table does not have links inside, following trick should work.

    Put entire table into a link and change the href attribute of the link in rows onmouseover events.

    Demo code:

    <script type="text/javascript">
    function setLink(elRow) {
    var elLink = document.getElementById('link');
    elLink.href = elRow.rowIndex + ".com";
    }
    </script>
    ...
    <a id=link>
    <table>
        <tr onMouseOver="setLink(this);"><td>first row</td></tr>
        <tr onMouseOver="setLink(this);"><td>second row</td></tr>
    </table>
    </a> 
    
    0 讨论(0)
  • 2020-12-24 13:18

    Fortunately or unfortunately, most modern browsers do not let you control the status bar anymore (it was possible and popular back in the day) because of fraudulent intentions.

    Your better bet would be a title attribute or a javascript tooltip.

    0 讨论(0)
  • 2020-12-24 13:23

    If your targeted browsers all support CSS Table display styles, you can use Javascript to wrap each row in an <a> tag styled to function as a <tbody>.

    Here's some JS code using jQuery to make it happen: (jsfiddle)

    $(function() {
      $('.table-linked').each(function() {
        var table, tbody;
        table = this;
        tbody = $('tbody', this);
        tbody.children().each(function() {
          var href, row;
          row = $(this);
          href = row.attr('data-href');
          $('<a href="' + href + '" style="display: table-row-group" />').append(row).appendTo(table);
        });
        tbody.remove();
      });
    });
    

    This code will transform a table that looks like this:

    <table class="table-linked">
        <tbody>
            <tr data-href="/a"><td>a</td><td>1</td></tr>
            <tr data-href="/b"><td>b</td><td>2</td></tr>
        </tbody>
    </table>
    

    Into this DOM structure in the browser:

    <table>
        <a href="/a" style="display: table-row-group">
            <tr><td>a</td><td>1</td></tr>
        </a>
        <a href="/b" style="display: table-row-group">
            <tr><td>b</td><td>1</td></tr>
        </a>
    </table>
    

    Browsers don't seem to be capable of parsing this structure as HTML code (and needless to say it won't validate), it needs to be constructed using JS

    0 讨论(0)
  • 2020-12-24 13:24

    If you don't want to use javascript, you can do what Chris Porter suggested by wrapping each td element's content in matching anchor tags. Then set the anchor tags to display: block and set the height and line-height to be the same as the td's height. You should then find that the td's touch seamlessly and the effect is that the whole row is clickable. Watch out for padding on the td, which will cause gaps in the clickable area. Instead, apply padding to the anchor tags as it will form part of the clickable area if you do that.

    I also like to set the row up to have a highlight effect by applying a different background color on tr:hover.

    Example

    For the latest Bootstrap (version 3.0.2), here's some quick CSS to show how this can be done:

    table.row-clickable tbody tr td {
        padding: 0;
    }
    
    table.row-clickable tbody tr td a {
        display: block;
        padding: 8px;
    }
    

    Here's a sample table to work with:

    <table class="table table-hover row-clickable">
        <tbody>
            <tr>
                <td><a href="#">Column 1</a></td>
                <td><a href="#">Column 2</a></td>
                <td><a href="#">Column 3</a></td>
            </tr>
        </tbody>
    </table>
    

    Here's an example showing this in action.

    0 讨论(0)
提交回复
热议问题