Tuesday, November 28, 2023
HomeVideo Editing2 Methods to Make HTML Desk Rows Clickable

2 Methods to Make HTML Desk Rows Clickable


On this tutorial, I’ll train you two fast methods to make HTML desk rows clickable. We’ll begin with an HTML/CSS strategy. Then, we’ll proceed with one other one which additionally makes use of a little bit of JavaScript. Lastly, we’ll see adapt certainly one of these strategies in Bootstrap tables.

HTML/CSS Methodology

This primary strategy requires solely HTML and CSS.

The actions wanted are the next:

  • We’ll go contained in the cells of every desk row a hyperlink with the goal URL. 
1
...
2
<tbody>
3
  <tr>
4
    <td>
5
      <a href="PAGE_URL" goal="_blank">...</a>
6
    </td>
7
    <td>
8
      <a href="PAGE_URL" goal="_blank">...</a>
9
    </td>
10
    <td>
11
      <a href="PAGE_URL" goal="_blank">...</a>
12
    </td>
13
  </tr>
14
</tbody>
15
...

  • We’ll ensure that the desk borders are collapsed, so there aren’t any gaps between the hyperlinks.
Collapsed bordersCollapsed bordersCollapsed borders

  • We’ll add some kinds to the hyperlinks. Most significantly, we’ll make them block-level parts.

Right here’s the corresponding demo:

One factor to notice is that on small screens the place the texts of the primary columns wrap, the desk hyperlinks don’t have equal heights.

Cells with unequal heightsCells with unequal heightsCells with unequal heights

In fact, relying in your content material, you possibly can repair this in several methods, for instance:

  • In case your cells aren’t broad sufficient, you can also make the primary column of every row sticky and provides the desk white-space: nowrap.
  • You may lay out the desk rows and cells utilizing CSS Grid and flexbox—is perhaps tough!
  • You may also use JavaScript to calculate the hyperlinks’ heights of every desk row on DOM load and window resize and make them equal.

On this case, we’ll go along with the third answer that we must always use rigorously as efficiency issues. Fortunately sufficient, we’ll reuse one operate we’ve outlined in a earlier demo and simply make just a few changes.  If we wish, we will fireplace this code solely on small screens, however once more this is dependent upon the desk contents.

Table with equal height cells per rowTable with equal height cells per rowTable with equal height cells per row

Right here’s the JS wanted:

1
const tableRows = doc.querySelectorAll(".table-clickable tbody tr");
2

3
operate setTableLinksHeights() {
4
  for (const tableRow of tableRows) {
5
    const tableRowLinks = tableRow.querySelectorAll("a");
6
    setEqualHeights(tableRowLinks);
7
  }
8
}
9

10
operate setEqualHeights(el) {
11
  let counter = 0;
12
  for (let i = 0; i < el.size; i++) {
13
    el[i].model.removeProperty("top");
14
    const singleHeight = el[i].offsetHeight;
15

16
    if (counter < singleHeight) {
17
      counter = singleHeight;
18
    }
19
  }
20

21
  for (let i = 0; i < el.size; i++) {
22
    el[i].model.top = `${counter}px`;
23
  }
24
}
25

26
setTableLinksHeights();
27
window.addEventListener("resize", setTableLinksHeights);

JavaScript Methodology

Another strategy requires just a few traces of JavaScript to show desk rows into clickable parts.

The actions wanted are the next:

  • For every row, we’ll set a customized HTML attribute with the goal URL.
1
...
2
<tbody>
3
  <tr data-href="PAGE_URL">
4
    <td>...</td>
5
    <td>...</td>
6
    <td>...</td>
7
  </tr>
8
  ...
9
</tbody>
10
...

  • We’ll point out that desk rows are clickable parts by giving them cursor: pointer. Equally to the earlier instance, we’ll present just a few kinds for the hover state.
  • We’ll hear for the click on occasion on the desk rows, and upon firing, we’ll open the goal web page in a brand new browser tab.  
1
const tableRows = doc.querySelectorAll(".table-clickable tbody tr");
2

3
for (const tableRow of tableRows) {
4
  tableRow.addEventListener("click on", operate () {
5
    window.open(this.dataset.href, "_blank");
6
  });
7
}

It’s price noting that should you don’t care about opening a brand new tab, as an alternative of the open() technique, you need to use the href property like this:

1
window.location.href = this.dataset.href;

When you do that strategy, make certain to view the demo in debug mode.

We will omit the window prefix in each circumstances.

Right here’s the corresponding demo:

Bootstrap Tables

We utilized the strategies above in tables that use our personal kinds. However, we will simply embed them into tables styled by fashionable frameworks like Bootstrap.

Within the demo under, we model our desk with Bootstrap kinds (discover the additional lessons we go) and switch its rows into clicks utilizing the aforementioned second strategy. 

Conclusion

On this tutorial, we examined two methods of creating all the desk rows clickable. Alongside the way in which, we confirmed mix these methods with Bootstrap tables. These strategies are actually helpful whenever you listing posts/initiatives in tabular format and wish customers to navigate to their internal pages.

When you can consider one other approach, don’t neglect to share it with us!

As all the time, thanks rather a lot for studying!



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments