In Table user can select any number of rows to upload on "upload API" but in those selected Rows any one of the row contains status as "NOT OK" I need to show popup that there is "not ok" status please "deselect" that row, if the rows contains status OK then I have to call "upload API".
onUpload(){
let selectedRows=this.agGrid.api.getSelectedRows();
selectedRows.forEach((row:any,i:any)=>{
if( row.status==='OK'){
/** upload API CALL **/
}else{
this.isTooltipUpload=true;
setTimeout(()=>{
this.isTooltipUpload=false;
},2000)
}
})
}
selectedRows :[
{athlete: 'Michael Phelps', age: 23, status:'OK', year: 2008, date: '24/08/2008', …}
{athlete: 'Michael Phelps', age: 19, status:'OK', year: 2004, date: '29/08/2004', …}
{athlete: 'Michael Phelps', age: 27, status:'Not ok', year: 2012, date: '12/08/2012', …}
]
here I am looping the selectedRows using forEach method and comparing the row.status===’OK’, if it is true I need to Call API else need to show popup(this.isTooltipUpload=true).
what I am getting:
in selectedRows even status is ‘NOT OK’ "if"(if condition false) condition is calling the API and else calling the popup
can any one please help me where I’m doing wrong.
>Solution :
Currently, your code considers each row separately, checking if each individual one is valid. You directly make an API call when the current row is valid, without having ensured that every single row is valid beforehand.
You can use Array#some to check if any row is not valid before attempting API calls.
if (selectedRows.some(r => r.status !== 'OK')) {
// show error popup
} else {
// loop over selectedRows and make each API call
}