Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions src/components/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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';
Copy link
Contributor

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 is setSortState /shrug

Copy link
Author

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 the order, so it's not just toggling (I think of a toggle as only having 2 options, and column has more than that).

}
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();
}
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have wide in the initial state, but it doesn't seem to be used anywhere -- should this calc (and standardColumnWidth) be moved to the constructor?


Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would prefer to see a const with a ternary

Copy link
Author

Choose a reason for hiding this comment

The 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!

Copy link
Contributor

Choose a reason for hiding this comment

The 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>&#8593;</span> : <span>&#8595;</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>
Expand All @@ -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,
};
9 changes: 6 additions & 3 deletions src/components/MainView.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ export default class MainView extends React.Component {
}

parseCSV(results) {
const rowData = results.data.filter((d) => Object.keys(d).length > 1);
this.setState({
data: results.data,
filteredData: results.data,
data: rowData,
filteredData: rowData,
headers: Object.keys(rowData[0] || {}),
});
}

Expand Down Expand Up @@ -117,6 +119,7 @@ export default class MainView extends React.Component {

render() {
const data = this.state.data;
const headers = this.state.headers;
const filteredData = this.state.filteredData;
const dataSource = encodeURI(this.state.dataSource);
const isError = this.state.isError;
Expand Down Expand Up @@ -156,7 +159,7 @@ export default class MainView extends React.Component {
<hr />
<div className="row">
<div className="col-xs-12">
<DataTable limit={20} values={filteredData} />
<DataTable limit={20} values={filteredData} headers={headers} />
</div>
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ body {
.table-responsive {
border: none;
}

.table-header {
cursor: pointer;
}

.pager li a {
cursor: pointer;
display: inline-block;
Expand Down Expand Up @@ -54,4 +59,4 @@ thead {

tr:nth-child(even) {
background-color: #f7f7f7;
}
}