-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sorting to table columns #71
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,10 @@ export default class DataTable extends React.Component { | |
this.state = { | ||
page: 0, | ||
wide: false, | ||
sort: { | ||
column: props.headers[0], | ||
order: 'asc', | ||
}, | ||
}; | ||
this.resize = this.resize.bind(this); | ||
this.prevPage = this.prevPage.bind(this); | ||
|
@@ -29,6 +33,46 @@ export default class DataTable extends React.Component { | |
window.removeEventListener('resize', this.resize); | ||
} | ||
|
||
setSortState(col) { | ||
const { sort } = this.state; | ||
|
||
if (!sort.order) { | ||
sort.order = 'asc'; | ||
} | ||
if (sort.order && sort.column === col) { | ||
sort.order = sort.order === 'asc' ? 'desc' : 'asc'; | ||
} | ||
else { | ||
sort.order = 'asc'; | ||
} | ||
|
||
sort.column = col; | ||
this.setState({ sort }); | ||
} | ||
|
||
sortByOrder(column, sortOrder) { | ||
return (a, b) => { | ||
let result; | ||
if (a[column] < b[column]) { | ||
result = -1; | ||
} | ||
if (a[column] > b[column]) { | ||
result = 1; | ||
} | ||
if (a[column] === b[column]) { | ||
result = 0; | ||
} | ||
return result * sortOrder; | ||
}; | ||
} | ||
|
||
sortRows(values) { | ||
const { sort: { column, order } } = this.state; | ||
const sortOrder = order === 'asc' ? 1 : -1; | ||
const sortedValues = values.sort(this.sortByOrder(column, sortOrder)); | ||
return sortedValues; | ||
} | ||
|
||
resize() { | ||
this.forceUpdate(); | ||
} | ||
|
@@ -59,8 +103,8 @@ export default class DataTable extends React.Component { | |
const page = this.state.page; | ||
const start = page * limit; | ||
const end = start + limit; | ||
const values = props.values.slice(start, end); | ||
const columns = Object.keys(values[0] || {}); | ||
const values = this.sortRows(props.values).slice(start, end); | ||
const columns = props.headers; | ||
const standardColumnWidth = 100; | ||
const wide = (columns.length * standardColumnWidth < window.innerWidth); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have |
||
|
||
|
@@ -69,23 +113,36 @@ export default class DataTable extends React.Component { | |
<table className={'table table-responsive ' + (wide ? styles.wide : styles.narrow)}> | ||
{(() => { | ||
if (wide) { | ||
return (<thead> | ||
<tr> | ||
{columns.map((col, i) => <th key={i}>{col}</th>)} | ||
</tr> | ||
</thead>); | ||
return ( | ||
<thead> | ||
<tr> | ||
{columns.map((col, i) => { | ||
let arrow; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would prefer to see a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would create a nested ternary here, which the linter yelled at me for. I also feel like it makes it less readable, so I'm inclined to leave it as is unless you feel very strongly about it. Let me know! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about moving this embedded logic into other methods (which would be the first step to extracting them to components, were we to go that far)? For example: https://gist.github.com/bscofield/0fb968a9f69b14955b508f919d5f2b86 |
||
if (col === this.state.sort.column) { | ||
arrow = this.state.sort.order === 'asc' ? | ||
<span>↑</span> : <span>↓</span>; | ||
} | ||
return ( | ||
<th key={i} className="table-header" onClick={() => this.setSortState(col)}> | ||
{col} {arrow} | ||
</th> | ||
); | ||
})} | ||
</tr> | ||
</thead> | ||
); | ||
} | ||
return undefined; | ||
})()} | ||
|
||
<tbody> | ||
{values.map((val, i) => ( | ||
<tr key={i}> | ||
{columns.map((col, j) => ( | ||
{columns.map((col, j) => | ||
<td key={j}> | ||
{wide ? '' : <span>{col + ':'}</span>} {val[col]} | ||
</td> | ||
))} | ||
)} | ||
</tr> | ||
))} | ||
</tbody> | ||
|
@@ -107,4 +164,5 @@ export default class DataTable extends React.Component { | |
DataTable.propTypes = { | ||
limit: React.PropTypes.number.isRequired, | ||
values: React.PropTypes.array.isRequired, | ||
headers: React.PropTypes.array.isRequired, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this
toggleSortState
then? or maybe it issetSortState
/shrug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can update it if you think it's clearer, but it's setting the
column
as well as theorder
, so it's not just toggling (I think of a toggle as only having 2 options, andcolumn
has more than that).