Improved table group by with sort option.
This commit is contained in:
parent
decb38ed4b
commit
2e4ba95f56
1 changed files with 63 additions and 53 deletions
|
@ -225,65 +225,45 @@ class App.ControllerTable extends App.Controller
|
||||||
events:
|
events:
|
||||||
click: @deleteRow
|
click: @deleteRow
|
||||||
|
|
||||||
if @orderDirection && @orderBy
|
if @orderDirection && @orderBy && !@groupBy
|
||||||
for header in @headers
|
@objects = @sortList(@objects)
|
||||||
if header.name is @orderBy
|
|
||||||
@objects = _.sortBy(
|
# group by
|
||||||
@objects
|
if @groupBy
|
||||||
(item) ->
|
|
||||||
# if we need to sort translated col.
|
# get groups
|
||||||
if header.translate
|
groupObjects = {}
|
||||||
App.i18n.translateInline(item[header.name])
|
for object in @objects
|
||||||
# if we need to sort a relation
|
group = object[@groupBy]
|
||||||
if header.relation
|
if !group
|
||||||
if item[header.name]
|
withId = "#{@groupBy}_id"
|
||||||
App[header.relation].findNative(item[header.name]).displayName()
|
|
||||||
else
|
if object[withId] && attributes[withId] && attributes[withId].relation
|
||||||
''
|
if App[attributes[withId].relation].exists(object[withId])
|
||||||
else
|
item = App[attributes[withId].relation].findNative(object[withId])
|
||||||
item[header.name]
|
if item && item.displayName
|
||||||
)
|
group = item.displayName().toLowerCase()
|
||||||
if @orderDirection is 'DESC'
|
|
||||||
header.sortOrderIcon = ['arrow-down', 'table-sort-arrow']
|
groupObjects[group] ||= []
|
||||||
@objects = @objects.reverse()
|
groupObjects[group].push object
|
||||||
else
|
|
||||||
header.sortOrderIcon = ['arrow-up', 'table-sort-arrow']
|
groupsSorted = []
|
||||||
else
|
for key of groupObjects
|
||||||
header.sortOrderIcon = undefined
|
groupsSorted.push key
|
||||||
|
groupsSorted = groupsSorted.sort()
|
||||||
|
|
||||||
|
# get new order
|
||||||
|
@objects = []
|
||||||
|
for group in groupsSorted
|
||||||
|
localObjects = @sortList(groupObjects[group])
|
||||||
|
@objects = @objects.concat localObjects
|
||||||
|
groupObjects[group] = [] # release old array
|
||||||
|
|
||||||
# execute header callback
|
# execute header callback
|
||||||
if @callbackHeader
|
if @callbackHeader
|
||||||
for callback in @callbackHeader
|
for callback in @callbackHeader
|
||||||
@headers = callback(@headers)
|
@headers = callback(@headers)
|
||||||
|
|
||||||
# group by
|
|
||||||
if @groupBy
|
|
||||||
|
|
||||||
# get new order
|
|
||||||
groupObjects = _.groupBy(
|
|
||||||
@objects
|
|
||||||
(item) =>
|
|
||||||
return '' if !item[@groupBy]
|
|
||||||
return item[@groupBy].displayName() if item[@groupBy].displayName
|
|
||||||
item[@groupBy]
|
|
||||||
)
|
|
||||||
groupOrder = []
|
|
||||||
for group, value of groupObjects
|
|
||||||
groupOrder.push group
|
|
||||||
|
|
||||||
# sort new groups
|
|
||||||
groupOrder = _.sortBy(
|
|
||||||
groupOrder
|
|
||||||
(item) ->
|
|
||||||
item
|
|
||||||
)
|
|
||||||
|
|
||||||
# create new data array
|
|
||||||
@objects = []
|
|
||||||
for group in groupOrder
|
|
||||||
@objects = @objects.concat groupObjects[group]
|
|
||||||
groupObjects[group] = [] # release old array
|
|
||||||
|
|
||||||
if @tableId
|
if @tableId
|
||||||
@calculateHeaderWidths()
|
@calculateHeaderWidths()
|
||||||
|
|
||||||
|
@ -425,6 +405,36 @@ class App.ControllerTable extends App.Controller
|
||||||
|
|
||||||
table
|
table
|
||||||
|
|
||||||
|
sortList: (objects) =>
|
||||||
|
|
||||||
|
for header in @headers
|
||||||
|
if header.name is @orderBy
|
||||||
|
objects = _.sortBy(
|
||||||
|
objects
|
||||||
|
(item) ->
|
||||||
|
# if we need to sort translated col.
|
||||||
|
if header.translate
|
||||||
|
return App.i18n.translateInline(item[header.name])
|
||||||
|
|
||||||
|
# if we need to sort by relation name
|
||||||
|
if header.relation
|
||||||
|
if item[header.name]
|
||||||
|
localItem = App[header.relation].findNative(item[header.name])
|
||||||
|
if localItem && localItem.displayName
|
||||||
|
localItem = localItem.displayName().toLowerCase()
|
||||||
|
return localItem
|
||||||
|
return ''
|
||||||
|
item[header.name]
|
||||||
|
)
|
||||||
|
if @orderDirection is 'DESC'
|
||||||
|
header.sortOrderIcon = ['arrow-down', 'table-sort-arrow']
|
||||||
|
objects = objects.reverse()
|
||||||
|
else
|
||||||
|
header.sortOrderIcon = ['arrow-up', 'table-sort-arrow']
|
||||||
|
else
|
||||||
|
header.sortOrderIcon = undefined
|
||||||
|
objects
|
||||||
|
|
||||||
# bind on delete dialog
|
# bind on delete dialog
|
||||||
deleteRow: (id, e) =>
|
deleteRow: (id, e) =>
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
|
|
Loading…
Reference in a new issue