【问题标题】:Nested Row with Table Row with Bootstrap 4 / ReactJS使用 Bootstrap 4 / ReactJS 的带有表行的嵌套行
【发布时间】:2019-10-27 20:23:09
【问题描述】:

我正在尝试使用 Bootstrap 4 和 React JS 在表格的整个宽度的表格行中创建一个下拉/折叠区域。我无法找到我想要的视觉效果,我只记得以前在某个地方使用过它(可能在 AWS 上?),但我无法再次找到它。

编辑:已找到所需的操作 (https://i.imgur.com/Pcg1JgI.png)。单击 V 形后,将打开下面的行。

这是我当前的代码:

import React from "react";
import { API, graphqlOperation } from 'aws-amplify';
import { listTrackerItems } from '../graphql/queries';

class TrackerGeneralPage extends React.Component {
    state = {
        id: '',
        active: [],
        completed: [],
        completedDateNow: '',
        newStatus: 'Complete'
      };

      async componentDidMount() {
        const result = await API.graphql(graphqlOperation(listTrackerItems))
        let data = result.data.listTrackerItems.items
        let General = data.filter(t=>t.department === 'General');
        const activeGeneral = General.filter(t=>t.status === 'Active');
        const completedGeneral = General.filter(t=>t.status === 'Completed');
        const date = new Date();
        const month = date.getUTCMonth() + 1;
        const day = date.getUTCDate();
        const year = date.getUTCFullYear();
        const timeNow = month + "/" + day + "/" + year;
        this.setState({active: activeGeneral, completed: completedGeneral, completedDateNow: timeNow });
      }

  render() {
    const { active } = this.state

    return (
        <>
        <div>
          <div class="container">
            <div class="row">
              <div class="col-sm">
              </div>
              <div class="col-sm">
                <div className="mx-auto" align="center">
                <h1>General</h1>
                </div>
              </div>
              <div class="col-sm">
                <div className="mx-auto mt-2" align="center">
                  {/* <!-- Button trigger modal --> */}
                  <a role="button" className="btn btn-dark" href="/tracker/nwa/lab/add" >
                    Add Tracker
                  </a>
                </div>
              </div>
            </div>
          </div>

          {/* Active Trackers */}
          <div id="toggle-active" className="">
            <div className="my-4">
              <table className="table table-hover table-bordered mb-3">
                <thead className="thead-dark">
                  <tr>
                    <th scope="col">Actions</th>
                    <th scope="col">Name</th>
                    <th scope="col">Regulation</th>
                    {/* <th scope="col">Occurrence</th> */}
                    <th scope="col">Due On</th>
                    <th scope="col">Assigned</th>
                    <th scope="col">Status</th>
                    {/* <th scope="col">Completer</th>
                    <th scope="col">Date Completed</th>
                    <th scope="col">Description</th> */}
                  </tr>
                </thead>
                <tbody>
                {active.map(item => (
                    <tr key={item.id}>
                      <th scope="row" className="dropdown">
                        <a class="btn" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample"><img src="https://icon.now.sh/chevronDown" width="25" height="25" className="d-inline-block align-top" alt="Open Row" /></a>
                        </th>
                      <td>{item.name}</td>
                      <td>{item.reg}</td>
                      <td>{item.dateDue}</td>
                      <td>{item.assigned}</td>
                      <td>{item.status}</td>
                      <div class="collapse" id="collapseExample">
                        <div class="card card-body">
                          <td>{item.occurrence}</td>
                          <td>{item.completer}</td> */}
                          <td>{item.dateCompleted}</td>
                          <td>{item.description}</td>
                        </div>
                      </div>
                    </tr>
                ))}
                 </tbody>
              </table>
            </div> {/* End Table Section */}
          </div> {/* End Active Toggle */}

        </div> {/* End Container */}
        </>
    ) 
  }
}

export default TrackerGeneralPage;

如果我当前单击下拉箭头,它会将其放在表格的右侧:visual on imgur

