d-draggable-table
Version:
Derby draggable table component based on derby sink.
54 lines (44 loc) • 1.37 kB
text/coffeescript
_ = require 'lodash'
defaultData = [
{ cells: [1, 'a', '2', 'helo', '1'] },
{ cells: [3, 'b', '1', 'world', '3'] },
{ cells: [2, 'd', '3', 'test', 'a'] },
{ cells: [4, 'c', '4', 'string', 'c'] }
]
bubbleSort = (list, field, model) ->
anySwaps = false
swapPass = ->
for r in [0..list.length-2]
if list[r].cells[field] > list[r+1].cells[field]
anySwaps = true
model.move('table', r, r+1)
swapPass()
while anySwaps
anySwaps = false
swapPass()
list
StringSorter = (algorithm) ->
sort: (list, field, model) -> algorithm list, field, model
module.exports = class DraggableTable
view: __dirname
init: ->
create: ->
= .get('table') || defaultData
= .get('headers') || [1..[0].cells.length]
.set('table', )
.set('headers', )
onRowMove: (from, to) ->
.move('table', from, to)
onColMove: (from, to) ->
sort: (field) ->
field = field|0
table = .get 'table'
if .get('sortBy') is field
.set 'desc', !.get('desc')
else
.set 'desc', false
sorter = new StringSorter(bubbleSort)
sorter.sort(table, field, )
table.reverse() if .get("desc")
.set 'sortBy', field
.set 'refresh', !.get('refresh')