Haseoh
Haseoh

Reputation: 930

Cannot read property of undefined inside the row expansion

Befote I wrote this question I checked most of similar cases to mine, but with no success. I'm trying to display number and auth inside expandable row - I'm using PrimeNG. No matter what I try, nothing works. Since I'm out of fuel for this problem I need your help.

Simplified code:

JSON

{
"status": 0,
"dallases": [{
    "vehicle_id": 17954,
    "dallassettings": "3",
    "dallasupdated": "False",
    "dallas_list": [{
        "number": 666111222,
        "auth": 3
    }, {
        "number": 666777888,
        "auth": 4
    }, {
        "number": 123454321,
        "auth": 4
    }]
}
}

Service

export class VehicleService {
    private defUrl = 'dummy.url';

constructor(private http: Http) { }
getVehicle(username?: string, password?: string) {
    const url = (!username || !password) ? this.defUrl : 'dummy.url' + username + '/' + Md5.hashStr(password);
    return this.http.get(url)
        .map(res => res.json());

Component

export class VehicleComponent implements OnInit {

  cols: any[];

  ngOnInit() {
    this.cols = [
      { field: 'vehicle_id', header: "Vehicle ID" },
      { field: 'dallassettings', header: 'Dallas settings' },
      { field: 'dallasupdated', header: 'Dallas updated' },
      { field: 'dallas_list', header: 'Dallas list' }
    ];

  public vehicles: GeneralVehicle[];

  constructor(private vehicleService: VehicleService, private router: Router) {
    this.vehicleService.getVehicle().subscribe(vehicle => {
      this.vehicles = vehicle;
    });
  }

interface GeneralVehicle {
  status: number;
  dallases: Vehicle[];
}

interface Vehicle {
  vehicle_id: number;
  dallassettings: string;
  dallasupdated: string;
  dallas_list: DallasList[];
}

interface DallasList {
  number: number;
  auth: number;
}

Template

<div *ngIf="vehicles">
    <p-dataTable [value]="vehicles.dallases" expandableRows="true">
        <p-header>List of vehicles: <b>{{currentUser}}</b></p-header>
        <p-column expander="true" styleClass="col-icon"></p-column>
        <p-column field="vehicle_id" header="Vehicle ID" [sortable]="true"></p-column>
        <p-column field="dallassettings" header="Dallas settings" [sortable]="true"></p-column>
        <p-column field="dallasupdated" header="Dallas updated" [sortable]="true"></p-column>
        <ng-template let-vehicle="value.dallas_list" pTemplate="rowexpansion"> <!-- the problem is here -->
            <div class="ui-grid ui-grid-responsive ui-fluid" style="font-size:16px;padding:20px">
                <div class="ui-grid-row">
                    <div class="ui-grid-col-9">
                        <div class="ui-grid ui-grid-responsive ui-grid-pad">
                            <div class="ui-grid-row">
                                <div class="ui-grid-col-2 label">Number: </div>
                                <div class="ui-grid-col-10">{{dallas_list.number}}</div>
                            </div>
                            <div class="ui-grid-row">
                                <div class="ui-grid-col-2 label">Auth: </div>
                                <div class="ui-grid-col-10">{{dallas_list.auth}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </ng-template>
    </p-dataTable>
</div>

I'm expecting to be able to display all numbers and auths for each of the rows inside the expendable row.

Upvotes: 2

Views: 1680

Answers (2)

Mario Petrovic
Mario Petrovic

Reputation: 8332

You are not using ng-template variable correctly:

 <div *ngIf="vehicles">
    <p-dataTable [value]="vehicles.dallases" expandableRows="true">
        <p-header>List of vehicles: <b>{{currentUser}}</b></p-header>
        <p-column expander="true" styleClass="col-icon"></p-column>
        <p-column field="vehicle_id" header="Vehicle ID" [sortable]="true"></p-column>
        <p-column field="dallassettings" header="Dallas settings" [sortable]="true"></p-column>
        <p-column field="dallasupdated" header="Dallas updated" [sortable]="true"></p-column>
        <ng-template let-vehicle pTemplate="rowexpansion"> <!-- the problem is here -->
            <div class="ui-grid ui-grid-responsive ui-fluid" style="font-size:16px;padding:20px">
                <div class="ui-grid-row">
                    <div class="ui-grid-col-9">
                        <div class="ui-grid ui-grid-responsive ui-grid-pad" *ngFor="let list of vehicle.dallas_list">
                            <div class="ui-grid-row">
                                <div class="ui-grid-col-2 label">Number: </div>
                                <div class="ui-grid-col-10">{{list.number}}</div>
                            </div>
                            <div class="ui-grid-row">
                                <div class="ui-grid-col-2 label">Auth: </div>
                                <div class="ui-grid-col-10">{{list.auth}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </ng-template>
    </p-dataTable>

Edit

The changes were made inside ng-template. let-vehicle is a reference to the current item in the iteration of the rows done by PrimeNG. Previously it was let-vehicle="value.dallas_list". This was wrong since that was reference to the whole list. You need only one as said before.

Than *ngFor was added to <div class="ui-grid ui-grid-responsive ui-grid-pad" *ngFor="let list of vehicle.dallas_list"> to iterate through current dallas_list and print list.number and list.auth inside html:

<div class="ui-grid ui-grid-responsive ui-grid-pad" *ngFor="let list of vehicle.dallas_list">
 <div class="ui-grid-row">
        <div class="ui-grid-col-2 label">Number: </div>
        <div class="ui-grid-col-10">{{list.number}}</div>
    </div>
    <div class="ui-grid-row">
        <div class="ui-grid-col-2 label">Auth: </div>
        <div class="ui-grid-col-10">{{list.auth}}</div>
    </div>
</div>

Upvotes: 2

micronyks
micronyks

Reputation: 55443

I'm not sure what exactly you want and I don't know anything about dataTable.

Try following things,

/* don't know if this is relevant or not */
<ng-template let-vehicle [ngForOf]="value.dallas_list"  pTemplate="rowexpansion">
  <li> {{vehicle.number}}</li>
  <li> {{vehicle.auth}}</li>
</ng-template>

OR

in your existing code access the element by its index number,

<div class="ui-grid-col-10">{{dallas_list[0].number}}</div>  //added [0]

Upvotes: 0

Related Questions