如果我移动这个:

<div class="collapse" id="collapseExample">
  <div class="card card-body">
    <td>{item.occurrence}</td>
    <td>{item.completer}</td> */}
    <td>{item.dateCompleted}</td>
    <td>{item.description}</td>
  </div>
</div>

进入下拉/折叠按钮所在的&lt;th&gt;,它只将其全部放入该特定列:visual on imgur

我希望按钮打开表格行正下方的下拉/折叠区域,就好像它已连接一样(将下方的行进一步向下推)并且是表格/行的全宽。

每个按钮也只需要打开特定的行下拉/折叠区域,现在如果我点击任何按钮,它会打开所有行的下拉/折叠区域。

这是如何完成的/这叫什么?

【问题讨论】:

    标签: reactjs twitter-bootstrap bootstrap-4


    【解决方案1】:

    将此作为一个想法。只需将折叠添加到您的 &lt;td&gt; 并覆盖引导程序 &lt;td&gt; 填充;

    您可以传递值以将每一行的数据目标替换为 unique id,例如 fd1fd2 或其他可以避免 collision 的 id 名称你的map 函数。

    /*overwrite bootstrap table padding*/
    .hiddenRow {
        padding: 0 !important;
    }
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    
    <table class="table table-hover">
        <thead>
        <th></th><th>1</th><th>2</th><th>3</th>
        </thead>
        
        <tbody>
            <tr data-toggle="collapse" data-target="#folder1">
            <td class="text-center"><span>+</span></td>
                <td>Cell 1.1</td>
                <td>Cell 1.2</td>
                <td>Cell 1.3</td>
            </tr>
            <tr>
                <td colspan="4" class="hiddenRow">
                    <div id='folder1' class="collapse">
                       row1
                    </div>
                </td>
            </tr>
             <tr data-toggle="collapse" data-target="#folder2">
                <td class="text-center"><span>+</span></td>
                <td>Cell 2.1</td>
                <td>Cell 2.2</td>
                <td>Cell 2.3</td>
            </tr>
            <tr style="background:lightyellow">
                <td colspan="4" class="hiddenRow">
                    <div id="folder2" class="collapse">
                    row2
                    </div>
                </td>
            </tr>
        </tbody>
    </table>

    【讨论】:

    • 太棒了!除了多个项目的href外,这可以按预期工作。我尝试使用 {item.id} 但 href 不起作用:href="#{item.id}"href={item.id}。关于如何让每一行都有不同的 ID 来切换有什么想法吗?我在第二个&lt;tr&gt; 中更新了 ID,以匹配:id={item.id}
    • @Matthew 你的身份证是数字开头的吗?如果是这样,那可能就是问题所在。像data-target="#123string" id="3245"id="123string" 可能行不通。所以你可能会做类似data-target="#somechar{item.id}"
    • @Yunahi,试过了,没能成功。我只能让它工作:按钮:href="#data" 和隐藏行:id="data"。尝试了以下操作:按钮:href="#data{item.id}" 和隐藏行:id="data{item.id}" / 按钮:data-target="#data{item.id}" 和隐藏行:id="data{item.id}"
    • @Matthew 我相信您必须仔细阅读一些 reactjs 帖子,了解如何通过字符串连接正确地将唯一的 {item.id} 传递给 id 属性,因为某些框架或语言允许您执行id="data"+{item.id} 之类的操作,以传递您的{item.id}id='{item.id}' 可能会引起混淆。就像item.id = 123 一样,编译器中的 id='{item.id}' 可能会被解析为 id='{item.id}'{item.id} 这里是一个字符串 intsead 变量)而不是 id='123'
    • 正确代码:href={"#a" + item.id} and id={"a" + item.id}
    猜你喜欢
    • 2017-11-05
    • 2019-01-06
    • 2019-10-24
    • 1970-01-01
    • 2021-10-16
    • 2020-02-03
    • 2018-11-30
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多