【问题标题】:Expanding row inside a Table using react-bootstrap使用 react-bootstrap 扩展表内的行
【发布时间】:2021-12-06 20:36:17
【问题描述】:

我正在尝试使用react-bootsraphttp://www.bootply.com/T0v2NlXryW 实现类似的反应

我现在的反应代码是这样的,但不起作用:

<Table>
<thead>
    <tr>
        <th>Job Name</th>
        <th>Description</th>
        <th>Provider Name</th>
        <th>Region</th>
        <th>Status</th>
    </tr>
</thead>
<tbody>
    <tr data-toggle="collapse" data-target="#demo1" className="accordion-toggle">
        <td>OBS Name</td>
        <td>OBS Description</td>
        <td>hpcloud</td>
        <td>nova</td>
        <td> created</td>
    </tr>
    <tr>
        <td colspan="12" className="hiddenRow">
            <div className="accordian-body collapse" id="demo1">
                <h1>Hi from the hiddenRow</h1>
            </div>
        </td>
    </tr>
</tbody></Table>

表格显示正确,但点击时行未展开。

【问题讨论】:

    标签: twitter-bootstrap reactjs html-table react-bootstrap


    【解决方案1】:
    <table class="table table-condensed" style="border-collapse:collapse;">
        <thead>
            <tr>
                <th>#</th>
                <th>Date</th>
                <th>Description</th>
                <th>Credit</th>
                <th>Debit</th>
                <th>Balance</th>
            </tr>
        </thead>
        <tbody>
            <tr onClick={this.onClickHandler}>
                <td>1</td>
                <td>05 May 2013</td>
                <td>Credit Account</td>
                <td class="text-success">$150.00</td>
                <td class="text-error"></td>
                <td class="text-success">$150.00</td>
            </tr>
            <tr >
                <td colspan="6" class="hiddenRow"><div class="collapse" id="demo1"> Demo Content1 </div> </td>
            </tr>       
        </tbody>
    </table>
    
    private onClickHandler(e: React.MouseEvent<HTMLTableRowElement>) {
      const hiddenElement = e.currentTarget.nextSibling.firstChild.firstChild as HTMLElement;
      hiddenElement.className.indexOf("collapse in") > -1 ? hiddenElement.classList.remove("in") :  hiddenElement.classList.add("in");
    }
    

    【讨论】:

    【解决方案2】:

    您的代码对我有用 - 单击时会切换隐藏行。看看这个 Bootply:http://www.bootply.com/QZPe6DSp1F

    也许您没有加载 JQuery?

    【讨论】:

    • 抱歉,我可能不够清楚,但我正在使用 React 进行此操作
    • Bootstrap 的 javascript 东西依赖于 jQuery
    【解决方案3】:

    我将 C. Draghici 的答案改编为新版本的 react 和 react-bootstrap

    onClickHandler = (e) => {
        const hiddenElement = e.currentTarget.nextSibling;
        hiddenElement.className.indexOf("collapse show") > -1 ? hiddenElement.classList.remove("show") : hiddenElement.classList.add("show");
    };
    
    <Table>
        <thead>
        <tr>
            <th>#</th>
            <th>Date</th>
            <th>Description</th>
            <th>Credit</th>
            <th>Debit</th>
            <th>Balance</th>
        </tr>
        </thead>
        <tbody>
        <tr onClick={this.onClickHandler}>
            <td>1</td>
            <td>05 May 2013</td>
            <td>Credit Account</td>
            <td className="text-success">$150.00</td>
            <td className="text-error" />
            <td className="text-success">$150.00</td>
        </tr>
        <tr className="collapse">
            <td colSpan="6">
                Demo Content1
            </td>
        </tr>
        </tbody>
    </Table>
    

    【讨论】:

      【解决方案4】:

      现在变得容易多了,因为 Bootstrap 实现了折叠功能。看here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-03
        • 2019-07-01
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        • 2017-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多