Hi
How to delete particulat student id from Local Storage Data.
ID
Name
MobileNo
Action
@for (item of studentList;track $index){
{{item.id}}
{{item.name}}
{{item.mobileno}}
}
export class AppComponent implements OnInit{
title = 'AngularFrm';
isEditMode: boolean = false;
isSubmitMode: boolean = true;
@ViewChild('myModal') model : ElementRef | undefined;
studentObj: Student = new Student();
studentList:Student[] = [];
ngOnInit(): void {
const localData = localStorage.getItem("angular17Crud");
if(localData != null){
this.studentList=JSON.parse(localData)
}
}
-------------------------------------------------
export class Student {
id:number;
name:string;
mobileno:string;
email:string;
city:string;
state:string;
pincode:string;
address:string;
constructor(){
this.id=0;
this.address ='';
this.name='';
this.mobileno='';
this.email='';
this.city='';
this.state='';
this.pincode='';
}
}
Thanks
Naimish MakwanaPosted May 9, 2024, 8:38 AM
To delete a particular student ID from the Local Storage data, you can add a
onDeletemethod in yourAppComponentclass. This method will filter out the student with the given ID from thestudentListarray and then update the Local Storage data. Here’s how you can do it:In the above code,
onDeletemethod takes a student ID as a parameter. It filters out the student with the given ID from thestudentListarray. Then, it updates the Local Storage data with the newstudentListarray.Thanks
Jobin SPosted May 9, 2024, 8:37 AM
Hi Ramco
1.if you delete exact row in I'd to pass local storage list to find index and slice method to remove exact data
Deletefunction(id){
Var index= list.findindex(x=>x.id==id);
Localstoagelist.splice(index,1);
Then set localstorage
}