Comments (13)
my implementation of sort, filter and pagination :
import DataTables from 'material-ui-datatables'
import users from '../data/users.json'
import { sortByStringAscending, sortByStringDescending } from '../helpers/sorting'
import { TABLE_COLUMNS_USERS } from '../constants/tableColumnsSpecifications'
class Users extends Component {
state = {
searchPhrase: "",
page: 1,
rowSize: 10,
}
handleFilter = value => {
this.setState({
searchPhrase: value
})
}
handlePreviousPageClick = () => {
this.setState({page: this.state.page - 1})
}
handleNextPageClick = () => {
this.setState({page: this.state.page + 1})
}
handleRowSizeChange = (rowSizeIndex, rowSize) => {
this.setState({page: 1, rowSize});
}
render() {
let data = sortByStringAscending([...users], 'email')
.filter(row =>
row.email.includes(this.state.searchPhrase) ||
row.lastName.includes(this.state.searchPhrase) ||
row.firstName.includes(this.state.searchPhrase))
let displayData = data.slice(this.state.rowSize * (this.state.page - 1), this.state.rowSize * (this.state.page))
const handleSort = (key, order) => order === 'desc' ? sortByStringDescending(displayData, key) : sortByStringAscending(displayData, key)
return (
<DataTables
height={'auto'}
showRowHover={true}
columns={TABLE_COLUMNS_USERS}
data={displayData}
showCheckboxes={false}
onSortOrderChange={handleSort}
showHeaderToolbar={true}
showHeaderToolbarFilterIcon={false}
initialSort={{column: 'email', order: 'asc'}}
onFilterValueChange={this.handleFilter}
headerToolbarMode={'filter'}
count={data.length}
page={this.state.page}
rowSize={this.state.rowSize}
onPreviousPageClick={this.handlePreviousPageClick}
onNextPageClick={this.handleNextPageClick}
onRowSizeChange={this.handleRowSizeChange}
/>
)
}
}
export default Users
/////////////////////////////////////////////////////////////////////
and now for COLUMNS :
/////////////////////////////////////////////////////////////////////
export const TABLE_COLUMNS_USERS = [
{
key: 'firstName',
label: 'First Name',
sortable: true
}, {
key: 'lastName',
label: 'Last Name',
sortable: true
}, {
key: 'email',
label: 'Email',
sortable: true
}, {
key: 'type',
label: 'Type',
sortable: true
},
]
/////////////////////////////////////////////////////////////////////
and sort logic :
/////////////////////////////////////////////////////////////////////
export const sortByStringAscending = (array, condition) => array.sort((a, b) => a[condition].localeCompare(b[condition]))
export const sortByStringDescending = (array, condition) => array.sort((a, b) => b[condition].localeCompare(a[condition]))```
from material-ui-datatables.
confirmed, I tried to work on the example also, copy and paste the exact same codes, doesn't work
from material-ui-datatables.
@UkiMiawz the only way to get it working is write your own sort/paging/filter handlers.
from material-ui-datatables.
@hyojin, can I make the filter section displayed by default?
from material-ui-datatables.
@logysis I'm going to add the props which can handle the search bar. #44
from material-ui-datatables.
Great news @hyojin
from material-ui-datatables.
Hi, how do you simulate a click on the cell in an Enzyme test?
from material-ui-datatables.
from material-ui-datatables.
@hyojin can you make filter section displayed by default with some value in it?
from material-ui-datatables.
@logysis yes, you can use props, headerToolbarMode and filterValue.
here is example
from material-ui-datatables.
@hyojin thank you!
from material-ui-datatables.
@hyojin just wondering, when are you going to release a version with built-in paging/sort/filter?
from material-ui-datatables.
@hyojin another question, is there a way to place the cursor in search box by default?
from material-ui-datatables.
Related Issues (20)
- Boolean type field results data not rendered. HOT 1
- object with included object HOT 2
- Tooltip overflowing container HOT 2
- Bumping to MUI 19
- show viewed rows
- disable when loading
- is it supports with server side data ?
- i am getting below error HOT 5
- rowSizeList can not open the list menu HOT 1
- Button instead of IconButton in toolbar.
- Total Page number show in drop menu
- popover of rowSize is opened on top left screen instead of on place HOT 1
- react 16? HOT 3
- peer dependency material-ui
- autosize column width as per contents
- TypeError: Cannot read property 'baseTheme' of undefined HOT 3
- onTouchTap Warning
- muiTheme problem with "material-ui": "next"
- getting Warning: Expected `onMouseUp` listener to be a function, instead got `false`.
- The Table rows don't stay selected when the state of a variable changes.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from material-ui-datatables.