Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

django - How to reload page while keeping FormGroup content alive

I have got a filter function in a sidenav in my application which filters the list of profiles. However when I submit the filter function in the HTML I call this onFilterButtonSubmit function (see below) which should give my profile-list.component.ts the parameters while also reloading the page.

onFilterButtonSubmit(minA, maxA, gen): void {
this.profileService.minAge = minA;
this.profileService.maxAge = maxA;
this.profileService.gender = gen;
this.router.navigate(['/home']);
}

The site is not being reloaded though. Only when I am on another page (f.e. profile-page), parsing the filter criteria and clicking on Filter, it correctly reroutes me to /home and displays the results I filtered for. I also tried using window.location.reload(); which reloads the page successfully but does not save the FormField content. I am kinda lost here, any help is appreciated. Not sure which other code parts are relevant but let me know and I will add it to the question.

I am using Angular and Django for the backend.

app.component.ts

export class AppComponent implements OnInit {
title = 'frontend';
isLoggedIn = false;
filteredFormControl: any = new FormControl('');
filteredProfilesFormGroup: FormGroup;
profiles: Profile[];
public isFiltered: boolean;

constructor(public userService: UserService, public profileService: ProfileService, private router: 
Router) {
}

ngOnInit(): void {
  this.filteredProfilesFormGroup = new FormGroup({
    minAge: new FormControl(''),
    maxAge: new FormControl(''),
    gender: new FormControl('')
  });

  this.userService.isLoggedIn.subscribe((isLoggedIn) => {
    this.isLoggedIn = isLoggedIn;
  });
}

onFilterButtonSubmit(minA, maxA, gen): void {
  this.profileService.minAge = minA;
  this.profileService.maxAge = maxA;
  this.profileService.gender = gen;
  this.router.navigate(['/home']);
}

profile-list.component.ts


  profiles: Profile[];
  filteredProfiles: Profile[];
  filteredFormControl = this.appComponent.filteredFormControl;
  minAge = '';
  maxAge = '';
  gender = '';

  constructor(private profileService: ProfileService,
              public userService: UserService,
              public dialog: MatDialog,
              public appComponent: AppComponent,
              private router: Router) {
  }

  ngOnInit(): void {
    this.minAge = this.profileService.minAge;
    this.maxAge = this.profileService.maxAge;
    this.gender = this.profileService.gender;
    this.filterCustom(this.minAge, this.maxAge, this.gender);

    this.filteredFormControl.valueChanges
      .subscribe((newValue: string) => {
        this.filter(newValue);
      });
  }

  filterCustom(gender: string, minAge: string, maxAge: string): void {
    this.profileService.testFilter(gender, minAge, maxAge)
      .subscribe((profiles: Profile[]) => {
        this.profiles = profiles;
        this.router.navigate(['/home']);
      });
  }
question from:https://stackoverflow.com/questions/66051116/how-to-reload-page-while-keeping-formgroup-content-alive

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...