ilbonte
ilbonte

Reputation: 151

Expanding row inside a Table using react-bootstrap

I'm trying to achieve something like this in react using react-bootsrap: http://www.bootply.com/T0v2NlXryW

My react code now is this, but is not working:

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

The table is displayed correctly but the rows does not expand on click.

Upvotes: 6

Views: 18137

Answers (4)

Ihor
Ihor

Reputation: 324

Now it came much easier, since Bootstrap implemented collapse functionality. Look here

Upvotes: 0

Nima Kaviyani
Nima Kaviyani

Reputation: 113

I adapted C. Draghici's answer to a new version of react and 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>

Upvotes: 1

C. Draghici
C. Draghici

Reputation: 187

<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");
}

Upvotes: 3

frogbandit
frogbandit

Reputation: 571

Your code is working for me - the hidden row is toggled on click. Check out this Bootply: http://www.bootply.com/QZPe6DSp1F.

Perhaps you aren't loading JQuery?

Upvotes: 1

Related Questions