Javascript function to sort tables
- Copy in the dentropize.js to your project or use jsDelivr.
vendor/javascript/dentropize.js
for Rails.
- Import the function
- Add to the appropriate event for your framework.
- Example below is a Rails 7 app using Importmaps and Turbo
// app/javascript/application.js
import dentropize from "dentropize"
document.addEventListener('turbo:load', dentropize);
# config/importmap.rb
pin "dentropize", to: "dentropize.js"
- Add the CSS class "sortable" to your table.
- To sort by a hidden value use "data-sort"
<td data-sort="5">five</td>
- This code is essentially a heavily modified sortable made to work like jquery tablesorter but only the parts you really REALLY need.
- There is no requirement for a
<tbody>
instead any rows NOT in a<thead>
or<tfoot>
are sorted.