2

Below is my custom table component's sorting. It is working good for String and number data types except the multiple decimal point number. DTableSort.ts

@Component({
    selector: "myDefaultGSorter",
    template: `
        <a style="cursor: pointer" (click)="sort()" class="text-nowrap" 
        [ngClass]="isSortedByMeAsc ? 'sort_by_asc' : isSortedByMeDesc ? 'sort_by_desc' : 'no_sorting'">
        <ng-content></ng-content>
        </a>`
})

DTable.ts

import { Directive , EventEmitter , Input , Output , OnInit , OnChanges, DoCheck, IterableDiffers, IterableDiffer } from '@angular/core';
import { ReplaySubject } from "rxjs";
import * as _ from "lodash";

export interface SortEvent {
  sortBy: string|string[];
  sortOrder: string
}

@Directive({
  selector: '<table [mxData]',
  exportAs: `mxDataTable`
})

export class DTableG implements OnChanges , OnInit, DoCheck  {

  private diff: IterableDiffer<any>;
  public onPageChange = new EventEmitter<PageEvent>();

  @Output() public onSortChange = new ReplaySubject<SortEvent>(1);
  @Input("mfSortBy") public sortBy: string|string[] = "";
  @Input("mfSortOrder") public sortOrder = "asc";
  @Input("mxData") public mxData : any[] = [];

  ngOnChanges() {        
    data = _.orderBy(data, this.sortBy, [this.sortOrder]);
  }
  /** sort */

  public setSort(sortBy: string|string[], sortOrder: string): void {
    if (this.sortBy !== sortBy || this.sortOrder !== sortOrder) {
        this.sortBy = sortBy;
        this.sortOrder = _.includes(["asc","desc"], sortOrder) ? sortOrder : "asc";
        this.onSortChange.next({sortBy: sortBy, sortOrder: sortOrder});  
        this.onSortChange.next({sortBy: sortBy, sortOrder: sortOrder});
     }
  }
}

And using the custom table and calling sort event with myDefaultGSorter as below

<table class="dTable" [mxData]="mgmtList" style="width: 98%;margin: 0px auto;"
       #applListMgmtTable="mxDataTable" >
  <thead>

  <tr>
    <th style="width: 13%">
      <myDefaultGSorter by="requirementId">
        Requirement ID
      </myDefaultGSorter>
    </th>
    <th style="width: 32%">
      <myDefaultGSorter by="applicableRequirement">
        Applicable Requirement
      </myDefaultGSorter>
    </th>

    <th style="width: 15%">
      <myDefaultGSorter by="requirementSource">
        Source
      </myDefaultGSorter>
    </th>
    <th style="width: 8%">Action</th>
  </tr>
  </thead>
  <tbody>
  <tr *ngFor = "let dataL of applListMgmtTable.data">
    <td>
      {{dataL.requirementId}}
    </td>
    <td>
      {{dataL.applicableRequirement}}
    </td>
    <td>
      {{dataL.requirementSource}}
    </td>
    <td>
    </td>
  </tr>
  </tbody>
</table>

Requirement ids are getting sorted as

4.1.13
4.1.19
4.1.25
4.1.7
4.1.8
4.3.2
4.3.3
4.4.2

What I am expecting is as

4.1.7
4.1.8
4.1.13
4.1.19
4.1.25
4.3.2
4.3.3
4.4.2
6
  • 1
    can you add your sort function here which your are using to sort the data Commented Dec 13, 2022 at 5:10
  • @jitender added sort function in the question. It is in ngOnChanges() Commented Dec 13, 2022 at 5:49
  • may be this link can help you? stackoverflow.com/a/20080521/5621827 Commented Dec 13, 2022 at 7:40
  • @jitender Yes i saw this link before posting this question. But i could not find how to use in my existing sort Commented Dec 13, 2022 at 7:49
  • 1
    It looks like _.orderBy accepts a string or a sort function for its second parameter. Try passing in a function that sorts the way you require. Commented Dec 13, 2022 at 10:29

0

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.