Skip to content
Snippets Groups Projects
Commit 9d160a85 authored by piotr.kupczyk@id.ethz.ch's avatar piotr.kupczyk@id.ethz.ch
Browse files

SSDM-7594 : NEW openBIS UI - General Template/Infrastructure for Tables -...

SSDM-7594 : NEW openBIS UI - General Template/Infrastructure for Tables - Visualisation/Creation/Edit - make visible columns configurable
parent c1872913
No related branches found
No related tags found
No related merge requests found
import _ from 'lodash'
import React from 'react'
import Popover from '@material-ui/core/Popover'
import Button from '@material-ui/core/Button'
import FormControlLabel from '@material-ui/core/FormControlLabel'
import Checkbox from '@material-ui/core/Checkbox'
import logger from '../../../common/logger.js'
class ColumnsConfig extends React.Component {
constructor(props){
super(props)
this.state = {
el: null
}
this.handleOpen = this.handleOpen.bind(this)
this.handleClose = this.handleClose.bind(this)
this.handleChange = this.handleChange.bind(this)
}
handleOpen(event){
this.setState({
el: event.currentTarget
})
}
handleClose(){
this.setState({
el: null
})
}
handleChange(event){
let columns = [...this.props.visibleColumns]
let column = event.target.value
if(columns.includes(column)){
_.pull(columns, column)
}else{
columns.push(column)
}
this.props.onColumnsChange(columns)
}
render() {
logger.log(logger.DEBUG, 'ColumnsConfig.render')
const { allColumns, visibleColumns } = this.props
const { el } = this.state
return (
<React.Fragment>
<Button
variant="contained"
onClick={this.handleOpen}
>
Columns
</Button>
<Popover
open={Boolean(el)}
anchorEl={el}
onClose={this.handleClose}
anchorOrigin={{
vertical: 'top',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
>
{allColumns.map(column => (
<FormControlLabel key={column}
control={
<Checkbox
value={column}
checked={visibleColumns.includes(column)}
onChange={this.handleChange}
/>
}
label={column}
/>
))}
</Popover>
</React.Fragment>
)
}
}
export default ColumnsConfig
...@@ -10,8 +10,9 @@ import TableHead from '@material-ui/core/TableHead' ...@@ -10,8 +10,9 @@ import TableHead from '@material-ui/core/TableHead'
import TableFooter from '@material-ui/core/TableFooter' import TableFooter from '@material-ui/core/TableFooter'
import TableRow from '@material-ui/core/TableRow' import TableRow from '@material-ui/core/TableRow'
import TableSortLabel from '@material-ui/core/TableSortLabel' import TableSortLabel from '@material-ui/core/TableSortLabel'
import Pagination from '../../common/table/Pagination.jsx'
import FilterField from '../../common/form/FilterField.jsx' import FilterField from '../../common/form/FilterField.jsx'
import ColumnsConfig from '../../common/table/ColumnsConfig.jsx'
import Pagination from '../../common/table/Pagination.jsx'
import * as pages from '../../../common/consts/pages.js' import * as pages from '../../../common/consts/pages.js'
import * as objectTypes from '../../../common/consts/objectType.js' import * as objectTypes from '../../../common/consts/objectType.js'
import * as actions from '../../../store/actions/actions.js' import * as actions from '../../../store/actions/actions.js'
...@@ -72,6 +73,8 @@ const entityKindToObjecType = { ...@@ -72,6 +73,8 @@ const entityKindToObjecType = {
'MATERIAL': objectTypes.MATERIAL_TYPE 'MATERIAL': objectTypes.MATERIAL_TYPE
} }
const allColumns = ['code', 'description']
function mapDispatchToProps(dispatch){ function mapDispatchToProps(dispatch){
return { return {
objectOpen: (objectType, objectId) => { dispatch(actions.objectOpen(pages.TYPES, objectType, objectId)) } objectOpen: (objectType, objectId) => { dispatch(actions.objectOpen(pages.TYPES, objectType, objectId)) }
...@@ -84,9 +87,11 @@ class Search extends React.Component { ...@@ -84,9 +87,11 @@ class Search extends React.Component {
super(props) super(props)
this.state = { this.state = {
loaded: false, loaded: false,
filter: '' filter: '',
visibleColumns: allColumns
} }
this.handleFilterChange = this.handleFilterChange.bind(this) this.handleFilterChange = this.handleFilterChange.bind(this)
this.handleColumnsChange = this.handleColumnsChange.bind(this)
this.handlePageChange = this.handlePageChange.bind(this) this.handlePageChange = this.handlePageChange.bind(this)
this.handlePageSizeChange = this.handlePageSizeChange.bind(this) this.handlePageSizeChange = this.handlePageSizeChange.bind(this)
this.handleLinkClick = this.handleLinkClick.bind(this) this.handleLinkClick = this.handleLinkClick.bind(this)
...@@ -98,7 +103,8 @@ class Search extends React.Component { ...@@ -98,7 +103,8 @@ class Search extends React.Component {
load(){ load(){
this.setState({ this.setState({
loaded: false loaded: false,
columnConfigEl: null
}) })
Promise.all([ Promise.all([
...@@ -166,6 +172,12 @@ class Search extends React.Component { ...@@ -166,6 +172,12 @@ class Search extends React.Component {
})) }))
} }
handleColumnsChange(visibleColumns){
this.setState(() => ({
visibleColumns
}))
}
handleSortChange(column){ handleSortChange(column){
return () => { return () => {
this.setState((prevState) => ({ this.setState((prevState) => ({
...@@ -237,7 +249,7 @@ class Search extends React.Component { ...@@ -237,7 +249,7 @@ class Search extends React.Component {
} }
const { classes } = this.props const { classes } = this.props
const { page, pageSize, filter, sort, sortDirection, allTypes } = this.state const { page, pageSize, filter, visibleColumns, sort, sortDirection, allTypes } = this.state
let types = [...allTypes] let types = [...allTypes]
types = this.filter(types) types = this.filter(types)
...@@ -256,39 +268,51 @@ class Search extends React.Component { ...@@ -256,39 +268,51 @@ class Search extends React.Component {
<Table classes={{ root: classes.table }}> <Table classes={{ root: classes.table }}>
<TableHead classes={{ root: classes.tableHeader }}> <TableHead classes={{ root: classes.tableHeader }}>
<TableRow> <TableRow>
<TableCell> {
<TableSortLabel visibleColumns.includes('code') &&
active={sort === 'code'} <TableCell>
direction={sortDirection} <TableSortLabel
onClick={this.handleSortChange('code')} active={sort === 'code'}
> direction={sortDirection}
onClick={this.handleSortChange('code')}
>
Code Code
</TableSortLabel> </TableSortLabel>
</TableCell> </TableCell>
<TableCell> }
<TableSortLabel {
active={sort === 'description'} visibleColumns.includes('description') &&
direction={sortDirection} <TableCell>
onClick={this.handleSortChange('description')} <TableSortLabel
> active={sort === 'description'}
direction={sortDirection}
onClick={this.handleSortChange('description')}
>
Description Description
</TableSortLabel> </TableSortLabel>
</TableCell> </TableCell>
}
</TableRow> </TableRow>
</TableHead> </TableHead>
<TableBody> <TableBody>
{types.map(type => ( {types.map(type => (
<TableRow key={type.permId.entityKind + '-' + type.permId.permId} hover> <TableRow key={type.permId.entityKind + '-' + type.permId.permId} hover>
<TableCell> {
<Link visibleColumns.includes('code') &&
component="button" <TableCell>
classes={{ root: classes.tableLink }} <Link
onClick={this.handleLinkClick(type.permId)}>{type.code} component="button"
</Link> classes={{ root: classes.tableLink }}
</TableCell> onClick={this.handleLinkClick(type.permId)}>{type.code}
<TableCell> </Link>
{type.description} </TableCell>
</TableCell> }
{
visibleColumns.includes('description') &&
<TableCell>
{type.description}
</TableCell>
}
</TableRow> </TableRow>
))} ))}
<TableRow classes={{ root: classes.tableSpacer }}> <TableRow classes={{ root: classes.tableSpacer }}>
...@@ -297,6 +321,13 @@ class Search extends React.Component { ...@@ -297,6 +321,13 @@ class Search extends React.Component {
</TableBody> </TableBody>
<TableFooter classes={{ root: classes.tableFooter }}> <TableFooter classes={{ root: classes.tableFooter }}>
<TableRow> <TableRow>
<TableCell>
<ColumnsConfig
allColumns={allColumns}
visibleColumns={visibleColumns}
onColumnsChange={this.handleColumnsChange}
/>
</TableCell>
<Pagination <Pagination
count={allTypes.length} count={allTypes.length}
page={page} page={page}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment