ngx-datatable icon indicating copy to clipboard operation
ngx-datatable copied to clipboard

Column custom sort: pass row data to comparator

Open PeS82 opened this issue 7 years ago • 9 comments

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here, post on Stackoverflow or Gitter

Current behavior When TableColumn.comparator is used, arguments passed to the comparator are values of that column.

Expected behavior It would be nice to have an option to receive row data to comparator.

Due to backward compatibility it would be nice to add another Input to TableColumn, e.g. rowComparator(row, row), not to break legacy code.

What is the motivation / use case for changing the behavior?

My specific case is I am displaying time (duration) in a column (e.g. "1d 20h 14m", "5h 20m"). In other column there is the same value, as number in seconds. Obviously, alphanumeric sorting is wrong in this case and I need to sort the column by seconds.

Since comparator feeds me with string value I must parse the duration back to seconds despite the fact that the value is already present in the table model.

I believe this would be handy in other cases when sortable value is already present however the displayed value is somehow "humanized".

  • Table version: 9.3.0

  • Angular version:

4.1.3 but that is not relevant I believe

PeS82 avatar Jun 14 '17 16:06 PeS82

I have the exactly the same issue, any solutions?

olaf89 avatar Aug 08 '17 11:08 olaf89

same here

commitBlob avatar Aug 08 '17 14:08 commitBlob

Latest version has this feature, both rows are passed as the 3rd and 4th parameters to the comparator function, although I don't think it's been pushed to NPM yet.

Ecksters avatar Nov 03 '17 22:11 Ecksters

This should have been added in ngx-datatable 10.4.0. I've tried it and it works.

chrisbmoore avatar Nov 06 '17 21:11 chrisbmoore

@chrisbmoore any demo script.. ?

ArfanMirza avatar Apr 24 '18 10:04 ArfanMirza

The API has it here: https://swimlane.gitbooks.io/ngx-datatable/api/column/inputs.html#comparator

Custom sort comparator, used to apply custom sorting via client-side. Function receives five parameters, namely values and rows of items to be sorted as well as direction of the sort ('asc'|'desc'): (valueA, valueB, rowA, rowB, sortDirection) => -1|0|1

Using the demo, it looks like:

companyComparator(propA, propB, rowA, rowB) {
    console.log('Sorting Comparator', propA, propB, rowA, rowB);
    //do something with rowA and rowB objects.
    if (rowA.age < rowB.age) return -1;
    if (rowA.age > rowB.age) return 1;
  }

chrisbmoore avatar Apr 24 '18 14:04 chrisbmoore

For anyone else confused as to how to use a custom comparator in a template, you can pass the row data to the comparator by putting prop="" in the template, like this:

<ngx-datatable-column name="Reviewed" prop="" [comparator]="sortReviewedComparator">
	<ng-template let-row="row" ngx-datatable-cell-template>
		<span {{(row.numReviewed/row.numEntries)*100||0}}% Reviewed">{{row.numReviewed}}/{{row.numEntries}}</span>
	</ng-template>
</ngx-datatable-column>

Ryan-Haines avatar Apr 25 '18 16:04 Ryan-Haines

@Ryan-Haines, thanks this worked perfectly. Any way to use multiple comparators across separate columns? I've used it but it always binds to the last comparator. Been stuck at it for some time now...

arpansac avatar Apr 14 '20 19:04 arpansac

propA, propB are always empty here. What is the point of having these arguments in comparator, if this function is triggered only if prop="", i.e. for custom templates? Instead, I would like to pass custom arg into this function, but have no idea how.

@Ryan-Haines, thanks this worked perfectly. Any way to use multiple comparators across separate columns? I've used it but it always binds to the last comparator. Been stuck at it for some time now...

Indeed, other option is to have one comparator with custom parameters... But I don't know how to use such here.

alexandis avatar Jul 28 '22 17:07 alexandis