Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
Sort table rows without losing reactivity

A web app (Angular SPA) displays a large table but it does not allow sorting so I'm trying to do it via the console. I've got the sorting part but re-inserting the rows causes it to lose reactivity (listeners) which is pointless.

function sortRows() {
  const table = document.querySelector('#mytable');
  const tbody = table.querySelector('tbody');
  const rows = tbody.querySelectorAll('tr');
  const rowsSortedBySize = Array.from(rows).sort((a, b) => {
    return Number(b.children[3].textContent) - Number(a.children[3].textContent);
  });
  // remove rows
  tbody.innerHTML = "";
  // add sorted rows
  rowsSortedBySize.forEach(row => tbody.appendChild(row));
}
<table id="mytable">
  <tbody>
    <tr>
      <td><input type="checkbox" name="one"></td>
      <td>name</td>
      <td>description</td>
      <td>1</td>
      <td>date</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="two"></td>
      <td>name</td>
      <td>description</td>
      <td>2</td>
      <td>date</td>
    </tr>
  </tbody>
</table>

<button onclick="sortRows()">Sort Rows</button>

How can I sort without re-inserting to avoid losing reactivity?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not using Angular in the correct way.

I will show you a different solution for your problem, but you need to make a lot of changes.

First, create an array in your .ts file. It has to be an array of objects, containing this data:

<td><input type="checkbox" name="one"></td>
<td>name</td>
<td>description</td>
<td>1</td>
<td>date</td>

So.

public myData = [
{
  name: 'name 1', // name field
  description: 'description 1', // description field
  itemNumber: 1,
  date: new Date(),
  selected: true
},
{
  name: 'name 2',
  description: 'description 2',
  itemNumber: 2,
  date: new Date(),
  selected: false
}]

Then, on your .html file

<table id="mytable">
  <tbody>
    <tr *ngFor="let item of myData; let i = index">
      <td>
         <input type="checkbox" [checked]="item.selected" (change)="item.selected = !item.selected">
      </td>
      <td>{{ item.name }}</td>
      <td> {{ item.description }} </td>
      <td> {{ item.itemNumber }} </td>
      <td> {{ item.date }} </td>
    </tr>
  </tbody>
</table>

<button (click)="sortRows()">Sort Rows</button>

So now your method would be something like this:

public sortRows(): void {
  this.myData = this.myData.sort((a, b) =>  b.itemNumber - a.itemNumber);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!