Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在reactjs中对表进行分组?_Reactjs_Material Ui - Fatal编程技术网

如何在reactjs中对表进行分组?

如何在reactjs中对表进行分组?,reactjs,material-ui,Reactjs,Material Ui,我正在使用material UI中的Table控件来显示数据,但是现在我需要显示组数据,包括展开和折叠数据。是否有任何包可以实现此目标?请帮助我 import { TableRowProps, Table, TableBody, TableFooter, PagiFooter, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table'; <Table> <Tabl

我正在使用material UI中的Table控件来显示数据,但是现在我需要显示组数据,包括展开和折叠数据。是否有任何包可以实现此目标?请帮助我

import { TableRowProps, Table, TableBody, TableFooter, PagiFooter, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table';
 <Table>
  <TableHeader {...headerProps}>
  {this.renderHeaders()}
  </TableHeader>
  <TableBody displayRowCheckbox={false}
  deselectOnClickaway={false}>
  {this.renderRows()}
  </TableBody>
</Table>
import{TableRowProps,Table,TableBody,TableFooter,PagiFooter,TableHeader,TableHeaderColumn,TableRow,TableRowColumn}从“物料ui/表格”导入;
{this.renderHeaders()}
{this.renderRows()}

表格组件目前不支持扩展,但您可以使用可扩展卡组件解决此问题,只需确保调整行的垂直对齐即可

下面是一段代码片段:

<TableBody displayRowCheckbox={false}>
  {this.props.user.leads.map((lead, i) => (
    <TableRow key={i}>
      <TableRowColumn>
        <Card style={{boxShadow: 'none'}}>
          <CardHeader
            title={lead.brand}
            style={{paddingLeft: 0, fontWeight: 600}}
            actAsExpander={true}
          />
          <CardText expandable={true}>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Donec mattis pretium massa. Aliquam erat volutpat. Nulla facilisi.
            Donec vulputate interdum sollicitudin. Nunc lacinia auctor quam sed pellentesque.
            Aliquam dui mauris, mattis quis lacus id, pellentesque lobortis odio.
          </CardText>
        </Card>
      </TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>{lead.budget}</TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>{lead.eventName ? 'Event' : 'Content'}</TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>{lead.firstName + ' ' + lead.lastName}</TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>Archive | Start Campaign</TableRowColumn>
    </TableRow>
  ))}
</TableBody>

{this.props.user.leads.map((lead,i)=>(
Lorem ipsum dolor sit amet,是一位杰出的献身者。
Donec mattis pretium massa.不可否认的事实。
请不要在拍卖会上提出异议,因为拍卖人是佩伦茨克人。
在毛里斯河对岸,马蒂斯·奎斯·拉库斯,佩伦特斯·洛博蒂斯·奥迪奥。
{lead.budget}
{lead.eventName?'Event':'Content'}
{lead.firstName+''+lead.lastName}
存档|启动活动
))}

表格组件目前不支持扩展,但您可以使用可扩展卡组件解决此问题,只需确保调整行的垂直对齐即可

下面是一段代码片段:

<TableBody displayRowCheckbox={false}>
  {this.props.user.leads.map((lead, i) => (
    <TableRow key={i}>
      <TableRowColumn>
        <Card style={{boxShadow: 'none'}}>
          <CardHeader
            title={lead.brand}
            style={{paddingLeft: 0, fontWeight: 600}}
            actAsExpander={true}
          />
          <CardText expandable={true}>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Donec mattis pretium massa. Aliquam erat volutpat. Nulla facilisi.
            Donec vulputate interdum sollicitudin. Nunc lacinia auctor quam sed pellentesque.
            Aliquam dui mauris, mattis quis lacus id, pellentesque lobortis odio.
          </CardText>
        </Card>
      </TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>{lead.budget}</TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>{lead.eventName ? 'Event' : 'Content'}</TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>{lead.firstName + ' ' + lead.lastName}</TableRowColumn>
      <TableRowColumn style={{verticalAlign: 'top', height: 'auto', paddingTop: '1.4em'}}>Archive | Start Campaign</TableRowColumn>
    </TableRow>
  ))}
</TableBody>

{this.props.user.leads.map((lead,i)=>(
Lorem ipsum dolor sit amet,是一位杰出的献身者。
Donec mattis pretium massa.不可否认的事实。
请不要在拍卖会上提出异议,因为拍卖人是佩伦茨克人。
在毛里斯河对岸,马蒂斯·奎斯·拉库斯,佩伦特斯·洛博蒂斯·奥迪奥。
{lead.budget}
{lead.eventName?'Event':'Content'}
{lead.firstName+''+lead.lastName}
存档|启动活动
))}

物料界面的一种方法,但也可以与其他表格格式中的相同结构一起使用:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import Collapse from '@material-ui/core/Collapse';
import CardActions from '@material-ui/core/CardActions';
import IconButton from '@material-ui/core/IconButton';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';

const styles = theme => ({
  root: {
    width: '100%',
    marginTop: theme.spacing.unit * 3,
    overflowX: 'auto',
  },
  table: {
    minWidth: 700,
  },
  expand: {
    transform: 'rotate(0deg)',
    transition: theme.transitions.create('transform', {
      duration: theme.transitions.duration.shortest,
    }),
    marginLeft: 'auto',
    [theme.breakpoints.up('sm')]: {
      marginRight: -8,
    },
  },
  expandOpen: {
    transform: 'rotate(180deg)',
  },
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
  id += 1;
  return { id, name, calories, fat, carbs, protein };
}

const rows = [
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Gingerbread', 356, 16.0, 49, 3.9),
  createData('Gingerbread', 356, 16.0, 49, 3.9)
];
class TableGroup extends React.Component {
  constructor(props) {
    super(props);
    this.state = { expanded: {} };
  }
  handleExpandClick(key) {
    let state = { ...this.state };
    state.expanded[key] = !!!state.expanded[key];
    this.setState(state);
  }
  render() {
    const props = this.props;
    const { groupProperty, rows, ...other } = props;
    const groups = rows.reduce((acc, row) => {
      acc[row[groupProperty]] = acc[row[groupProperty]] || [];
      acc[row[groupProperty]].push(row);
      return acc;
    }, {});
    const keys = Object.keys(groups);


    return keys.map(key => (
      <TableRow>
        <TableCell colspan="5">
          <CardActions disableActionSpacing>
            <b>{key} ({groups[key].length})</b>
            <IconButton
              className={this.state.expanded[key] ? props.classes.expandOpen : props.classes.expand}
              onClick={this.handleExpandClick.bind(this, key)}
              aria-expanded={this.state.expanded[key]}
              aria-label="Show more"
            >
              <ExpandMoreIcon />
            </IconButton>
          </CardActions>
          <Collapse in={this.state.expanded[key]} timeout="auto" unmountOnExit>
            <Table {...other}>
              <TableBody>
                {groups[key].map(row => (
                  <TableRow key={row.id}>
                    <TableCell component="th" scope="row">
                      {row.name}
                    </TableCell>
                    <TableCell numeric>{row.calories}</TableCell>
                    <TableCell numeric>{row.fat}</TableCell>
                    <TableCell numeric>{row.carbs}</TableCell>
                    <TableCell numeric>{row.protein}</TableCell>
                  </TableRow>
                )
                )}
              </TableBody>
            </Table>
          </Collapse>
        </TableCell>
      </TableRow>
    )
    )
  }
};

function SimpleTable(props) {
  const { classes } = props;

  return (
    <Paper className={classes.root}>
      <Table >
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell numeric>Calories</TableCell>
            <TableCell numeric>Fat (g)</TableCell>
            <TableCell numeric>Carbs (g)</TableCell>
            <TableCell numeric>Protein (g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          <TableGroup classes={classes} rows={rows} groupProperty="name" />
        </TableBody>
      </Table>
    </Paper>
  );
}

SimpleTable.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SimpleTable);
从“React”导入React;
从“道具类型”导入道具类型;
从“@material ui/core/styles”导入{withStyles}”;
从“@material ui/core/Table”导入表格;
从“@material ui/core/TableBody”导入表体;
从“@material ui/core/TableCell”导入TableCell;
从“@material ui/core/TableHead”导入表头;
从“@material ui/core/TableRow”导入TableRow;
从“@material ui/core/Paper”导入纸张;
从“@material ui/core/Collapse”导入折叠;
从“@material ui/core/CardActions”导入CardActions;
从“@material ui/core/IconButton”导入IconButton;
从“@material ui/icons/ExpandMore”导入ExpandMoreIcon;
常量样式=主题=>({
根目录:{
宽度:“100%”,
marginTop:theme.space.unit*3,
溢出x:'自动',
},
表:{
最小宽度:700,
},
扩展:{
变换:“旋转(0度)”,
转换:theme.transitions.create('transform'{
持续时间:theme.transitions.duration.shortest,
}),
marginLeft:'自动',
[theme.breakpoints.up('sm'):{
marginRight:-8,
},
},
expandOpen:{
变换:“旋转(180度)”,
},
});
设id=0;
函数createData(名称、卡路里、脂肪、碳水化合物、蛋白质){
id+=1;
返回{id,name,carries,fat,carbs,protein};
}
常量行=[
createData(“冷冻酸奶”,159,6.0,24,4.0),
createData(“冷冻酸奶”,159,6.0,24,4.0),
createData(“冰淇淋三明治”,237,9.0,37,4.3),
createData(“冰淇淋三明治”,237,9.0,37,4.3),
createData('Eclair',262,16.0,24,6.0),
createData('Eclair',262,16.0,24,6.0),
createData('Cupcake',305,3.7,67,4.3),
createData('Cupcake',305,3.7,67,4.3),
createData('Gingerbread',356,16.0,49,3.9),
createData('Gingerbread',356,16.0,49,3.9)
];
类TableGroup扩展了React.Component{
建造师(道具){
超级(道具);
this.state={扩展:{};
}
handleExpandClick(键){
让state={…this.state};
state.expanded[key]=!!!state.expanded[key];
本.设置状态(状态);
}
render(){
const props=this.props;
const{groupProperty,rows,…other}=props;
常量组=行。减少((acc,行)=>{
acc[row[groupProperty]]=acc[row[groupProperty]]| |[];
acc[行[组属性]]推送(行);
返回acc;
}, {});
常量键=对象键(组);
返回key.map(key=>(
{key}({groups[key].length})
{groups[key].map(行=>(
{row.name}
{row.carries}
{row.fat}
{row.carbs}
{row.protein}
)
)}
)
)
}
};
函数SimpleTable(props){
常量{classes}=props;
返回(
甜点(100克)
卡路里
脂肪(g)
碳水化合物(克)
蛋白质(g)
);
}
SimpleTable.propTypes={
类:PropTypes.object.isRequired,
};
导出默认样式(样式)(SimpleTable);

物料界面的一种方法,但也可以与其他表格格式中的相同结构一起使用:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import Collapse from '@material-ui/core/Collapse';
import CardActions from '@material-ui/core/CardActions';
import IconButton from '@material-ui/core/IconButton';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';

const styles = theme => ({
  root: {
    width: '100%',
    marginTop: theme.spacing.unit * 3,
    overflowX: 'auto',
  },
  table: {
    minWidth: 700,
  },
  expand: {
    transform: 'rotate(0deg)',
    transition: theme.transitions.create('transform', {
      duration: theme.transitions.duration.shortest,
    }),
    marginLeft: 'auto',
    [theme.breakpoints.up('sm')]: {
      marginRight: -8,
    },
  },
  expandOpen: {
    transform: 'rotate(180deg)',
  },
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
  id += 1;
  return { id, name, calories, fat, carbs, protein };
}

const rows = [
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Gingerbread', 356, 16.0, 49, 3.9),
  createData('Gingerbread', 356, 16.0, 49, 3.9)
];
class TableGroup extends React.Component {
  constructor(props) {
    super(props);
    this.state = { expanded: {} };
  }
  handleExpandClick(key) {
    let state = { ...this.state };
    state.expanded[key] = !!!state.expanded[key];
    this.setState(state);
  }
  render() {
    const props = this.props;
    const { groupProperty, rows, ...other } = props;
    const groups = rows.reduce((acc, row) => {
      acc[row[groupProperty]] = acc[row[groupProperty]] || [];
      acc[row[groupProperty]].push(row);
      return acc;
    }, {});
    const keys = Object.keys(groups);


    return keys.map(key => (
      <TableRow>
        <TableCell colspan="5">
          <CardActions disableActionSpacing>
            <b>{key} ({groups[key].length})</b>
            <IconButton
              className={this.state.expanded[key] ? props.classes.expandOpen : props.classes.expand}
              onClick={this.handleExpandClick.bind(this, key)}
              aria-expanded={this.state.expanded[key]}
              aria-label="Show more"
            >
              <ExpandMoreIcon />
            </IconButton>
          </CardActions>
          <Collapse in={this.state.expanded[key]} timeout="auto" unmountOnExit>
            <Table {...other}>
              <TableBody>
                {groups[key].map(row => (
                  <TableRow key={row.id}>
                    <TableCell component="th" scope="row">
                      {row.name}
                    </TableCell>
                    <TableCell numeric>{row.calories}</TableCell>
                    <TableCell numeric>{row.fat}</TableCell>
                    <TableCell numeric>{row.carbs}</TableCell>
                    <TableCell numeric>{row.protein}</TableCell>
                  </TableRow>
                )
                )}
              </TableBody>
            </Table>
          </Collapse>
        </TableCell>
      </TableRow>
    )
    )
  }
};

function SimpleTable(props) {
  const { classes } = props;

  return (
    <Paper className={classes.root}>
      <Table >
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell numeric>Calories</TableCell>
            <TableCell numeric>Fat (g)</TableCell>
            <TableCell numeric>Carbs (g)</TableCell>
            <TableCell numeric>Protein (g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          <TableGroup classes={classes} rows={rows} groupProperty="name" />
        </TableBody>
      </Table>
    </Paper>
  );
}

SimpleTable.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SimpleTable);
从“React”导入React;
从“道具类型”导入道具类型;
从“@material ui/core/styles”导入{withStyles}”;
从“@material ui/core/Table”导入表格;
从“@material ui/core/TableBody”导入表体;
从“@material ui/core/TableCell”导入TableCell;
从“@material ui/core/TableHead”导入表格头