0

I have a table and each row should link to a page.

I want to keep the structure of the html as is (not a bunch of divs and a grid like Wrapping HTML table rows in <a> tags)

Everything works with the javascript, but I am missing the bottom left tooltip that shows the url on hover from an tag. I also want the option to open in a new tab with CMD (mac) or CTRL (windows/linux).

I am currently doing the solution with jQuery:

    $('.clickable-row').on('click', function () {
        const url = $(this).attr('data-url')
        if (typeof url === 'string') {
            window.location.href = url
        }
    });

My html (twig):

        <table class='table'>
            <thead>
                <tr>
                    {% for h in data.header %}
                        <th>{{h|trans}}</th>
                    {% endfor %}
                </tr>
            </thead>
            <tbody>
                {% for row in data.rows %}
                    {% set url = data.getOnClickURL(loop.index - 1) %}
                    <tr {% if url %} class='clickable-row' data-url="{{url}}" {% endif %}>
                        {% for r in row %}
                                <td>{{r|trans}}</td>
                        {% endfor %}
                    </tr>
                {% endfor %}
            </tbody>
        </table>
elmgren
  • 193
  • 12

1 Answers1

0

try this solution without writing Javascript code using only HTML and CSS.

table {
    width: 100%;
    border-collapse: collapse;
}

table tr {
    position: relative;
}
table tr td {
    border: 1px solid black;
    text-align: center;
}

table tr a {
    position: absolute;
    width: 100%;
    display: block;
    height: 100%;
    top: 0px;
    left: 0px;
}
<table>
  <tr>
    <td><a href="https://google.com"></a> 1 </td>
    <td>2 </td>
    <td>3 </td>
  </tr>
<tr>
    <td><a href="https://github.com"></a>1 </td>
    <td>2 </td>
    <td>3 </td>
</tr>
<tr>
    <td><a href="https://stackoverflow.com/"></a>1 </td>
    <td>2 </td>
    <td>3 </td>
</tr>
<tr>
    <td>1 </td>
    <td>2 </td>
    <td>3 </td>
</tr>
<tr>
    <td>1 </td>
    <td>2 </td>
    <td>3 </td>
</tr>
</table>
ahmed moeed
  • 183
  • 6