How to Use slideDown (or show) function on a table row?

后端 未结 21 2125
清歌不尽
清歌不尽 2020-11-22 09:03

I\'m trying to add a row to a table and have that row slide into view, however the slidedown function seems to be adding a display:block style to the table row which messes

相关标签:
21条回答
  • 2020-11-22 09:32

    You could try wrapping the contents of the row in a <span> and having your selector be $('#detailed_edit_row span'); - a bit hackish, but I just tested it and it works. I also tried the table-row suggestion above and it didn't seem to work.

    update: I've been playing around with this problem, and from all indications jQuery needs the object it performs slideDown on to be a block element. So, no dice. I was able to conjure up a table where I used slideDown on a cell and it didn't affect the layout at all, so I am not sure how yours is set up. I think your only solution is to refactor the table in such a way that it's ok with that cell being a block, or just .show(); the damn thing. Good luck.

    0 讨论(0)
  • 2020-11-22 09:32

    I'm a bit behind the times on answering this, but I found a way to do it :)

    function eventinfo(id) {
        tr = document.getElementById("ei"+id);
        div = document.getElementById("d"+id);
        if (tr.style.display == "none") {
            tr.style.display="table-row";
            $(div).slideDown('fast');
        } else {
            $(div).slideUp('fast');
            setTimeout(function(){tr.style.display="none";}, 200);
        }
    }
    

    I just put a div element inside the table data tags. when it is set visible, as the div expands, the whole row comes down. then tell it to fade back up (then timeout so you see the effect) before hiding the table row again :)

    Hope this helps someone!

    0 讨论(0)
  • 2020-11-22 09:34

    I neded a table with hidden rows that slide in and out of view on row click.

    $('.tr-show-sub').click(function(e) {  
        var elOne = $(this);
        $('.tr-show-sub').each(function(key, value) {
            var elTwoe = $(this);
            
            if(elOne.get(0) !== elTwoe.get(0)) {
                if($(this).next('.tr-sub').hasClass('tr-sub-shown')) {
                    elTwoe.next('.tr-sub').removeClass('tr-sub-shown');
                    elTwoe.next('tr').find('td').find('div').slideUp();
                    elTwoe.next('tr').find('td').slideUp();
                }
            }
            
            if(elOne.get(0) === elTwoe.get(0)) {
                if(elOne.next('.tr-sub').hasClass('tr-sub-shown')) {
                    elOne.next('.tr-sub').removeClass('tr-sub-shown');
                    elOne.next('tr').find('td').find('div').slideUp();
                    elOne.next('tr').find('td').slideUp();
                } else {
                    elOne.next('.tr-sub').addClass('tr-sub-shown');
                    elOne.next('tr').find('td').slideDown();
                    elOne.next('tr').find('td').find('div').slideDown();
                }
            }
        })
    });
    body {
            background: #eee;
        }
        
        .wrapper {
            margin: auto;
            width: 50%;
            padding: 10px;
            margin-top: 10%;
        }
        
        table {
            background: white;
            width: 100%;
        }
        
        table th {
            background: gray;
            text-align: left;
        }
        
        table th, td {
            border-bottom: 1px solid lightgray;
            padding: 5px;
        }
        
        table .tr-show-sub {
            background: #EAEAEA;
            cursor: pointer;
        }
    
        table .tr-sub td {
            display: none;
        }
        
        table .tr-sub td .div-sub {
            display: none;
        }
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <div class="wrapper">
        <table cellspacing="0" cellpadding="3">
            <thead>
                <tr class="table">
                    <th>col 1</th>
                    <th>col 2</th>
                    <th>col 3</th>
                </tr>
            </thead>
            <tbody>
                <tr class="tr-show-sub">
                    <td>col 1</td>
                    <td>col 2</td>
                    <td>col 3</td>
                </tr>
                <tr class="tr-sub">
                    <td colspan="5"><div class="div-sub">
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis auctor tortor sit amet sem tempus rhoncus. Etiam scelerisque ligula id ligula congue semper interdum in neque. Vestibulum condimentum id nibh ac pretium. Proin a dapibus nibh. Suspendisse quis elit volutpat, aliquet nisi et, rhoncus quam. Quisque nec ex quis diam tristique hendrerit. Nullam sagittis metus sem, placerat scelerisque dolor congue eu. Pellentesque ultricies purus turpis, convallis congue felis iaculis sed. Cras semper elementum nibh at semper. Suspendisse libero augue, auctor facilisis tincidunt eget, suscipit eu ligula. Nam in diam at ex facilisis tincidunt. Fusce erat enim, placerat ac massa facilisis, tempus aliquet metus. Fusce placerat nulla sed tristique tincidunt. Duis vulputate vestibulum libero, nec lobortis elit ornare vel. Mauris imperdiet nulla non suscipit cursus. Sed sed dui ac elit rutrum mollis sed sit amet lorem. 
                    </div></td>
                </tr>
                <tr class="tr-show-sub">
                    <td>col 1</td>
                    <td>col 2</td>
                    <td>col 3</td>
                </tr>
                <tr class="tr-sub">
                    <td colspan="5"><div class="div-sub">
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis auctor tortor sit amet sem tempus rhoncus. Etiam scelerisque ligula id ligula congue semper interdum in neque. Vestibulum condimentum id nibh ac pretium. Proin a dapibus nibh. Suspendisse quis elit volutpat, aliquet nisi et, rhoncus quam. Quisque nec ex quis diam tristique hendrerit. Nullam sagittis metus sem, placerat scelerisque dolor congue eu. Pellentesque ultricies purus turpis, convallis congue felis iaculis sed. Cras semper elementum nibh at semper. Suspendisse libero augue, auctor facilisis tincidunt eget, suscipit eu ligula. Nam in diam at ex facilisis tincidunt. Fusce erat enim, placerat ac massa facilisis, tempus aliquet metus. Fusce placerat nulla sed tristique tincidunt. Duis vulputate vestibulum libero, nec lobortis elit ornare vel. Mauris imperdiet nulla non suscipit cursus. Sed sed dui ac elit rutrum mollis sed sit amet lorem. 
                    </div></td>
                </tr>
                <tr>
                    <td>col 1</td>
                    <td>col 2</td>
                    <td>col 3</td>
                </tr>
            </tbody>
        </table>
    </div>

    0 讨论(0)
  • 2020-11-22 09:34

    This code works!

    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <title></title>
            <script>
                function addRow() {
                    $('.displaynone').show();
                    $('.displaynone td')
                    .wrapInner('<div class="innerDiv" style="height:0" />');
                    $('div').animate({"height":"20px"});
                }
            </script>
            <style>
                .mycolumn{border: 1px solid black;}
                .displaynone{display: none;}
            </style>
        </head>
        <body>
            <table align="center" width="50%">
                <tr>
                    <td class="mycolumn">Row 1</td>
                </tr>
                <tr>
                    <td class="mycolumn">Row 2</td>
                </tr>
                <tr class="displaynone">
                    <td class="mycolumn">Row 3</td>
                </tr>
                <tr>
                    <td class="mycolumn">Row 4</td>
                </tr>
            </table>
            <br>
            <button onclick="addRow();">add</button>    
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 09:34

    Quick/Easy Fix:

    Use JQuery .toggle() to show/hide the rows onclick of either Row or an Anchor.

    A function will need to be written to identify the rows you would like hidden, but toggle creates the functionality you are looking for.

    0 讨论(0)
  • 2020-11-22 09:39

    I simply wrap the tr dynamically then remove it once the slideUp/slideDown has complete. It's a pretty small overhead adding and removing one or a couple of tags and then removing them once the animation is complete, I don't see any visible lag at all doing it.

    SlideUp:

    $('#my_table > tbody > tr.my_row')
     .find('td')
     .wrapInner('<div style="display: block;" />')
     .parent()
     .find('td > div')
     .slideUp(700, function(){
    
      $(this).parent().parent().remove();
    
     });
    

    SlideDown:

    $('#my_table > tbody > tr.my_row')
     .find('td')
     .wrapInner('<div style="display: none;" />')
     .parent()
     .find('td > div')
     .slideDown(700, function(){
    
      var $set = $(this);
      $set.replaceWith($set.contents());
    
     });
    

    I have to pay tribute to fletchzone.com as I took his plugin and stripped it back to the above, cheers mate.

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