IgniteUI / igniteui-angular

Ignite UI for Angular is a complete library of Angular-native, Material-based Angular UI components with the fastest grids and charts, Pivot Grid, Dock Manager, Hierarchical Grid, and more.
https://www.infragistics.com/products/ignite-ui-angular
Other
571 stars 160 forks source link

Grid with DefaultSortingStrategy throws error #14592

Closed georgianastasov closed 2 weeks ago

georgianastasov commented 1 month ago

Description

When using DefaultSortingStrategy with any grid component (igx-grid, igx-tree-grid, igx-hierarchical-grid), an error occurs in the console related to sorting functionality.

Steps to reproduce

  1. Create a grid component (igx-grid, igx-tree-grid, or igx-hierarchical-grid) in your Angular application.

  2. Add the following sorting strategy to your component class: public defaultSortingStrategy = DefaultSortingStrategy.instance();

  3. Bind the defaultSortingStrategy to the grid's [sortStrategy] property: <igx-grid #grid [data]="data" [sortStrategy]="defaultSortingStrategy">

  4. Run the application and observe the console for errors.

Result

An error is thrown in the console:

ERROR TypeError: Cannot read properties of undefined (reading 'call')
    at _DefaultSortingStrategy.compareObjects (sorting-strategy.ts:85:31)
    at cmpFunc (sorting-strategy.ts:57:56)
    at Array.sort (<anonymous>)
    at _DefaultSortingStrategy.arraySort (sorting-strategy.ts:95:21)
    at _DefaultSortingStrategy.sort (sorting-strategy.ts:58:21)
    at _DataUtil.sort (data-util.ts:52:24)
    at _DataUtil.treeGridSort (data-util.ts:70:24)
    at data-util.ts:65:41
    at Array.forEach (<anonymous>)
    at _DataUtil.treeGridSort (data-util.ts:61:26)

Expected result

The grid should sort data correctly without any errors in the console.

RivaIvanova commented 2 weeks ago

The documentation states that: sorting-column-grid

The grid sortStrategy is of type IGridSortingStrategy and the sort method is as follows:

sort(data: any[], expressions: ISortingExpression[], grid?: GridType): any[]

The column sortStrategy is of type ISortingStrategy and the sort method is as follows:

sort: (
    data: any[],
    fieldName: string,
    dir: SortingDirection,
    ignoreCase: boolean,
    valueResolver: (obj: any, key: string, isDate?: boolean) => any,
    isDate?: boolean,
    isTime?: boolean,
    grid?: GridType
) => any[];

The DefaultSortingStrategy implements the ISortingStrategy and its sort method requires the same parameters: sort(data, fieldName, dir, ignoreCase, valueResolver, isDate?, isTime?): any[]

So the DefaultSortingStrategy cannot be assigned to the grid sortStrategy since they are of different types and the DefaultSortingStrategy is intended to be used on a column level not on the grid level.

If we proceed with the Steps to reproduce, the app cannot be run due to a compilation error which basically says the above. defaultSortingStrategy - column

The equivalent of the DefaultSortingStrategy for the grid is the IgxSorting class:

public defaultGridSortingStrategy = new IgxSorting();
public defaultColumnSortingStrategy = DefaultSortingStrategy.instance();
<igx-grid [data]="data" [sortStrategy]="defaultGridSortingStrategy">
  <igx-column
    field="ID"
    [sortable]="true"
    [sortStrategy]="defaultColumnSortingStrategy"
  ></igx-column>
</igx-grid>