t348575
t348575

Reputation: 753

Complementing of boolean not working, with ngIf in ngx-datatable

onSelect() is called whenever a row in ngx-datatable is selected. isNodeSelected for some reason is not getting complemented when the same row is selected, even though it enters that if statement. isNodeSelected is used with ngIf to display buttons. Webstorm highlights the complement statement and says 'narrowed to true'

onSelect({ selected }) {
    this.isNodeSelected = true;
    if (Array.isArray(this.selected) && this.selected.length === 1) {
      if (this.selected[0].hasOwnProperty('id') && this.selected[0].id === selected[0].id) {
        this.isNodeSelected = !this.isNodeSelected;
      } else if (this.selected[0].hasOwnProperty('id') && this.selected[0].id !== selected[0].id) {
        this.selected = selected;
        this.isNodeSelected = true;
      }
    } else {
      this.selected = selected;
      this.isNodeSelected = true;
    }
  }

Upvotes: 0

Views: 183

Answers (1)

JMP
JMP

Reputation: 1934

If this function is in your component.ts file then at least you need to replace:

onSelect({ selected }) {

with

onSelect(selected) {

as the curly brace is used to insert variables values in the template, not in the code behind.

Other than that, step through each bit with your debugger to check the values at each point.

E.g. does it enter the second if statement, if so, what is the value of selected[0].id etc.

Upvotes: 1

Related Questions