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

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

    Select the contents of the row like this:

    $(row).contents().slideDown();
    

    .contents() - Get the children of each element in the set of matched elements, including text and comment nodes.

    0 讨论(0)
  • 2020-11-22 09:52
    function hideTr(tr) {
      tr.find('td').wrapInner('<div style="display: block;" />').parent().find('td > div').slideUp(50, function () {
        tr.hide();
        var $set = jQuery(this);
        $set.replaceWith($set.contents());
      });
    }
    
    function showTr(tr) {
      tr.show()
      tr.find('td').wrapInner('<div style="display: none;" />').parent().find('td > div').slideDown(50, function() {
        var $set = jQuery(this);
        $set.replaceWith($set.contents());
      });
    }
    

    you can use these methods like:

    jQuery("[data-toggle-tr-trigger]").click(function() {
      var $tr = jQuery(this).parents(trClass).nextUntil(trClass);
      if($tr.is(':hidden')) {
        showTr($tr);
      } else {
        hideTr($tr);
      }
    });
    
    0 讨论(0)
  • 2020-11-22 09:58

    I'd like to add a comment to #Vinny's post but dont have the rep so have to post an answer...

    Found a bug with your plugin - when you just pass an object in with arguments they get overwritten if no other arguments are passed in. Easily resolved by changing the order the arguments are processed, code below. I've also added an option for destroying the row after closing (only as I had a need for it!): $('#row_id').slideRow('up', true); // destroys the row

    (function ($) {
        var sR = {
            defaults: {
                slideSpeed: 400,
                easing: false,
                callback: false
            },
            thisCallArgs: {
                slideSpeed: 400,
                easing: false,
                callback: false,
                destroyAfterUp: false
            },
            methods: {
                up: function (arg1, arg2, arg3) {
                    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;
                    }
                    if (typeof arg1 == 'object') {
                        for (p in arg1) {
                            sR.thisCallArgs[p] = arg1[p];
                        }
                    } else if (typeof arg1 != 'undefined' && (typeof arg1 == 'number' || arg1 == 'slow' || arg1 == 'fast')) {
                        sR.thisCallArgs.slideSpeed = arg1;
                    } else if (typeof arg1 != 'undefined' && (typeof arg1 == 'boolean')) {
                        sR.thisCallArgs.destroyAfterUp = arg1;
                    } else {
                        sR.thisCallArgs.slideSpeed = sR.defaults.slideSpeed;
                    }
    
                    var $row = $(this);
                    var $cells = $row.children('th, 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 (sR.thisCallArgs.destroyAfterUp)
                            {
                                $row.replaceWith('');
                            }
                            if (typeof sR.thisCallArgs.callback == 'function') {
                                sR.thisCallArgs.callback.call(this);
                            }
                        }
                    }, 100);
                    return $(this);
                },
                down: function (arg1, arg2, arg3) {
    
                    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;
                    }
                    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;
                    }
    
                    var $cells = $(this).children('th, 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);
    
    0 讨论(0)
提交回复
热议问题