Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Tree Data should work without initial sort, fixes #394 #395

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/examples/slickgrid/Example27.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ export default class Example27 extends React.Component<Props, State> {

// you can optionally sort by a different column and/or sort direction
// this is the recommend approach, unless you are 100% that your original array is already sorted (in most cases it's not)
initialSort: {
columnId: 'title',
direction: 'ASC'
},
// initialSort: {
// columnId: 'title',
// direction: 'ASC'
// },
// we can also add a custom Formatter just for the title text portion
titleFormatter: (_row, _cell, value, _def, dataContext) => {
let prefix = '';
Expand Down
18 changes: 6 additions & 12 deletions src/slickgrid-react/components/slickgrid-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1556,18 +1556,12 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP
if (this._isDatasetHierarchicalInitialized && this.datasetHierarchical) {
sortedDatasetResult = this.treeDataService.sortHierarchicalDataset(this.datasetHierarchical);
flatDatasetOutput = sortedDatasetResult.flat;
} else if (Array.isArray(flatDatasetInput) && flatDatasetInput.length > 0) {
if (this._gridOptions?.treeDataOptions?.initialSort) {
// else we need to first convert the flat dataset to a hierarchical dataset and then sort
sortedDatasetResult = this.treeDataService.convertFlatParentChildToTreeDatasetAndSort(flatDatasetInput, this._columnDefinitions, this._gridOptions);
this.sharedService.hierarchicalDataset = sortedDatasetResult.hierarchical;
flatDatasetOutput = sortedDatasetResult.flat;
} else {
// else we assume that the user provided an array that is already sorted (user's responsability)
// and so we can simply convert the array to a tree structure and we're done, no need to sort
this.sharedService.hierarchicalDataset = this.treeDataService.convertFlatParentChildToTreeDataset(flatDatasetInput, this.gridOptions);
flatDatasetOutput = flatDatasetInput || [];
}
} else if (this._gridOptions && Array.isArray(flatDatasetInput) && flatDatasetInput.length > 0) {
// we need to first convert the flat dataset to a hierarchical dataset and then sort it
// we'll also add props, by mutation, required by the TreeDataService on the flat array like `__hasChildren`, `parentId` and anything else to work properly
sortedDatasetResult = this.treeDataService.convertFlatParentChildToTreeDatasetAndSort(flatDatasetInput, this._columnDefinitions, this._gridOptions);
this.sharedService.hierarchicalDataset = sortedDatasetResult.hierarchical;
flatDatasetOutput = sortedDatasetResult.flat;
}

// if we add/remove item(s) from the dataset, we need to also refresh our tree data filters
Expand Down