简体   繁体   中英

load drop down values alphabetic order angular 6

I am trying to load drop down with alphabetic order. currently i used unique value pipe for the drop down. how i add another pipe for get alphabetic order..

<select class="form-control fix-dropdown" required  
    (change)="batchSorceList();"
    [ngClass]="{'is-invalid':glheaderform.submitted && orgname.invalid}"
    #orgname="ngModel" [(ngModel)]="orgNameModel.orgName"
    name="orgName"> 
        <option value="undefined" disabled="true">--Select--</option>
        <option *ngFor="let bank of orgNameModel | unique ">{{bank.orgName}}</option> 
</select>

As a best practice in Angular, you should not use OrderPipe

if you want to sort items based on names, use the pure javascript sort function as follows

sortBy(prop: string) {
  return this.orgNameModel.sort((a, b) => a[prop] > b[prop] ? 1 : a[prop] === b[prop] ? 0 : -1);
}

and in your HTML,

<option *ngFor="let bank of of filterBy('orgName')"| unique ">{{bank.orgName}}</option>

使用orderBy对选择框进行排序

<option *ngFor="let bank of orgNameModel | unique | orderBy : 'your column name' ">{{bank.orgName}}</option>

Implement a pipe like this with a sort function (See live here: https://stackblitz.com/edit/angular-d17atb )-

@Pipe({name: 'sortBy'})
export class SortByPipe implements PipeTransform {
  transform(value: any[]) : any[] {
    value.sort(function(a,b){
    let alc = a.toLowerCase(),
    blc = b.toLowerCase();
    return alc > blc ? 1 : alc < blc ? -1 : 0;
 });
    console.log(value);
    return value;
  }
}

And the html will be like -

 <li *ngFor="let value of list | sortBy">{{value}}</li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM