Files
nairobi/app/assets/javascripts/table_sortable.js.coffee
Javi Martín 936aa7af0f Simplify adding rows to sortable tables
The `append` method can handle arrays, so there's no need to loop
through each element.

There's more to improve on this method, like the `asc` variable being
global to a table instead of depending on the current column, but for
now I'm only refactoring and maintaining the current behaviour.
2019-09-11 03:14:16 +02:00

23 lines
676 B
CoffeeScript

App.TableSortable =
getCellValue: (row, index) ->
$(row).children("td").eq(index).text()
comparer: (index) ->
(a, b) ->
valA = App.TableSortable.getCellValue(a, index)
valB = App.TableSortable.getCellValue(b, index)
return if $.isNumeric(valA) and $.isNumeric(valB) then valA - valB else valA.localeCompare(valB)
initialize: ->
$("table.sortable th").click ->
table = $(this).parents("table").eq(0)
rows = table.find("tr:gt(0)").not("tfoot tr").toArray().sort(App.TableSortable.comparer($(this).index()))
@asc = !@asc
if @asc
table.append rows
else
table.append rows.reverse()
return