0

Good afternoon I have a query, I want to order a json that I get after consuming an api, to show it in a datatable, the question is that I do not want to order it in the backend, but just before passing it to the datatable, so that it is displayed directly in the datatable for the property that you specify.

This is my code with which I subscribe and get my data

My JSON

enter image description here

  RenderDataTable() {
        this.isLoading=true;  // El Spinner se muestra mientras se estan consumiendo los datos de la Api.
        this.PrtgService.getAllElements(this.table).subscribe(  
          (res) => {  
              this.dataSource = new MatTableDataSource();  
              this.dataSource.data = res;  // Asigando a lo que me estoy subscribiendo para asignarlo a un DataSource
              this.dataSource.paginator = this.paginator; // Paginando el DataSource
              this.isLoading=false;     // Si la carga de datos proveniente de la Api fue existosa, dejo de mostrar el Spinner
              // console.log(res);          // Imprimo para comporbar que todo Ok y estoy consumiendo lo deseado.  
              this.LengthEquipos=res.length;   // Obtengo la longitud del arrray que me traigo de la API.   
              // console.log(this.LengthEquipos);
        },    
        (error) => {     
          console.log('Se produjo un error mientras intentaba recuperar '+this.table + '!' + error);      
        });            
    }

And as my answer shows:

enter image description here

But in my case I want to get my data ordered by the Vendedor property

5
  • So... instead of assigning res to the data, assign res.sort() and pass a callback to it? The Array.sort() method can take a callback function, which has two parameters, which are two items from the array. If it returns a positive number, the first item gets moved after the second, if it returns a negative number, the first item gets moved BEFORE the second. Hint: the localeCompare method of strings returns just such a number for two strings based on their alphabetical order. Commented Oct 10, 2019 at 20:42
  • Would you do this with an arrow function? Commented Oct 10, 2019 at 20:45
  • You could, yes. Commented Oct 10, 2019 at 20:46
  • I already saw the method, but I don't understand very well how I would apply it in my case to assign to the datatable Commented Oct 10, 2019 at 20:49
  • I've posted an answer including some code. Commented Oct 10, 2019 at 20:53

1 Answer 1

2

To elaborate on my comment about sorting the array before assigning it to the table: Change this line:

this.dataSource.data = res; 

To this:

this.dataSource.data = res.sort((a, b) => a.vendedor.localeCompare(b.vendedor));
Sign up to request clarification or add additional context in comments.

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.