Skip to content

Commit 4264f0a

Browse files
committed
Fix order of grouping is now fixed when sorting
1 parent 6516860 commit 4264f0a

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/controls/listView/IListView.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { IColumn ,IGroup} from 'office-ui-fabric-react/lib/components/DetailsLis
33

44
export { SelectionMode };
55

6+
export enum GroupOrder {
7+
ascending = 1,
8+
descending
9+
}
10+
611
export interface IListViewProps {
712

813
/**
@@ -20,7 +25,7 @@ export interface IListViewProps {
2025
/**
2126
* The fields you want to group your list view by
2227
*/
23-
groupByFields?: string[];
28+
groupByFields?: IGrouping[];
2429
/**
2530
* Boolean value to indicate if the component should render in compact mode.
2631
* Set to false by default
@@ -52,6 +57,16 @@ export interface IListViewState {
5257
groups?: IGroup[];
5358
}
5459

60+
export interface IGrouping {
61+
name: string;
62+
order: GroupOrder;
63+
}
64+
65+
export interface IGroupsItems {
66+
items: any[];
67+
groups: IGroup[];
68+
}
69+
5570
export interface IViewField {
5671

5772
/**

src/controls/listView/ListView.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as React from 'react';
22
import { DetailsList, DetailsListLayoutMode, Selection, SelectionMode, IGroup } from 'office-ui-fabric-react/lib/DetailsList';
3-
import { IListViewProps, IListViewState, IViewField } from './IListView';
3+
import { IListViewProps, IListViewState, IViewField, IGrouping, GroupOrder } from './IListView';
44
import { IColumn } from 'office-ui-fabric-react/lib/components/DetailsList';
55
import { findIndex, has, sortBy, isEqual, cloneDeep } from '@microsoft/sp-lodash-subset';
66
import { FileTypeIcon, IconType } from '../fileTypeIcon/index';
77
import * as strings from 'ControlStrings';
8+
import { IGroupsItems } from '../../../lib/ListView';
89

910
/**
1011
* File type icon component
@@ -55,7 +56,7 @@ export class ListView extends React.Component<IListViewProps, IListViewState> {
5556
* @param items
5657
* @param groupByFields
5758
*/
58-
private _getGroups(items: any[], groupByFields: string[], level: number = 0, startIndex: number = 0): {items: any[], groups: IGroup[]} {
59+
private _getGroups(items: any[], groupByFields: IGrouping[], level: number = 0, startIndex: number = 0): IGroupsItems {
5960
// Group array which stores the configured grouping
6061
let groups: IGroup[] = [];
6162
let updatedItemsOrder: any[] = [];
@@ -67,7 +68,7 @@ export class ListView extends React.Component<IListViewProps, IListViewState> {
6768
// Create grouped items object
6869
const groupedItems = {};
6970
items.forEach((item: any) => {
70-
let groupName = item[groupField];
71+
let groupName = item[groupField.name];
7172
// Check if the group name exists
7273
if (typeof groupName === "undefined") {
7374
// Set the default empty label for the field
@@ -86,8 +87,16 @@ export class ListView extends React.Component<IListViewProps, IListViewState> {
8687
groupedItems[groupName].push(item);
8788
});
8889

90+
// Sort the grouped items object by its key
91+
const sortedGroups = {};
92+
let groupNames = Object.keys(groupedItems);
93+
groupNames = groupField.order === GroupOrder.ascending ? groupNames.sort() : groupNames.sort().reverse();
94+
groupNames.forEach((key: string) => {
95+
sortedGroups[key] = groupedItems[key];
96+
});
97+
8998
// Loop over all the groups
90-
for (const groupItems in groupedItems) {
99+
for (const groupItems in sortedGroups) {
91100
// Retrieve the total number of items per group
92101
const totalItems = groupedItems[groupItems].length;
93102
// Create the new group

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { escape } from '@microsoft/sp-lodash-subset';
55
import { FileTypeIcon, IconType, ApplicationType, ImageSize } from '../../../FileTypeIcon';
66
import { Dropdown, IDropdownOption } from 'office-ui-fabric-react/lib/components/Dropdown';
77
import { Placeholder } from '../../../Placeholder';
8-
import { ListView, IViewField, SelectionMode } from '../../../ListView';
8+
import { ListView, IViewField, SelectionMode, GroupOrder, IGrouping } from '../../../ListView';
99
import { SPHttpClient } from '@microsoft/sp-http';
1010
import { SiteBreadcrumb } from '../../../SiteBreadcrumb';
1111

@@ -114,7 +114,7 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
114114

115115
// Specify the fields on which you want to group your items
116116
// Grouping is takes the field order into account from the array
117-
const groupByFields: string[] = ["ListItemAllFields.Title"];
117+
const groupByFields: IGrouping[] = [{name: "ListItemAllFields.City", order: GroupOrder.ascending }, {name: "ListItemAllFields.Country.Label", order: GroupOrder.descending}];
118118

119119
return (
120120
<div className={styles.controlsTest}>

0 commit comments

Comments
 (0)