简体   繁体   English

我需要帮助生成标题。 使用react.js

[英]I need help generating header. using react.js

I am trying to create a table using objects right now my table generates the table rows but I also want it to generate the table header using the same object, right now the table header is being hard coded, if someone could help me with how to generate the table header inside the same object 'schoolData'. 我正在尝试使用对象创建表,现在我的表生成表行,但我也希望它使用相同的对象生成表头,现在表头正在被硬编码,如果有人可以帮助我在同一对象“ schoolData”中生成表头。

//Rows

const schoolData = [
  { school : 'Middle School', year : '2017', date : '6/12/17', location : 
'Class',},
  { school : 'High School', year : '2017', date : '2/24/17', location : 
'Gym',},

];


export default class TableComplex extends Component {

  render() {
    return (
      <Tabs>
    <Tab label="School Type">
    <Table>
      <TableHeader displaySelectAll={false} adjustForCheckbox={false}>
        <TableRow>
          <TableHeaderColumn>School</TableHeaderColumn>
          <TableHeaderColumn>Year</TableHeaderColumn>
          <TableHeaderColumn>Date</TableHeaderColumn>
          <TableHeaderColumn>Location</TableHeaderColumn>
        </TableRow>
      </TableHeader>
      <TableBody displayRowCheckbox={false}>
        {schoolData.map( (row) => (
          <TableRow>
            <TableRowColumn>{row.school}</TableRowColumn>
            <TableRowColumn>{row.year}</TableRowColumn>
            <TableRowColumn>{row.date}</TableRowColumn>
            <TableRowColumn>{row.location}</TableRowColumn>
          </TableRow>
          ))}
      </TableBody>
    </Table>
  </Tab>

  </Tabs>

);
  }
}

您可以使用对象键

for ( property in schoolData[0] ) { console.log( property ); }

You can use object.keys on on the first element of school data and map TableHeaderColumn 您可以在学校数据的第一个元素上使用object.keys并映射TableHeaderColumn

 const schoolData = [ { school : 'Middle School', year : '2017', date : '6/12/17', location : 'Class',}, { school : 'High School', year : '2017', date : '2/24/17', location : 'Gym',}, ]; // declare this in render method const headers = Object.keys(schoolData[0]).map(header => <TableHeaderColumn>{header}</TableHeaderColumn>); // put it in your react component <TableHeader displaySelectAll={false} adjustForCheckbox={false}> <TableRow> { headers } </TableRow> </TableHeader> // for TableRows const rows = (row) => Object.keys(row).map(r => <TableRowColumn>{row[r]}</TableRowColumn>); <TableRow> {rows(row)} </TableRow> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM