HTML / Javascript - Expand and collapse table rows (childs) by clicking on a parent row

后端 未结 1 1434
悲&欢浪女
悲&欢浪女 2021-02-04 13:13

I am trying to solve one issue since days and finally understood that I will not succeed without help ... I want to do a common thing that we see on internet everyday : be able

相关标签:
1条回答
  • 2021-02-04 13:53

    You will need to wrap each group of parent + children in a <tbody> for this and use a small script to toggle a class name on this parent <tbody>. Here's an example:

    $('table').on('click', 'tr.parent .fa-chevron-down', function(){
      $(this).closest('tbody').toggleClass('open');
    });
    .parent ~ .cchild {
      display: none;
    }
    .open .parent ~ .cchild {
      display: table-row;
    }
    .parent {
      cursor: pointer;
    }
    tbody {
      color: #212121;
    }
    .open {
      background-color: #e6e6e6;
    }
    
    .open .cchild {
      background-color: #999;
      color: white;
    }
    .parent > *:last-child {
      width: 30px;
    }
    .parent i {
      transform: rotate(0deg);
      transition: transform .3s cubic-bezier(.4,0,.2,1);
      margin: -.5rem;
      padding: .5rem;
     
    }
    .open .parent i {
      transform: rotate(180deg)
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    
    <div class="container">
        <table class="table">
            <tr>
                <th>Age</th>
                <th>Sex</th>
                <th>Name</th>
                <th colspan="2">From</th>
            </tr>
    
            <tbody>
            <tr class="parent">
                <td>100</td>
                <td>M</td>
                <td>Dodo</td>
                <td>UK</td>
                <td><i class="fa fa-chevron-down"></i></td>
            </tr>
            <tr class="cchild">
                <td>10</td>
                <td>M</td>
                <td>Child</td>
                <td colspan="2">UK</td>
            </tr>
            <tr class="cchild">
                <td>10</td>
                <td>M</td>
                <td>Child</td>
                <td colspan="2">UK</td>
            </tr>
            <tr class="cchild">
                <td>10</td>
                <td>M</td>
                <td>Child</td>
                <td colspan="2">UK</td>
            </tr>
            </tbody>
            <tbody>
            <tr class="parent">
                <td>100</td>
                <td>M</td>
                <td>Dodo</td>
                <td>UK</td>
                <td><i class="fa fa-chevron-down"></i></td>
            </tr>
            <tr class="cchild">
                <td>10</td>
                <td>M</td>
                <td>Child</td>
                <td colspan="2">UK</td>
            </tr>
            <tr class="cchild">
                <td>10</td>
                <td>M</td>
                <td>Child</td>
                <td colspan="2">UK</td>
            </tr>
            <tr class="cchild">
                <td>10</td>
                <td>M</td>
                <td>Child</td>
                <td colspan="2">UK</td>
            </tr>
            </tbody>
        </table>
    </div>

    0 讨论(0)
提交回复
热议问题