lundi 2 décembre 2019

How to make a select option as default selected in angular using ng-multiselect-dropdown?

So here I am getting data from the database and store it into events array. this events array data is shown on book-ticket.component.html using ng-multiselect-dropdown. From here on selecting one option this data corresponding to selected item stored in this.event.
I am passing id from the another component to this book-ticket.component and here we received it using

this.idByAllEvents = this.activatedRoute.snapshot.params.id;

HTML part of book-ticket Component is

 <div class="form-group">
            <label>Event</label>
            <ng-multiselect-dropdown [placeholder]="'Select the Event'" [data]="this.events" name="event"
                [(ngModel)]="this.event" [settings]="dropdownSettings1">
            </ng-multiselect-dropdown>
        </div>

.ts part of book-ticket component is

export class BookTicketComponent implements OnInit {
  user:User;
  event:Event;
  offer:Offer;
  // offer:string;
  // event:string;
  idByAllEvents: string; // This is coming from all events page

  price:number;
  percentageReduce:number; 
  offers:Offer[];
  events:Event[];
  dropdownSettings1: IDropdownSettings;
  dropdownSettings2: IDropdownSettings;
  selectedItem: any[];

  constructor(private Userservice:UserService,private Offerservice:OfferService,
    private Eventservice:EventService, private router:Router, 
    private activatedRoute:ActivatedRoute) { 
    this.idByAllEvents = this.activatedRoute.snapshot.params.id;
    console.log(this.idByAllEvents);
    this.user = new User();
    // this.offer = new Offer();
    // this.event = new Event();
    // this.offer = new Offer(); 
    // this.event = new Event();
    this.selectedItem = [];

  }


  ngOnInit() {

    this.dropdownSettings1 = {
      singleSelection: true,
      idField: '_id',
      textField: 'name',
      selectAllText: 'Select All',
      unSelectAllText: 'UnSelect All',
      itemsShowLimit: 3,
      allowSearchFilter: true,
      closeDropDownOnSelection: true,
    };



    this.Eventservice.GetEvents().subscribe(res => {
      this.events = res;
      this.justExecute();
      this.Offerservice.GetOffers().subscribe(res => {
      this.offers = res
      })
    });

 justExecute(){
    console.log(this.idByAllEvents);
    console.log(this.events);
    for (let index = 0; index < this.events.length; index++) {
      const element = this.events[index]._id;
      console.log(element);
      if(element == this.idByAllEvents){
        this.selectedItem.push(this.events[element]);
        // this.selectedItem = this.events[element]
      }
    }
    console.log(this.selectedItem);

  }




  }
SaveData(form:NgForm) {
    if(form.valid) {...}
}
}

I want that option corresponding to this.idByAllEvents is selected by default ON HTML page. But dropdown list shows all the options of this.events.

I tried using setting selectedItem by function justExecute but console.log(this.selectedItem) gives undefined array. If we get value corresponding to idByAllEvents in selectedItem then can we use [(ngModel)]="selectedItem" on html page to select that value.

How we should do this this. Is any another way to do it present .




Aucun commentaire:

Enregistrer un commentaire