ngx-datatable is unable to assign a new row model when using [rows]="rows | async"

I am currently working with Angular 2 (version 4.1.0), angularfire2, and ngx-datatable.

Within my component, I have a datatable that renders an Observable based on Angularfire2. Users can filter the data in the name column using a similar implementation to the ngx-datatable filter demo. When querying firebase for a new result set and attempting to assign this new Observable to the row model of the datatable, I encounter the following error:

ERROR TypeError: Cannot read property '0' of null
    at DataTableBodyComponent../src/components/body/body.component.ts.DataTableBodyComponent.updateRows (index.js:4384)
    at DataTableBodyComponent../src/components/body/body.component.ts.DataTableBodyComponent.recalcLayout (index.js:4599)
    at DataTableBodyComponent.set [as rows] (index.js:4185)
    at updateProp (core.es5.js:10966)
    at checkAndUpdateDirectiveDynamic (core.es5.js:10726)
    ...

Expected behavior:

After applying a filter and retrieving the new Observable, I expect the datatable to render the updated data.

Key code snippets are provided below:

HTML, Datatable (using [rows]="rows | async" directive):

...
<ngx-datatable #table [rows]="rows | async" 
 [columnMode]="'flex'" [headerHeight]="50" [footerHeight]="50" [rowHeight]="'auto'"
 [limit]="10" class="material striped" [selected]="selected" [selectionType]="'single'"
 (select)='onSelect($event)'>
...

The component logic:

@Input()
  set onFilteringList (filter: string) {
    this._onFilteringList = filter;
    // Assigning the filtered data after rendering causes an error!
    this.rows = this.workItemSrv.getFilteredByUserWorkItemList(this.onFilteringList);
  }
ngOnInit() { // Operating fine here 
    if (this.onFilteringList) {
      this.rows = this.workItemSrv.getFilteredByUserWorkItemList(this.onFilteringList);
    } else {
      this.rows = this.workItemSrv.getWorkItemList();
    }
  }

I have attempted three different methods to display a filtered result set:

  1. Routing: using route parameters as filters seems promising, but the component is not reinitialized upon navigation. This leads to errors after the initial routing and table rendering (refer to the code snippets).

  2. Using firebase query methods, it becomes challenging to define a query with parameters to retrieve either all items or a subset based on a filter. Employing two Observables with and without filters may be a solution, but swapping the row model results in the aforementioned error.

  3. Utilizing rxjs filter method has not yielded success in applying a filter on the Observable.

None of these approaches have resolved my issue. How can I efficiently change the row model of the datatable or apply filtering directly on the Observable?

Thanks, Markus

Answer №1

To optimize performance, consider eliminating the use of async data model:

...
<ngx-datatable #table [rows]="rows" 
...

Update the component like so:

@Input()
set onFilteringList (filter: string) {
  this._onFilteringList = filter;
  this.workItemSrv.getFilteredByUserWorkItemList(this.onFilteringList)
    .subscribe(rows => this.rows = rows);
}

