3

I am working on a new project on Angular 6.

I have a list of students with a *NgFor :

(html)

<tr *ngFor="let kk of Repdata | filterdata: queryString : 'name' ; let ind = index">

    <td>{{ind + 1}}</td>
    <td>{{kk.name}}</td>
    <td>{{kk.prenom}}</td>

(filterdata is use for a seachbar)

typescript :

GetUser(){
   return this.http.get('http://localhost:8080/api/getUser/')
           .map((response: Response) => response.json())
}

javascript :

app.get("/api/getUser",function(req,res){
    model.find({},function(err,data){
              if(err){
                  res.send(err);
              }
              else{
                  res.send(data);
                  }
          });
  })

In my table, I have my data ordered by id, but I want to order it by name. I don't know how to.

3

2 Answers 2

2

if you just want your data sorted by name outside of the Database you can use the sort() function as explained here Sort array by firstname (alphabetically) in Javascript

you could also use the Sort function provided by mongodb as it seems you re using mongoose. https://docs.mongodb.com/manual/reference/method/cursor.sort/

if u want to just sort it in the Angular view you can use a custome sort pipe as explained here: Angular 2 OrderBy Pipe

edit:

You shouldnt use Pipes for Filtering / Sorting as they are very unperformant as stated here: https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe

Sign up to request clarification or add additional context in comments.

1 Comment

0

You can perform sorting by installing DataTable in your app. It can also used to perform pagination.

You can install DataTable by running following command in angular 6:

npm i angular-6-datatable --save

For complete guide, go through this below link:

https://www.npmjs.com/package/angular-6-datatable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.