【问题标题】:Tablesorter scroller's height remains same after triggering update触发更新后,Tablesorter 滚动条的高度保持不变
【发布时间】:2014-10-16 03:36:04
【问题描述】:

当我使用滚动小部件并更新表中的数据时,滚动元素的高度 - $(".tablesorter-scroller .tablesorter-scroller-table").height() - 保持不变。我想要做的是让滚动条的高度是动态的,这样一个有 50 行的表将增长到最大 height(由 widgetOptions.scroller_height 定义),一个有 2 行的表会缩小。这样,行下方就没有巨大的空白区域。

现在,我只是将生成的内联样式属性 height 更改为 max-height。像这样:

$(table).tablesorter(tableOptions);
//Change the generated "height" inline-style to "max-height"
$("#tableContainer .tablesorter-scroller .tablesorter-scroller-table").css("height",""); //this removes "height" from inline style
$("#tableContainer .tablesorter-scroller .tablesorter-scroller-table").css("max-height",CONFIG.clipTableHeight); //aka, widgetOptions.scroller_height when setting table

有更好或更正确的方法吗?我在触发更新后尝试使用以下内容:

// Trigger update
$("#clipsTable").trigger("update");
// Reset everything
$("#clipsTable").trigger("resetToLoadState");
$("#clipsTable").trigger("refreshWidgets",false,true);

感谢莫蒂的帮助。我的最终初始化块如下所示:

$(function(){
  /* Code to calculate value of CONFIG.clipTableHeight */
  /* ... */
  CONFIG.clipTableHeight = 140;
  
  var tableOptions = {
    headers: {
      0: { sorter: false },
      1: { sorter: false },
      2: { sorter: false }
    },
    sortList: [[0,0]],
    textExtraction: {
      0: function(node){
        return $(node).find("input[name='rank']").val();
      }
    },
    widthFixed: true,
    widgets: ['scroller'],
    widgetOptions: {
      scroller_height: CONFIG.clipTableHeight,
      scroller_jumpToHeader: true
    },
    initialized: function(){
      $(".tablesorter-scroller-table").css({
        height: '',
        'max-height': CONFIG.clipTableHeight + 'px'
      });
    }
  };
  
  $("#myTable").tablesorter(tableOptions);
  
});

【问题讨论】:

    标签: javascript jquery tablesorter


    【解决方案1】:

    您需要使用 initialized 回调来调整可滚动 div 的高度 (demo)

    $(function () {
    
        var $table = $('table'),
            updateScroller = function (height) {
                $('.tablesorter-scroller-table').css({
                    height: '',
                    'max-height': height + 'px'
                });
            };
    
        $table.tablesorter({
            theme: 'blue',
            widthFixed: true,
            widgets: ["zebra", "scroller"],
            widgetOptions: {
                scroller_height: 300,
                scroller_upAfterSort: false,
                scroller_jumpToHeader: false
            },
            initialized: function(){
                updateScroller( 300 );
            }
        });
    
        $('.update').on('click', function () {
            updateScroller( $('input').val() );
        });
    
    });
    

    【讨论】:

    • 由于我不需要重新使用 updateScroller 函数或让高度可变,我最终只是将逻辑转储到初始化中。再次感谢您的帮助,在浏览您的文档时完全忽略了回调函数!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2013-10-09
    • 2011-09-28
    • 2011-12-21
    相关资源
    最近更新 更多