Skip to content

Commit ac2b85f

Browse files
authored
Merge pull request #48 from ashvin27/custom_on_sort_function_#38
Custom on sort function #38
2 parents c9bc890 + c68c998 commit ac2b85f

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

example/dist/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23925,6 +23925,13 @@ var UserList = function (_Component) {
2392523925
value: function pageChange(pageData) {
2392623926
console.log("OnPageChange", pageData);
2392723927
}
23928+
}, {
23929+
key: 'customSort',
23930+
value: function customSort(column, records, sortOrder) {
23931+
console.log("column: %s, records: %O, sortOrder: %s", column, records, sortOrder);
23932+
23933+
return records;
23934+
}
2392823935
}, {
2392923936
key: 'render',
2393023937
value: function render() {
@@ -23938,7 +23945,8 @@ var UserList = function (_Component) {
2393823945
columns: this.columns,
2393923946
onPageChange: this.pageChange.bind(this),
2394023947
extraButtons: this.extraButtons,
23941-
loading: this.state.loading
23948+
loading: this.state.loading,
23949+
onSort: this.customSort
2394223950
})
2394323951
);
2394423952
}
@@ -24090,7 +24098,7 @@ var ReactDatatable = function (_Component) {
2409024098
}
2409124099
}, {
2409224100
key: 'sortColumn',
24093-
value: function sortColumn(column, sortOrder) {
24101+
value: function sortColumn(event, column, sortOrder) {
2409424102
var _this4 = this;
2409524103

2409624104
if (!column.sortable) return false;
@@ -24541,18 +24549,18 @@ var ReactDatatable = function (_Component) {
2454124549
isFirst = void 0,
2454224550
isLast = void 0;
2454324551
if (this.props.dynamic === false) {
24544-
var records = this.sortRecords(),
24552+
var records = this.props.onSort ? this.props.onSort(this.state.sort.column, this.props.records, this.state.sort.order) : this.sortRecords(),
2454524553
filterValue = this.state.filter_value;
2454624554
filterRecords = records;
2454724555

2454824556
if (filterValue) {
2454924557
filterRecords = this.filterData(records);
2455024558
}
24551-
totalRecords = filterRecords.length;
24559+
totalRecords = Array.isArray(filterRecords) ? filterRecords.length : 0;
2455224560
pages = this.pages = this.numPages(totalRecords);
2455324561
isFirst = this.isFirst();
2455424562
isLast = this.isLast();
24555-
filterRecords = this.paginate(filterRecords);
24563+
filterRecords = Array.isArray(filterRecords) ? this.paginate(filterRecords) : [];
2455624564
} else {
2455724565
filterRecords = this.props.records;
2455824566
totalRecords = this.props.total_record;
@@ -24621,8 +24629,8 @@ var ReactDatatable = function (_Component) {
2462124629
className: classText,
2462224630
width: width,
2462324631
style: columnStyle,
24624-
onClick: function onClick() {
24625-
return _this8.sortColumn(column, sortOrder);
24632+
onClick: function onClick(event) {
24633+
return _this8.sortColumn(event, column, sortOrder);
2462624634
} },
2462724635
column.text
2462824636
);

example/src/UserList.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ class UserList extends Component {
158158
console.log("OnPageChange", pageData);
159159
}
160160

161+
customSort(column, records, sortOrder) {
162+
console.log("column: %s, records: %O, sortOrder: %s", column, records, sortOrder);
163+
164+
return records;
165+
}
166+
161167
render() {
162168
return (
163169
<div>
@@ -169,6 +175,7 @@ class UserList extends Component {
169175
onPageChange={this.pageChange.bind(this)}
170176
extraButtons={this.extraButtons}
171177
loading={this.state.loading}
178+
onSort={this.customSort}
172179
/>
173180
</div>
174181
)

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ReactDatatable extends Component {
8787
});
8888
}
8989

90-
sortColumn(column, sortOrder) {
90+
sortColumn(event, column, sortOrder) {
9191
if (!column.sortable) return false;
9292
let newSortOrder = (sortOrder == "asc") ? "desc" : "asc";
9393
this.setState({
@@ -410,18 +410,18 @@ class ReactDatatable extends Component {
410410
render() {
411411
let filterRecords, totalRecords, pages, isFirst, isLast;
412412
if(this.props.dynamic === false){
413-
let records = this.sortRecords(),
413+
let records = (this.props.onSort) ? this.props.onSort(this.state.sort.column, this.props.records, this.state.sort.order) : this.sortRecords(),
414414
filterValue = this.state.filter_value;
415415
filterRecords = records;
416416

417417
if (filterValue) {
418418
filterRecords = this.filterData(records);
419419
}
420-
totalRecords = filterRecords.length;
420+
totalRecords = Array.isArray(filterRecords) ? filterRecords.length : 0;
421421
pages = this.pages = this.numPages(totalRecords);
422422
isFirst = this.isFirst();
423423
isLast = this.isLast();
424-
filterRecords = this.paginate(filterRecords);
424+
filterRecords = Array.isArray(filterRecords) ? this.paginate(filterRecords) : [];
425425
}else{
426426
filterRecords = this.props.records;
427427
totalRecords = this.props.total_record;
@@ -479,7 +479,7 @@ class ReactDatatable extends Component {
479479
className={classText}
480480
width={width}
481481
style={columnStyle}
482-
onClick={() => this.sortColumn(column, sortOrder)}>
482+
onClick={event => this.sortColumn(event, column, sortOrder)}>
483483
{column.text}
484484
</th>);
485485
})

0 commit comments

Comments
 (0)