Kevin asks,
Is there a Drupal Add On or Widget that would allow users to sort tables as they're displayed on the website?
Hi, Kevin,
Several options here:
Views
When a table is built using views, Drupal has click-sorting on table headers built in, which works well for most scenarios - although depending on where the data is coming from, some columns may not be sortable.
This can handle any amount of data, and sorting on the headers will sort across all of the data (but only one sort at a time, it's not quite like Excel where you can pick multiple column sorts on the fly).
For this, each row is an item of content -- this means you'd need to be reporting across a bunch of content items in the system.
TableField
There's https://www.drupal.org/project/tablefield , which allows setting up a field on content types (or on a block that could be placed using layout builder) that you can upload a CSV or enter data manually. With the data in a specific field, it should be relatively easy to add any of a bunch of different Javascript libraries -- it mentions “tablesorter” but that looks unmaintained. Here's a list of 19 different libraries, most of which should work: https://flatlogic.com/blog/top-19-remarkable-javascript-table-libraries-and-plugins/ ... I've used Vuetify datatables, but had to move away because they didn't get a version for Vue3, and Ag-Grid.
We can create a “Single Directory Component” for this to use any of those libraries. There's a bunch of other features these can include, although tablefield itself doesn't have any provision for paging data, so probably not the way to go if you're dealing with more than a couple hundred rows.
JS table enhancement
Some of those Javascript table libraries can “decorate” existing HTML tables. So if these are small tables just copied and pasted into the wysiwyg, we could provide the necessary code to add a sorting function like https://github.hubspot.com/sortable/docs/welcome/ . There are probably a couple other JS options that could also be used this way. This is the most finicky way to do it, and fairly easy for editors to accidentally break if they're not careful -- but on the other hand, tables can be easily interspersed this way inside a long article.
So it boils down to where the data is coming from, where they want to show it, how much data there is, and how they want to manage these tables!
Add new comment