Я импортировал данные о студентах с помощью import {allStudentList} from '../student-data';
в файле student-details.component.ts.
в файле student-data.ts Я сохранил все данные, например:
export const allStudentList = [
{
"ID": "1",
"Name": "Senpai",
"Gender": "1",
"Class": "32",
"Seat": "15",
"Club": "0",
"Persona": "1",
"Crush": "0",
"BreastSize": "0",
"Strength": "0",
"Hairstyle": "1",
"Color": "Black",
"Eyes": "Black",
"EyeType": "Default",
"Stockings": "None",
"Accessory": "0",
"ScheduleTime": "7_7_8_13.01_13.375_15.5_16_17.25_99_99",
"ScheduleDestination": "Spawn_Locker_Hangout_Seat_LunchSpot_Seat_Clean_Hangout_Locker_Exit",
"ScheduleAction": "Stand_Stand_Read_Sit_Eat_Sit_Clean_Read_Shoes_Stand",
"Info": "An average student. \n \n Average grades, average looks, average life... \n \n I'm not sure what you see in him."
},
{
"ID": "2",
"Name": "Yui Rio",
"Gender": "0",
"Class": "11",
"Seat": "14",
"Club": "1",
"Persona": "5",
"Crush": "0",
"BreastSize": "0.5",
"Strength": "0",
"Hairstyle": "2",
"Color": "Red",
"Eyes": "Red",
"EyeType": "Default",
"Stockings": "Red",
"Accessory": "0",
"ScheduleTime": "7_7_8_13_13.375_15.5_16_17.25_99_99",
"ScheduleDestination": "Spawn_Locker_Hangout_Seat_LunchSpot_Seat_Clean_Club_Locker_Exit",
"ScheduleAction": "Stand_Stand_Socialize_Sit_Socialize_Sit_Clean_SocialSit_Shoes_Stand",
"Info": ""
},
{
"ID": "3",
"Name": "Yuna Hina",
"Gender": "0",
"Class": "12",
"Seat": "14",
"Club": "1",
"Persona": "6",
"Crush": "0",
"BreastSize": "0.8",
"Strength": "0",
"Hairstyle": "3",
"Color": "Yellow",
"Eyes": "Yellow",
"EyeType": "Default",
"Stockings": "Yellow",
"Accessory": "0",
"ScheduleTime": "7_7_8_13_13.375_15.5_16_17.25_99_99",
"ScheduleDestination": "Spawn_Locker_Hangout_Seat_LunchSpot_Seat_Clean_Club_Locker_Exit",
"ScheduleAction": "Stand_Stand_Socialize_Sit_Socialize_Sit_Clean_SocialSit_Shoes_Stand",
"Info": ""
},
{
"ID": "100",
"Name": "Info-chan",
"Gender": "0",
"Class": "0",
"Seat": "0",
"Club": "99",
"Persona": "99",
"Crush": "0",
"BreastSize": "1",
"Strength": "99",
"Hairstyle": "1",
"Color": "White",
"Eyes": "Black",
"EyeType": "Default",
"Stockings": "None",
"Accessory": "0",
"ScheduleTime": "0",
"ScheduleDestination": "Nothing",
"ScheduleAction": "Nothing",
"Info": "Trying to look up my information? Don't bother. There is nothing that you need to know about me. You're a client, and I'm a provider. That's all we need to know about each other."
}
]
теперь я хочу изменить данные об одном ученике, это мой файл student-details.component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {allStudentList} from '../student-data';
@Component({
selector: 'app-student-details',
templateUrl: './student-details.component.html',
styleUrls: ['./student-details.component.css']
})
export class StudentDetailsComponent implements OnInit {
studentDetails:any = [];
allStudentList = allStudentList;
studentUpdateDetails:any = [];
id: number;
name:any;
gender : string;
class : string;
seat: string;
club: string;
persona : string;
crush : string;
breastSize : number;
strength: string;
hairstyle: string;
color: string;
eyes: string;
eyeType: string;
stockings: string;
accessory: string ;
scheduleTime : string;
scheduleDestination: string ;
scheduleAction: string;
info: string;
constructor( private route: ActivatedRoute,) { }
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.studentDetails = Array.of(this.allStudentList[+params.get('id')]);
console.log(this.studentDetails);
this.id = this.studentDetails[0].ID;
this.name = this.studentDetails[0].Name;
this.gender = this.studentDetails[0].Gender;
this.class = this.studentDetails[0].Class;
this.seat = this.studentDetails[0].Seat;
this.club = this.studentDetails[0].Club;
this.persona = this.studentDetails[0].Persona;
this.crush = this.studentDetails[0].Crush;
this.breastSize = this.studentDetails[0].BreastSize;
this.strength = this.studentDetails[0].Strength;
this.hairstyle = this.studentDetails[0].Hairstyle;
this.color = this.studentDetails[0].Color;
this.eyes = this.studentDetails[0].Eyes;
this.eyeType = this.studentDetails[0].EyeType;
this.stockings = this.studentDetails[0].Stockings;
this.accessory = this.studentDetails[0].Accessory;
this.scheduleTime = this.studentDetails[0].ScheduleTime;
this.scheduleDestination = this.studentDetails[0].ScheduleDestination;
this.scheduleAction = this.studentDetails[0].ScheduleAction;
this.info = this.studentDetails[0].Info;
});
}
clickedBtn(){
if(window.confirm('Are you sure you wanna update?')){
// alert("function called" + this.gender);
this.studentUpdateDetails = [
{"ID" : this.id},
{"Name" : this.name},
{"Gander" : this.gender},
{"Class" : this.class},
{"Seat" : this.seat},
{"Club" : this.club},
{"Persona" : this.persona},
{"Crush" : this.crush},
{"BreastSize" : this.breastSize},
{"Strength" : this.strength},
{"Hairstyle" : this.hairstyle},
{"Color" : this.color},
{"Eyes" : this.eyes},
{"EyeType" : this.eyeType},
{"Stockings" : this.stockings},
{"accessory" : this.accessory},
{"ScheduleTime" : this.eyes},
{"ScheduleDestination" : this.scheduleDestination},
{"ScheduleAction" : this.scheduleAction},
{"Info" : this.info},
]
//console.log('length=>' + this.studentUpdateDetails.length);
//this.studentDetails = this.studentUpdateDetails;
for (let i = 0; i < this.allStudentList.length; i++) {
if(this.allStudentList[i].ID === this.studentUpdateDetails[0].ID){
//this.users[i] = newItem;
alert("=>" + JSON.stringify(this.allStudentList[i]) + "=>" + JSON.stringify(this.studentUpdateDetails) );
this.allStudentList[i] = this.studentUpdateDetails;
//alert( "==" +this.studentUpdateDetails[i].ID + 'write here' + this.studentUpdateDetails[0].ID);
}
}
}
}
}