1

Component:

import { Component, OnInit, Input } from '@angular/core';
import * as _ from "lodash";

import { AF } from '../angularfire.service';

@Component({
  selector: 'app-record-chart',
  templateUrl: './record-chart.component.html',
  styleUrls: ['./record-chart.component.less']
})
export class RecordChartComponent implements OnInit {
  chartFilter = "Personal Records";
  // Fill Arrays
  currentUser = [];
  userRecords = [];
  topRecords = [];
  topRecordLabels = [];
  topRecordWeights = [];
  lineRecords = [];
  public lineRecordWeights:Array<number[]> = [];
  public lineRecordLabels:Array<any> = [];
  movements = [
    "Back Squat",
    "Bench Press",
    "Clean",
    "Clean & Jerk",
    "Deadlift",
    "Front Squat",
    "Jerk",
    "Power Clean",
    "Power Snatch",
    "Push Press",
    "Snatch",
    "Strict Press"
  ];
  movementCharts = [
    "Personal Records",
    "Back Squat",
    "Bench Press",
    "Clean",
    "Clean & Jerk",
    "Deadlift",
    "Front Squat",
    "Jerk",
    "Power Clean",
    "Power Snatch",
    "Push Press",
    "Snatch",
    "Strict Press"
  ];

  constructor(private afService: AF) {
    // Get current user details.
    afService.getCurrentUserInfo().then(currentUserDetails => {
      this.currentUser.push(currentUserDetails);
    }).then(() => {
      // Populate lifts array
      for(let movement of this.movements) {
        this.afService.getRecords(movement, this.currentUser[0].userID).subscribe((data) => {
          // Sort Arrays
          var sortedArray = _.orderBy(data, ['weight']);
          var sortedArray2 = _.orderBy(sortedArray,'date');
          this.userRecords.push(sortedArray);

          // Filter by PR
          var newRecords = sortedArray
          .filter(function(record) {
              return sortedArray.find(function(innerRecord) {
                  return innerRecord.name === record.name && innerRecord.weight > record.weight; }) === undefined;
           });
           var newRecords2 = sortedArray2
           .filter(function(record) {
               return sortedArray2.find(function(record) {
                   return record.movement === "Back Squat"; });
            });

           // Build array of PR's
           for (let record of newRecords) {
             this.topRecords.push(record);
           }

           // Build array of PR's
           for (let record of newRecords2) {
             this.lineRecords.push(record);
           }
        });
      }
    }).then(() => {
      // Push labels and weights to chart.
      setTimeout(() => {
      for (let item of this.topRecords) {
        this.topRecordLabels.push(item.movement);
        this.topRecordWeights.push(item.weight);
      }
      this.topRecordLabels = this.topRecords.map((item)=> item.movement);
      this.topRecordWeights = this.topRecords.map((item)=> item.weight);

      for (let item of this.lineRecords) {
        this.lineRecordLabels.push(item.date);
        this.lineRecordWeights.push(item.weight);
      }
      this.lineRecordWeights = this.lineRecords.map((item)=> item.weight);
    }, 300)
  })}

  ngOnInit() {
  }

}

Component section focused on:

var newRecords2 = sortedArray2
           .filter(function(record) {
               return sortedArray2.find(function(record) {
                   return record.movement === "Back Squat"; });
            });

HTML:

<div class="content-actions btn-group">
    <select class="form-select record-select" [(ngModel)]="chartFilter">
      <option *ngFor="let movement of movementCharts">{{ movement }}</option>
    </select>
    <button class="btn btn-primary" type="button" routerLink="/add-record">Add Record</button>
  </div>

I need to replace the "Back Squat" string in the component with the ngModel chartFilter, but I'm not sure how to go about passing a dynamic changing value into a for loop. The value changes whenever a new item is selected via the <select> menu.

2
  • replace the "Back Squat" with ? Commented Jun 17, 2017 at 23:01
  • this.chartFilter Commented Jun 17, 2017 at 23:03

2 Answers 2

3

Updated

You should be using [ngValue] as below,

 <select [(ngModel)]="chartFilter">
        <option *ngFor="let type of movementCharts" [ngValue]="type"> {{type}}</option>
      </select>

Update 1 : Based on chat

If you want to handle the event explicitly when the dropdown is changed you should use ngModelChange() event as below,

<select [ngModel]="chartFilter" (ngModelChange)="eventHanler($event)">
        <option *ngFor="let type of movementCharts" [ngValue]="type"> {{type}}</option>
      </select>

Typescript Code:

eventHanler(movement){
   //// do what ever you want

}

Note : Updated the demo accordingly

LIVE DEMO

Sign up to request clarification or add additional context in comments.

7 Comments

What? This doesn't make any sense. Arrays are built dynamically from this line: return record.movement === "Back Squat"; . hence why I need "Back Squat" to be set to the ngModel. The value has 13 different possibilities.
you want to replace the value "Back Squat" with this.chartFilter correct. or you want the "PersonalSquats" to be prefilled in it?
Yeah, "Back Squat" inside the for loop, not in the preset array.
var newRecords2 = sortedArray2 .filter(function(record) { return sortedArray2.find(function(record) { return record.movement === "Back Squat"; }); });
you are confusing me. elaborate your question. clearly. I can see that movementCharts value is defined in the class. so what is exact issue?
|
0

I find it a bit hard to understand. Basically you want to run this for loop whenever the value in the select changes but then instead of putting that code in the constructor section, why don't you use ngModelChange and assign a function to it just like @aravind mentions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.