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

后端 未结 21 2126
清歌不尽
清歌不尽 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:47

    Here's a plug-in that I wrote up for this, it takes a little from Fletch's implementation, but mine is used solely to slide a row up or down (no inserting rows).

    (function($) {
    var sR = {
        defaults: {
            slideSpeed: 400,
            easing: false,
            callback: false     
        },
        thisCallArgs: {
            slideSpeed: 400,
            easing: false,
            callback: false
        },
        methods: {
            up: function (arg1,arg2,arg3) {
                if(typeof arg1 == 'object') {
                    for(p in arg1) {
                        sR.thisCallArgs.eval(p) = arg1[p];
                    }
                }else if(typeof arg1 != 'undefined' && (typeof arg1 == 'number' || arg1 == 'slow' || arg1 == 'fast')) {
                    sR.thisCallArgs.slideSpeed = arg1;
                }else{
                    sR.thisCallArgs.slideSpeed = sR.defaults.slideSpeed;
                }
    
                if(typeof arg2 == 'string'){
                    sR.thisCallArgs.easing = arg2;
                }else if(typeof arg2 == 'function'){
                    sR.thisCallArgs.callback = arg2;
                }else if(typeof arg2 == 'undefined') {
                    sR.thisCallArgs.easing = sR.defaults.easing;    
                }
                if(typeof arg3 == 'function') {
                    sR.thisCallArgs.callback = arg3;
                }else if(typeof arg3 == 'undefined' && typeof arg2 != 'function'){
                    sR.thisCallArgs.callback = sR.defaults.callback;    
                }
                var $cells = $(this).find('td');
                $cells.wrapInner('<div class="slideRowUp" />');
                var currentPadding = $cells.css('padding');
                $cellContentWrappers = $(this).find('.slideRowUp');
                $cellContentWrappers.slideUp(sR.thisCallArgs.slideSpeed,sR.thisCallArgs.easing).parent().animate({
                                                                                                                    paddingTop: '0px',
                                                                                                                    paddingBottom: '0px'},{
                                                                                                                    complete: function () {
                                                                                                                        $(this).children('.slideRowUp').replaceWith($(this).children('.slideRowUp').contents());
                                                                                                                        $(this).parent().css({'display':'none'});
                                                                                                                        $(this).css({'padding': currentPadding});
                                                                                                                    }});
                var wait = setInterval(function () {
                    if($cellContentWrappers.is(':animated') === false) {
                        clearInterval(wait);
                        if(typeof sR.thisCallArgs.callback == 'function') {
                            sR.thisCallArgs.callback.call(this);
                        }
                    }
                }, 100);                                                                                                    
                return $(this);
            },
            down: function (arg1,arg2,arg3) {
                if(typeof arg1 == 'object') {
                    for(p in arg1) {
                        sR.thisCallArgs.eval(p) = arg1[p];
                    }
                }else if(typeof arg1 != 'undefined' && (typeof arg1 == 'number' || arg1 == 'slow' || arg1 == 'fast')) {
                    sR.thisCallArgs.slideSpeed = arg1;
                }else{
                    sR.thisCallArgs.slideSpeed = sR.defaults.slideSpeed;
                }
    
                if(typeof arg2 == 'string'){
                    sR.thisCallArgs.easing = arg2;
                }else if(typeof arg2 == 'function'){
                    sR.thisCallArgs.callback = arg2;
                }else if(typeof arg2 == 'undefined') {
                    sR.thisCallArgs.easing = sR.defaults.easing;    
                }
                if(typeof arg3 == 'function') {
                    sR.thisCallArgs.callback = arg3;
                }else if(typeof arg3 == 'undefined' && typeof arg2 != 'function'){
                    sR.thisCallArgs.callback = sR.defaults.callback;    
                }
                var $cells = $(this).find('td');
                $cells.wrapInner('<div class="slideRowDown" style="display:none;" />');
                $cellContentWrappers = $cells.find('.slideRowDown');
                $(this).show();
                $cellContentWrappers.slideDown(sR.thisCallArgs.slideSpeed, sR.thisCallArgs.easing, function() { $(this).replaceWith( $(this).contents()); });
    
                var wait = setInterval(function () {
                    if($cellContentWrappers.is(':animated') === false) {
                        clearInterval(wait);
                        if(typeof sR.thisCallArgs.callback == 'function') {
                            sR.thisCallArgs.callback.call(this);
                        }
                    }
                }, 100);
                return $(this);
            }
        }
    };
    
    $.fn.slideRow = function(method,arg1,arg2,arg3) {
        if(typeof method != 'undefined') {
            if(sR.methods[method]) {
                return sR.methods[method].apply(this, Array.prototype.slice.call(arguments,1));
            }
        }
    };
    })(jQuery);
    

    Basic Usage:

    $('#row_id').slideRow('down');
    $('#row_id').slideRow('up');
    

    Pass slide options as individual arguments:

    $('#row_id').slideRow('down', 500); //slide speed
    $('#row_id').slideRow('down', 500, function() { alert('Row available'); }); // slide speed and callback function
    $('#row_id').slideRow('down', 500, 'linear', function() { alert('Row available'); }); slide speed, easing option and callback function
    $('#row_id').slideRow('down', {slideSpeed: 500, easing: 'linear', callback: function() { alert('Row available');} }); //options passed as object
    

    Basically, for the slide down animation, the plug-in wraps the contents of the cells in DIVs, animates those, then removes them, and vice versa for the slide up (with some extra steps to get rid of the cell padding). It also returns the object you called it on, so you can chain methods like so:

    $('#row_id').slideRow('down').css({'font-color':'#F00'}); //make the text in the row red
    

    Hope this helps someone.

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

    If you need to slide and fade a table row at the same time, try using these:

    jQuery.fn.prepareTableRowForSliding = function() {
        $tr = this;
        $tr.children('td').wrapInner('<div style="display: none;" />');
        return $tr;
    };
    
    jQuery.fn.slideFadeTableRow = function(speed, easing, callback) {
        $tr = this;
        if ($tr.is(':hidden')) {
            $tr.show().find('td > div').animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
        } else {
            $tr.find('td > div').animate({opacity: 'toggle', height: 'toggle'}, speed, easing, function(){
                $tr.hide();
                callback();
            });
        }
        return $tr;
    };
    
    $(document).ready(function(){
        $('tr.special').hide().prepareTableRowForSliding();
        $('a.toggle').toggle(function(){
            $button = $(this);
            $tr = $button.closest('tr.special'); //this will be specific to your situation
            $tr.slideFadeTableRow(300, 'swing', function(){
                $button.text('Hide table row');
            });
        }, function(){
            $button = $(this);
            $tr = $button.closest('tr.special'); //this will be specific to your situation
            $tr.slideFadeTableRow(300, 'swing', function(){
                $button.text('Display table row');
            });
    });
    });
    
    0 讨论(0)
  • 2020-11-22 09:48

    I've gotten around this by using the td elements in the row:

    $(ui.item).children("td").effect("highlight", { color: "#4ca456" }, 1000);
    

    Animating the row itself causes strange behaviour, mostly async animation problems.

    For the above code, I am highlighting a row that gets dragged and dropped around in the table to highlight that the update has succeeded. Hope this helps someone.

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

    http://jsfiddle.net/PvwfK/136/

    <table cellspacing='0' cellpadding='0' class='table01' id='form_table' style='width:100%;'>
    <tr>
        <td style='cursor:pointer; width:20%; text-align:left;' id='header'>
            <label style='cursor:pointer;'> <b id='header01'>▲ Customer Details</b>
    
            </label>
        </td>
    </tr>
    <tr>
        <td style='widtd:20%; text-align:left;'>
            <div id='content' class='content01'>
                <table cellspacing='0' cellpadding='0' id='form_table'>
                    <tr>
                        <td>A/C ID</td>
                        <td>:</td>
                        <td>3000/A01</td>
                    </tr>
                    <tr>
                        <td>A/C ID</td>
                        <td>:</td>
                        <td>3000/A01</td>
                    </tr>
                    <tr>
                        <td>A/C ID</td>
                        <td>:</td>
                        <td>3000/A01</td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    

    $(function () {
    $(".table01 td").on("click", function () {
        var $rows = $('.content01');
        if ($(".content01:first").is(":hidden")) {
            $("#header01").text("▲ Customer Details");
            $(".content01:first").slideDown();
        } else {
            $("#header01").text("▼ Customer Details");
            $(".content01:first").slideUp();
        }
    });
    

    });

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

    Animations are not supported on table rows.

    From "Learning jQuery" by Chaffer and Swedberg


    Table rows present particular obstacles to animation, since browsers use different values (table-row and block) for their visible display property. The .hide() and .show() methods, without animation, are always safe to use with table rows. As of jQuery version 1.1.3, .fadeIn() and .fadeOut() can be used as well.


    You can wrap your td contents in a div and use the slideDown on that. You need to decide if the animation is worth the extra markup.

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

    I want to slide whole tbody and I've managed this problem by combining fade and slide effects.

    I've done this in 3 stages (2nd and 3rd steps are replaced in case you want to slide down or up)

    1. Assigning height to tbody,
    2. Fading all td and th,
    3. Sliding tbody.

    Example of slideUp:

    tbody.css('height', tbody.css('height'));
    tbody.find('td, th').fadeOut(200, function(){
        tbody.slideUp(300)
    });
    
    0 讨论(0)
提交回复
热议问题