Last active
February 1, 2018 09:00
-
-
Save olegkalyta/3e7a791be6322bb51ffab7e381ebf146 to your computer and use it in GitHub Desktop.
React-Bootstrap-Table adding remote breaks sorting, first file shows how to reproduce it, second file - how I fix it now
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table' | |
import '../node_modules/react-bootstrap-table/dist/react-bootstrap-table-all.min.css' | |
const data = [ | |
{ | |
id: 0, | |
name: 'Oleg', | |
yearBorn: 1988, | |
}, | |
{ | |
id: 1, | |
name: 'Yaroslav', | |
yearBorn: 1993, | |
}, | |
{ | |
id: 2, | |
name: 'Alex', | |
yearBorn: 1991, | |
} | |
] | |
class App extends Component { | |
render() { | |
return ( | |
<div className="App"> | |
<BootstrapTable data={data} remote> | |
<TableHeaderColumn isKey dataField='id'>ID</TableHeaderColumn> | |
<TableHeaderColumn dataField='name' dataSort>Name</TableHeaderColumn> | |
<TableHeaderColumn dataField='yearBorn' dataSort>Yearn Born</TableHeaderColumn> | |
</BootstrapTable> | |
</div> | |
); | |
} | |
} | |
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeTable extends Component { | |
constructor(props) { | |
super(props) | |
const { rows } = props | |
this.state = { | |
data: rows || [] | |
} | |
} | |
onSortChange = (sortName, sortOrder) => { | |
const data = [...this.state.data] | |
const sortOrderIsAsc = sortOrder === 'asc' | |
const aBiggerB = sortOrderIsAsc ? -1 : 1 | |
const bBiggerA = sortOrderIsAsc ? 1 : -1 | |
data.sort(function(a, b) { | |
const aValue = | |
typeof a[sortName] === 'string' ? a[sortName].toLowerCase() : a | |
const bValue = | |
typeof b[sortName] === 'string' ? b[sortName].toLowerCase() : b | |
if (aValue > bValue) { | |
return aBiggerB | |
} else if (bValue > aValue) { | |
return bBiggerA | |
} | |
return 0 | |
}) | |
this.setState({ data }) | |
} | |
render() { | |
const { data } = this.state | |
return ( | |
<div className="portlet-body"> | |
<div className="table-scrollable-borderless"> | |
<BootstrapTable | |
remote | |
data={data} | |
> | |
<TableHeaderColumn | |
isKey | |
dataSort | |
width="400px" | |
dataField="title" | |
> | |
Column ... | |
</TableHeaderColumn> | |
</BootstrapTable> | |
</div> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment