DEV Community

Cover image for No Javascript Table with Pagination
Takane Ichinose
Takane Ichinose

Posted on

7

No Javascript Table with Pagination

Pagination made entirely by CSS. There is no single Javascript code here.

I used the radio button hack, and HTML and CSS loop to implement the pagination.

To summarize the hack, I written the codes below:

Here is the HTML code

<!--This is the radio button for hacking. They should not be visible-->
<input type="radio" name="btn_radio" id="trigger1" checked />
<!--This is the table which we show and hide. It must be direct sibling of the radio button-->
<table>
  <tr>
    <td>TABLE 1</td>
  </tr>
</table>
<input type="radio" name="btn_radio" id="trigger2" />
<table>
  <tr>
    <td>TABLE 2</td>
  </tr>
</table>
<label for="trigger1">Page 1</label>
<label for="trigger2">Page 2</label>
Enter fullscreen mode Exit fullscreen mode

Here is the CSS code:

input[type="radio"] {
  display: none;
}
table {
  display: none;
}
/* Plus sign is a selector for direct sibling element */
input[type="radio"]:checked + table {
  display: block;
}
Enter fullscreen mode Exit fullscreen mode

Easy right? Just use your favorite HTML preprocessor, then you'll be able to populate the table, and the pagination links.

I also took advantage on

position: sticky;

CSS attribute, to have better view for layout.

If you compile the Pug source code into HTML, you will see the repetitive, pagination Elements, and "Showing x to y of z items".

The buttons above aren't working. Though, I still could add modal dialog for "Add New Data" function. It is out of the scope of my current pen, so I didn't add it.

But if I am going to add, I have 2 methods to do so:

  • First, is the target CSS pseudo-class. I just have to put an ID to a modal/dialog div <div id="my_modal"></div> in HTML. Then, I am going to set #my_modal { display: none; } to the modal dialog. Then, I am going to use the target pseudo-class like this #my_modal:target { display: block; } to show the dialog. To trigger it, I need a <a href="#my_modal">link</a> to trigger the effect.
  • Second is the checkbox hack. It is just similar with this Pagination hack.

Here is the preview of the Pagination.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
reobin profile image
Robin Gagnon

As absurd as they seem sometimes, I just love me some css only implementations of "complex" UI.

I tend to do it too much, but it’s always tempting to let javascript aside.

Collapse
 
takaneichinose profile image
Takane Ichinose

Yes, I'm just doing these things to challenge myself, and to improve my CSS skills. But in the end of the day, Javascript implementation will be more readable, in terms of coding, in my opinion.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️