ngOnInit() { 
  if (this.onFilteringList) {
    this.rows = 
    this.workItemSrv.getFilteredByUserWorkItemList(this.onFilteringList)
       .subscribe(rows => this.rows = rows);
  } else {
    this.rows = this.workItemSrv.getWorkItemList()
      .subscribe(rows => this.rows = rows);
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

My Angular project is not being recognized by the Jenkins pipeline

I am currently working on a project with a more intricate structure, which looks like this: 'rootFolder/frontendParentFolder/actualAngularProject' Within my Jenkins job, I am attempting to execute the code coverage task for the angular project. ...

Unusual behavior exhibited by ng-if within a widget

Hey there, seeking some assistance here. I'm currently making edits to a widget and within the client HTML code, I've come across two ng-if statements (the first one was already there, I added the second). <li> <a ng-if="data.closed ...

Activate a function to focus on an input field once ngIf condition becomes true and the input is revealed

I am currently attempting to focus the cursor on a specific input field that is only displayed when the condition of the surrounding ngIf directive is true. Here is an example of the HTML code structure: <div> <button (click)="showFirst = ...

The compiler is still throwing missing return errors despite narrowing down all potential types

I encountered the following issue: Function is missing a closing return statement and its return type does not include 'undefined'. Here's my TypeScript code snippet: function decodeData( data: string | number[] | ArrayBuffer | Uint8Arr ...

The utilization of angular2-materialize is not possible due to an error message indicating that jQuery.easing is undefined

Greetings to all who are taking the time to read this. I am encountering an issue while trying to run ng serve. Here is the error message I am receiving: TypeError: jQuery.easing is undefined Here is a breakdown of what I have done so far: ng new X cd X ...

Adding an element to an array in Angular 2 when an object is clicked

Whenever a row of objects is clicked, I want it to be added to the selectedProducts array. Within my TypeScript file: selectedProducts: Product[] = []; select(product) { this.selectedProducts.push(product); console.log(this.selectedProducts); ...

Can Angular be used to send form data to an external URL?

Let's take a look at an example with some code: <form method="post" action="URL"> <input type="text" name="first name" /> <input type="text" name="last name"/> <input type="submit" value="Submit" name="submit"/> < ...

Creating a callback function within its own definition

I need help with my download function that is calling a callback to "downloadCallback". Is it possible to define the downloadCallback function inside the download function itself? If so, how can I achieve this? Below is the implementation of the download ...

Disabling flow control to test an Angular app in Protractor results in an error being generated

I've been battling with this issue for quite some time now and my energy is running low. I am currently attempting to test an Angular application using protractor and async/await. The documentation suggests that I need to disable the control flow by a ...

Unit testing the TypeScript function with Karma, which takes NgForm as a parameter

As I work on writing unit tests for a specific method, I encounter the following code: public addCred:boolean=true; public credName:any; public addMachineCredential(credentialForm: NgForm) { this.addCred = true; this.credName = credentialForm.val ...

Maintaining accurate type-hinting with Typescript's external modules

Before I ask my question, I want to mention that I am utilizing Intellij IDEA. In reference to this inquiry: How do you prevent naming conflicts when external typescript modules do not have a module name? Imagine I have two Rectangle classes in different ...

Arrange text and a button side by side in a table cell using an Angular directive and an HTML template

I have successfully implemented an Angular search function on my project. You can find it here to allow users to search for courses. The search function uses a read-more directive that displays a preview of the description and keywords before allowing use ...

Data has not been loaded into the Angular NGX Datatable

As a beginner in Angular, I am struggling to set data from the module. ngOnInit() { this.populate(); } public populate() { this.productService.getAllProduct('6f453f89-274d-4462-9e4b-c42ae60344e4').subscribe(prod => { this. ...

Building dynamic input forms within Angular2

In order to customize the form for each guest, I aim to prompt users to provide their name, grade, and age for every guest they wish to invite. Initially, I inquire about the number of guests they plan to have. Subsequently, I intend to present a tailored ...

Error in Redux reducer file caused by an incorrect data type in action.payload

I have encountered a type error in my reducers' file where it says that my 'Property 'address' does not exist on type 'number | { connection: boolean; address: string; }'. This issue is arising in my React application while us ...

Navigate between tabs with ease for children

Setting up my routes has been a bit challenging. I created a listRoutes in my app-routing.module.ts with some parameters. const listRoutes: Routes = [ { path: '', component: MlsComponent, }, { path: 'vente', compon ...

Is the parent component not triggering the function properly?

Hey there, I'm working with the code snippet below in this component: <app-steps #appSteps [menuSteps]="steps" [currentComponent]="outlet?.component" (currentStepChange)="currentStep = $event"> <div appStep ...

The error message "angular4: No directive matching with exportAs set to ngModel" is being encountered

Currently encountering an issue with angular 4 Encountering a problem where there is no directive with "exportAs" set to "ngModel" ("" name="fullname" type="text" required maxlength="30" [(ngModel)="model.fullname" [ERROR ->]#f ...

How come this constant can be accessed before it has even been declared?

I find it fascinating that I can use this constant even before declaring it. The code below is functioning perfectly: import { relations } from 'drizzle-orm' import { index, integer, pgTable, serial, uniqueIndex, varchar } from 'drizzle-orm ...

nx serve is failing to recognize the import statement for "@angular/localize/init"

Currently, I am in the process of incorporating @angular/localize into my project within an Nx workspace ([email protected]). To achieve this, I am adhering to the official guidelines outlined at: https://angular.io/guide/i18n-overview In particular, ...