简体   繁体   English

如何动态地将html表格单元格数据插入到React中带有动态标题的表格中?

[英]How do I dynamically insert html table cell data into a table with dynamic headers in React?

I'm creating a <table> with React 0.14.6 that has a series of column headers that are dynamically inserted into a <thead> of a <table> : 我正在创建一个带有React 0.14.6<table> ,它有一系列列标题,动态插入到<table><thead>中:

CourseTable.js : CourseTable.js

import CourseList from './CourseList';
import GradesHeader from './GradesHeader';
import React, {Component} from 'react';

export default class CourseTable extends Component {
  /**
   * Gets all unique terms that have grades
   * @param courses {Array}
   * @return {Array}
   */
  getUniqueTerms(courses) {
    let uniqueTerms = [];
    courses.forEach(course => {
      if (course.grades) {
        Object.keys(course.grades).forEach(term => {
          if (uniqueTerms.indexOf(term) === -1) {
            uniqueTerms.push(term);
          }
        })
      }
    });
    return uniqueTerms;
  }

  createGradesHeaders(courses) {
    return this.getUniqueTerms(courses).map(term => {
      return (
        <th>
          <GradesHeader headerText={term}/>
        </th>
      );
    });
  }

  render() {
    let headers = this.createGradesHeaders(this.props.courses);
    return (
      <table className="table table-bordered table-condensed">
        <thead>
        <tr>
          <th>
            Period
          </th>
          <th>
            Class
          </th>
          <th>
            Term
          </th>
          <th>
            Date Enrolled
          </th>
          <th>
            Date Left
          </th>
          <th>
            <div>Absenses</div>
            <div>All (Excused)</div>
          </th>
          <th>
            Tardies
          </th>
          <th></th>
          {headers}
          <th>
            Teacher
          </th>
        </tr>
        </thead>
        <CourseList courseData={this.props.courses}/>
      </table>
    );
  }
}

GradesHeader.js : GradesHeader.js

import React, {Component} from 'react';

export default class GradesHeader extends Component {
  render() {
    return (
      <div>
        {this.props.headerText}
      </div>
    );
  }
}

Course.js : Course.js

import React, {Component} from 'react';

export default class Course extends Component {
  render() {
    const unexcused = !!this.props.courseData.attendance.unexcused ? this.props.courseData.attendance.unexcused : 0;
    const excused = !!this.props.courseData.attendance.excused ? this.props.courseData.attendance.excused : 0;
    const totalAbsences = unexcused + excused;
    return (
      <tr>
        <td>
          {this.props.courseData.expression}
        </td>
        <td>
          {this.props.courseData.course_name}
        </td>
        <td>
          {this.props.courseData.abbreviation}
        </td>
        <td>
          {this.props.courseData.dateenrolled}
        </td>
        <td>
          {this.props.courseData.dateleft}
        </td>
        <td>
          {totalAbsences}
          ({excused})
        </td>
          // Grades for this course go here
        <td>
        </td>
        <td>
          {this.props.courseData.lastfirst}
        </td>
      </tr>
    );
  }
}

You can see in CourseTable.js where I'm dynamically generating the headers for this section of the table's <thead> . 你可以在CourseTable.js中看到我动态生成表格<thead>这一部分的标题。 I'm confused on how to insert/pidgeonhole the data for each grade into the corresponding column. 我对如何将每个等级的数据/ pidgeonhole插入相应的列感到困惑。

I think I need to somehow identify each of the <th> header elements that are dynamically generated, and reference those when I insert into those columns, but I'm not sure how. 我想我需要以某种方式识别动态生成的每个<th>头元素,并在插入这些列时引用它们,但我不确定如何。

You should, as mentioned in a comment, use states. 正如评论中所提到的,您应该使用状态。 States are pretty useful. 各州非常有用。 ReactJS states If you want to pidgeonhole new headers you could assign them with an ID, so that you could cycle through them correctly when rendering a new. ReactJS状态如果你想要pidgeonhole新标题,你可以为它们分配一个ID,这样你就可以在渲染新的时正确地循环它们。 As mentioned, react re-renders on state change. 如上所述,反应会重新呈现状态变化。 There are a few examples of this in facebooks tutorial page and also in the documentation. 在facebooks教程页面和文档中都有一些这样的例子。

Say you gather all your headers in an object, or perhaps they are coming from a database. 假设您在对象中收集所有标题,或者它们可能来自数据库。

var headers = [{id: 0, name: grade}
               {id: 1, name: term}
]

So in your render method of heads you can put them in like this: 所以在你的头部渲染方法中,你可以像这样把它们放进去:

constructor(props) {
    super(props);
    this.state = {
        headers: headers,
    }
},

sortAlg(arr) {
    //Choose one, make one
},

render() {
this.sortAlg(this.state.headers);
var headerArr = [];
headers.forEach(function(header) {
    headerArr.push(<YourHeader header = {header} />
});
return (
  <table className="table table-bordered table-condensed">
    <thead>
        {headerArr}
    </thead>
    <CourseList courseData={this.props.courses}/>
  </table>
);

The <YourHeader /> is a component you make for better control. <YourHeader />是您为更好地控制而制作的组件。 How I like doing it, atleast. 至少我喜欢这样做。

I hope this helps, good luck. 我希望这会有所帮助,祝你好运。

If I understand well the question and your data structure, you could pass the grades computed with getUniqueTerms to your CourseList component. 如果我很好地理解了问题和您的数据结构,您可以将使用getUniqueTerms计算的成绩传递给CourseList组件。

So in CourseTable.js : 所以在CourseTable.js

render() {
    let grades = this.uniqueTerms(this.props.courses);
    let headers = this.createGradesHeaders(grades);
    // and then in your jsx
    <CourseList courseData={ this.props.courses } grades={ grades } />
}

And in the render method of your Course component : Course组件的render方法中:

{
    this.props.grades.map( grade =>
        <td key={ grade }>
            // whatever data you need to print from this.props.courseData.grades[grade]
        </td>
    )
}

Hope it helps! 希望能帮助到你!

暂无
暂无

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

相关问题 如何动态填充表格中的单元格? - How do I fill a cell in the table dynamically? 如何在表单元格中动态插入div? - How to dynamically insert div in a table cell? 如果表格是动态生成的,如何在点击时更改html单元格的颜色? - How do I change an html cell's color on click, if the table is generated dynamically? 如何在javascript中将输入字段动态添加到我的html表格行内的单元格中? - How do I dynamically add an input field into a cell inside my html table row in javascript? 如何在 React JS 中为特定 ID 动态插入表中的输入数据? - How to insert the input data dynamically in table for particular ID in react JS? 用javascript动态生成表时如何在表单元格中插入空间 - How to insert space in table cell when generating table dynamically by javascript 如何访问动态表的最后一个单元格以获取总数 - How do I access the last cell of dynamic table to get the total 如何将表头添加到包含Ajax数据的动态生成的表中? - How to add table headers to dynamically generated table containing Ajax data? 如何在另一个Java脚本功能内的Java脚本中访问动态html表中单元格的动态分配值 - How to access the dynamically assigned value for a cell in the dynamic html table in java script within another java script function 如何在动态Json Table React JS和表格单元格数据比较中添加Row Span - how to add Row Span in dynamic Json Table React JS and table cell data comparison
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM