lundi 18 janvier 2021

Keep checked checkboxes after refresh

I have an array of users and one checkbox per user, if I check it, the user gets a new status. I can't keep them checked if I refresh the browser tab. I can't use radio button in this case.

HTML

       <tr scope="row" *ngFor="let user of users">
        <td class="w-25">
          <input
            class="check-btn"
            type="checkbox"
            name="premium"
            [checked]="checked"
            [ngModel]="checked"
            (click)="
              eventChecked($event.target.checked ? '1' : '0', user.id)
            "
          />
        </td>
        <td class="w-50"></td>
      </tr>

TS

  users: IUser[];
  checked: any;

  constructor(
    private userService: UserService,

  ) {}

  ngOnInit(): void {
    this.getUsers();
  }

  getUsers() {
    this.userService.getUsers().subscribe(
      (data) => {
        this.users = data;
        console.log(data);
        });      
      },
      (error) => {
        console.log(error);
      }
    );
  }
  
  eventChecked(event: any, id: number) {
    console.log(id, event);
    this.checked = true;
    this.userService.editUser(event, id).subscribe();
  }



Aucun commentaire:

Enregistrer un commentaire