0

I would like to have sorting on multiple tables which are in same page. I tried to follow this package. https://github.com/VadimDez/ngx-order-pipe

When I'm sorting the table 1, table 2 is also getting sorted.How do I implement this on multiple tables separately. ?

order: any;
reverse: boolean = false;
  setOrder(value: string) {
    if (this.order === value) {
      console.log(this.order)
      this.reverse = !this.reverse;
    }
    this.order = value;
  }


 <table>
             <thead>
                  <tr>
                    <th (click)="setOrder('Data.name')">Name</th>
                    <th (click)="setOrder('Data.age')">Age</th>
                  </tr>
                </thead>
                <tbody>
                  <tr *ngFor="let Data of collection1 | orderBy: order:reverse:'case-insensitive'">
                    <td class="text-truncate">{{Data.name}}</td>
                    <td class="text-truncate">{{Data.age}}</td>
                  </tr>
                </tbody>
              </table>

  <table>
            <thead>
              <tr>
                <th (click)="setOrder('PersonData.id')">Id</th>
                <th (click)="setOrder('PersonData.sport')">Sport</th>
              </tr>
            </thead>
            <tbody>
              <tr *ngFor="let PersonData of collection2 | orderBy: order:reverse:'case-insensitive'">
                <td class="text-truncate">{{PersonData.id}}</td>
                <td class="text-truncate">{{PersonData.sport}}</td>
              </tr>
            </tbody>
          </table>
4
  • It's hard with this much information (you may want to provide more), but it looks like both tables could be using the same this.reverse property. If you create separate reverse flags does it work? Commented May 4, 2018 at 17:50
  • @ScottSword this is the only code I'm using for sorting. please tell me particular part of code you are looking for Commented May 4, 2018 at 17:59
  • all the code for this component would help. Also, did you see my reverse comment? If both tables are referencing the same reverse property and you toggle it, its going to reverse both. Commented May 4, 2018 at 19:12
  • @ScottSword yes that makes sense. It is working now. Thanks Commented May 7, 2018 at 14:06

2 Answers 2

1

I'm not familiar with the package you are using, but the best way to achieve your goal really depends on your use case. One way is to refactor your code so that each table is its own component, and handles its own sorting. That way sorting data isn't shared between the tables.

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

Comments

0

Whatever the code you share, it have only one table.

You don't need to use any third party library but create your own sort filter like this

sort(property){
    this.isDesc = !this.isDesc; //change the direction    
    this.column = property;
    let direction = this.isDesc ? 1 : -1;

    this.records.sort(function(a, b){
        if(a[property] < b[property]){
            return -1 * direction;
        }
        else if( a[property] > b[property]){
            return 1 * direction;
        }
        else{
            return 0;
        }
    });
};

See the full post in detail

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.