igniteui-angular icon indicating copy to clipboard operation
igniteui-angular copied to clipboard

igx-grid - An error "Cannot convert undefined or null to object" occurs when autoGenerate = true, if the initial data is unset or empty array.

Open tkiryu opened this issue 8 years ago • 1 comments

Description

igx-grid - An error "Cannot convert undefined or null to object" occurs when autoGenerate = true, if the initial data is unset or empty array.

Steps to reproduce

  1. Use [email protected].
  2. Configure igx-grid as follows.
<igx-grid [data]="" [autoGenerate]="true"></igx-grid>

Or like this.

<igx-grid [data]="[]" [autoGenerate]="true"></igx-grid>

Result

2018-03-26 8 20 17

Expected result

The error should not occur. In other words, igx-grid with autoGenerate=true should accept empty data.

Current workaround

Put empty object in the array.

<igx-grid [data]="[{}]" [autoGenerate]="true"></igx-grid>

tkiryu avatar Mar 25 '18 23:03 tkiryu

We are aware of this behavior and it is documented in the official grid doc.

If the data property is undefined or empty string this will result in an error when the grid tries to bind itself.

Note: In the code below, you may wonder why are we setting the records property to an empty array before subscribing to the service. The Http request is asynchronous, and until it completes, the records property will be undefined which will result in an error when the grid tries to bind to it. You should either initialize it with a default value or use a BehaviorSubject.

Also about the autoGenerate property, we are again mentioning that if there is no data, the grid will throw an error.

Note: The grid autoGenerate property is best to be avoided when binding to remote data for now. It assumes that the data is available in order to inspect it and generate the appropriate columns. This is usually not the case until the remote service responds, and the grid will throw an error. Making autoGenerate available, when binding to remote service, is on our roadmap for future versions.

zdrawku avatar Mar 26 '18 10:03 zdrawku