logoalt Hacker News

hk__2today at 3:45 PM6 repliesview on HN

Related to this, I’d love that HTML natively support sortable tables. This is a common need but every single time I have to reimplement it.


Replies

dansalvatotoday at 8:39 PM

I had always thought that table sorting seems like the perfect job for HTML, given its data-driven nature, and the possibility of implementing sort information into the attributes of table cell elements.

But thinking about it: Is there a precedent for HTML to be able to include information that instructs the device to present the DOM out of order? Maybe it's out of scope for HTML to have information about presentation order, when the order is supposed to be implied strictly by the DOM hierarchy itself.

Of course, CSS can visually reorder stuff (e.g. `order` on flex/grid items), but MDN has accessibility warnings regarding the use of `order` and visually presenting the data in an order not reflected by the DOM. So, maybe sorting is best done by reordering the DOM after all.

jwrtoday at 6:54 PM

This is… not obvious. It's only easy if you want the simplistic approach of sorting lexicographically or numerically. But in more complex tables, you will often encounter cases where the data representation for sorting is different from data representation for display.

Of course most JavaScript-based tables get this wrong, too.

show 1 reply
hahn-kevtoday at 5:39 PM

I feel like there's so few times it would actually work how I want it that it seems like there's little point.

wild_eggtoday at 4:33 PM

You want client side table sorting?

It's really easy to do this with server rendered HTML. Put a link with the sort param in the column headers and be done.

show 1 reply
krapptoday at 4:10 PM

That's what javascript is supposed to be for. All of this is what javascript is supposed to be for.

HTML describes layout, CSS describes style, JS adds interactivity.

Want sortable tables? Get a jquery plugin and spend five minutes, done.

This was all solved a decade or more ago.