load drop down values alphabetic order angular 6

a 夏天 提交于 2020-08-26 09:27:14

问题


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>

回答1:


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>



回答2:


Use orderBy to sort the select box

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



回答3:


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>


来源:https://stackoverflow.com/questions/55842594/load-drop-down-values-alphabetic-order-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!