Skip to content

Commit b469c49

Browse files
author
Çağatay Çivici
committed
1 parent 2b5b44e commit b469c49

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

components/common/shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class TemplateWrapper implements OnInit {
6565
})
6666
export class Column implements AfterContentInit{
6767
@Input() field: string;
68+
@Input() sortField: string;
6869
@Input() header: string;
6970
@Input() footer: string;
7071
@Input() sortable: any;

components/datatable/datatable.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,9 @@ export class DataTable implements AfterViewChecked,AfterViewInit,AfterContentIni
817817

818818
let targetNode = event.target.nodeName;
819819
if(targetNode == 'TH' || (targetNode == 'SPAN' && !this.domHandler.hasClass(event.target, 'ui-c'))) {
820-
this.sortOrder = (this.sortField === column.field) ? this.sortOrder * -1 : 1;
821-
this.sortField = column.field;
820+
let columnSortField = column.sortField||column.field;
821+
this.sortOrder = (this.sortField === columnSortField) ? this.sortOrder * -1 : 1;
822+
this.sortField = columnSortField;
822823
this.sortColumn = column;
823824
let metaKey = event.metaKey||event.ctrlKey;
824825

@@ -944,14 +945,16 @@ export class DataTable implements AfterViewChecked,AfterViewInit,AfterContentIni
944945
return false;
945946
}
946947

948+
let columnSortField = column.sortField||column.field;
949+
947950
if(this.sortMode === 'single') {
948-
return (this.sortField && column.field === this.sortField);
951+
return (this.sortField && columnSortField === this.sortField);
949952
}
950953
else if(this.sortMode === 'multiple') {
951954
let sorted = false;
952955
if(this.multiSortMeta) {
953956
for(let i = 0; i < this.multiSortMeta.length; i++) {
954-
if(this.multiSortMeta[i].field == column.field) {
957+
if(this.multiSortMeta[i].field == columnSortField) {
955958
sorted = true;
956959
break;
957960
}
@@ -963,15 +966,17 @@ export class DataTable implements AfterViewChecked,AfterViewInit,AfterContentIni
963966

964967
getSortOrder(column: Column) {
965968
let order = 0;
969+
let columnSortField = column.sortField||column.field;
970+
966971
if(this.sortMode === 'single') {
967-
if(this.sortField && column.field === this.sortField) {
972+
if(this.sortField && columnSortField === this.sortField) {
968973
order = this.sortOrder;
969974
}
970975
}
971976
else if(this.sortMode === 'multiple') {
972977
if(this.multiSortMeta) {
973978
for(let i = 0; i < this.multiSortMeta.length; i++) {
974-
if(this.multiSortMeta[i].field == column.field) {
979+
if(this.multiSortMeta[i].field == columnSortField) {
975980
order = this.multiSortMeta[i].order;
976981
break;
977982
}

showcase/demo/datatable/datatabledemo.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ <h3>Attributes</h3>
117117
<td>null</td>
118118
<td>Property of a row data.</td>
119119
</tr>
120+
<tr>
121+
<td>sortField</td>
122+
<td>string</td>
123+
<td>null</td>
124+
<td>Property of a row data used for sorting, defaults to field.</td>
125+
</tr>
120126
<tr>
121127
<td>header</td>
122128
<td>string</td>
@@ -495,7 +501,8 @@ <h3>Sorting</h3>
495501
</code>
496502
</pre>
497503

498-
<p>To customize sorting, set sortable option to <i>custom</i> and define a sortFunction that sorts the list.</p>
504+
<p>To customize sorting, set sortable option to <i>custom</i> and define a sortFunction that sorts the list. The property to use when
505+
sorting is field by default and this can be customized using sortField.</p>
499506
<pre>
500507
<code class="language-markup" pCode>
501508
&lt;p-dataTable [value]="cars" [multiSortMeta]="multiSortMeta"&gt;

0 commit comments

Comments
 (0)