使用react-bootstrap扩展表内的行

时间:2016-06-07 21:49:02

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

我正在尝试使用react-bootsrap做出类似的反应:http://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>

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

4 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

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

也许你没有加载JQuery?

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我将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>