首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回一组<td>单元

返回一组<td>单元
EN

Stack Overflow用户
提问于 2018-07-02 05:55:05
回答 2查看 61关注 0票数 0

我正在通过考勤数组映射对象,并试图在考勤记录存在时返回<td></td>单元格,在不存在考勤记录时返回空<td></td>单元格。我目前只有在考勤记录存在的情况下才能返回<td></td>单元。如何返回空的<td></td>单元格?

路径:file.jsx

代码语言:javascript
复制
render() {
  return (
    <div className="mt-3">
      <Table hover>
        <thead>
          <tr>
            <th className="border-top-0 pt-0">Absent</th>
            <th className="border-top-0 pt-0">Present</th>
          </tr>
        </thead>
        <tbody>
          {this.props.studentUserProfiles.map(studentUserProfile => (
            <tr key={studentUserProfile._id}>
              {this.props.attendances.map((attendance) => {
                if (attendance.studentUserProfileId === studentUserProfile._id) {
                  return (
                    <React.Fragment key={attendance._id}>
                      <td>{attendance.absentRollCallTeacherUserId ? 'True' : null}</td>
                      <td>{studentUserProfile.presentRollCallTeacherUserId ? 'True' : null}</td>
                    </React.Fragment>
                  );
                }
              })}
            </tr>
          ))}
        </tbody>
      </Table>
    </div>
  );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-02 06:21:52

也许这个能帮你

代码语言:javascript
复制
{this.props.studentUserProfiles.map(studentUserProfile => (
    <tr key={studentUserProfile._id}>
    {this.props.attendances.find(attend => studentUserProfile._id === attend.id) ? this.props.attendances.map((attendance) => {
        if (attendance.studentUserProfileId === studentUserProfile._id) {
            return (
                <React.Fragment key={attendance._id}>
                <td>{attendance.absentRollCallTeacherUserId ? 'True' : null}</td>
                <td>{studentUserProfile.presentRollCallTeacherUserId ? 'True' : null}</td>
                </React.Fragment>
            );
        }
    }) : (
        <React.Fragment>
            <td></td>
            <td></td>
            </React.Fragment>
    )}
    </tr>
))}

票数 1
EN

Stack Overflow用户

发布于 2018-07-02 06:11:35

只需在循环开始之前检查this.props.attendance的长度,然后使用JSX返回所需的标记。

代码语言:javascript
复制
render() {
    return (
      <div className="mt-3">
        <Table hover>
          <thead>
            <tr>
              <th className="border-top-0 pt-0">Absent</th>
              <th className="border-top-0 pt-0">Present</th>
            </tr>
          </thead>
          <tbody>
            {this.props.studentUserProfiles.map(studentUserProfile => (
              <tr key={studentUserProfile._id}>
              { this.props.attendance.length > 0 &&
                    <React.Fragment>
                     {this.props.attendances.map((attendance) => {
                         return (
                            <React.Fragment key={attendance._id}>
                                {attendance.studentUserProfileId == studentUserProfile._id &&
                                    <td>{attendance.absentRollCallTeacherUserId ? 'True' : null}</td>
                                    <td>{studentUserProfile.presentRollCallTeacherUserId ? 'True' : null}</td>
                                }
                                {attendance.studentUserProfileId != studentUserProfile._id &&
                                    <td></td>
                                    <td></td>
                                }
                            </React.Fragment>
                            );
                    })}
                </React.Fragment>
              }

              { this.props.attendance.length == 0 &&
                <React.Fragment >
                <td></td>
                <td></td>
               </React.Fragment>
              }

              </tr>
            ))}
          </tbody>
        </Table>
      </div>
    );
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51129817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档