@@ -8,9 +8,12 @@ import { TableRecord } from "../util/table";
88import arrayToTree , { Tree } from "array-to-tree" ;
99import { getObject } from "@jeltemx/mendix-react-widget-utils" ;
1010import { ValidationMessage } from "../util/validation" ;
11+ import sortBy from "lodash/sortBy" ;
1112
1213configure ( { enforceActions : "observed" } ) ;
1314
15+ export type SortingType = "none" | "asc" | "desc" ;
16+
1417export interface TableGuids {
1518 context : string | null ;
1619 rows ?: string [ ] ;
@@ -26,6 +29,8 @@ export interface TableStoreConstructorOptions {
2629 reloadEntriesOnColChange ?: boolean ;
2730 reloadEntriesOnRowChange ?: boolean ;
2831 validationMessages ?: ValidationMessage [ ] ;
32+ sortingTypeRows ?: SortingType ;
33+ sortingTypeColumns ?: SortingType ;
2934}
3035
3136export interface TableObjectOptions extends TableObjectGetOptions { }
@@ -54,12 +59,22 @@ export class TableStore {
5459
5560 private reloadOnColChange = true ;
5661 private reloadOnRowChange = true ;
62+ private sortingTypeRows : SortingType ;
63+ private sortingTypeColumns : SortingType ;
5764
5865 // Flow actions
5966
6067 // Constructor
6168 constructor ( opts : TableStoreConstructorOptions ) {
62- const { contextObject, subscriptionHandler, onSelectionChange, entriesLoader, validationMessages } = opts ;
69+ const {
70+ contextObject,
71+ subscriptionHandler,
72+ onSelectionChange,
73+ entriesLoader,
74+ validationMessages,
75+ sortingTypeRows,
76+ sortingTypeColumns
77+ } = opts ;
6378
6479 this . isLoading = false ;
6580 this . contextObject = contextObject || null ;
@@ -72,6 +87,9 @@ export class TableStore {
7287 this . entriesLoader = entriesLoader || ( ( _guids : TableGuids ) : void => { } ) ;
7388 this . validationMessages = validationMessages || [ ] ;
7489
90+ this . sortingTypeRows = sortingTypeRows || "none" ;
91+ this . sortingTypeColumns = sortingTypeColumns || "none" ;
92+
7593 if ( entriesLoader ) {
7694 when (
7795 ( ) => {
@@ -354,13 +372,21 @@ export class TableStore {
354372
355373 @computed
356374 get tableColumns ( ) : Array < ColumnProps < TableRecord > > {
357- return this . columns . map ( col => ( {
375+ const columns = this . columns . map ( col => ( {
358376 key : col . id ,
359377 dataIndex : col . id ,
360378 guid : col . guid ,
361379 title : col . title ,
380+ sortKey : col . _sortKey ,
362381 className : col . className
363382 } ) ) ;
383+
384+ if ( this . sortingTypeColumns === "asc" ) {
385+ return sortBy ( columns , "sortKey" ) ;
386+ } else if ( this . sortingTypeColumns === "desc" ) {
387+ return sortBy ( columns , "sortKey" ) . reverse ( ) ;
388+ }
389+ return columns ;
364390 }
365391
366392 @computed
@@ -369,6 +395,7 @@ export class TableStore {
369395 const record : TableRecord = {
370396 key : row . guid ,
371397 guid : row . guid ,
398+ sortKey : row . sortKey ,
372399 "row-title" : row . title ,
373400 _classObj : { } ,
374401 _className : row . className
@@ -399,7 +426,14 @@ export class TableStore {
399426 return record ;
400427 } ) ;
401428
402- const tree = arrayToTree ( rows , arrayToTreeOpts ) ;
429+ const tree = arrayToTree (
430+ this . sortingTypeRows === "none"
431+ ? rows
432+ : this . sortingTypeRows === "asc"
433+ ? sortBy ( rows , "sortKey" )
434+ : sortBy ( rows , "sortKey" ) . reverse ( ) ,
435+ arrayToTreeOpts
436+ ) ;
403437 return tree . filter ( treeEl => typeof treeEl . _parent === "undefined" && ! treeEl . _parent ) ;
404438 }
405439
0 commit comments