如何使 Tabulator table 适合固定高度和宽度的 div?

How to fit a Tabulator table to a div of fixed height and width?

我正在尝试在具有固定高度和宽度的 div 中创建制表符 table。

但是,如果 columns/rows 的数据不足以填满 div 的整个宽度或高度,制表符 table 仍会占据整个 height/width, free中只填充灰色space.

我怎样才能摆脱这个灰色地带?请注意,我不想调整行或列的大小。

 function create_table() {
    let table = new Tabulator("#table", {
        data: [
            {name: 'John', surname: 'Miller'},
            {name: 'Mike', surname: 'Tyson'},                        
        ],
        columns:[
            {title: 'Name', field: 'name'},
            {title: 'Surname', field: 'surname'},
        ]
    })               
  }
                
 create_table()
#table {
  height: 200px;
  width: 200px;
}
<!DOCTYPE HTML>
    <html>
        <head>
            <!-- Tabulator -->
            <link href="https://unpkg.com/tabulator-tables@4.2.7/dist/css/tabulator.min.css" rel="stylesheet">
            <script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.2.7/dist/js/tabulator.min.js"></script>
        </head>
        <body>
            <div id="table"></div>
        </body>
    </html>

我已经将 height 更改为 max-height。这能解决您的问题吗?无论行多少,table的高度都不会超过200px

还添加了 display: inline-blockmax-width: 200px 来调整包含 div 的宽度。一旦 table 宽度 and/or 高度达到包含 div.

中定义的最大值,您就可以使 table 在任何方向或两个方向上滚动

 function create_table() {
    let table = new Tabulator("#table", {
        data: [
            {name: 'John', surname: 'Miller'},
            {name: 'Mike', surname: 'Tyson'},                        
        ],
        columns:[
            {title: 'Name', field: 'name'},
            {title: 'Surname', field: 'surname'},
        ]
    })               
  }
                
 create_table()
#table {
  display: inline-block;
  max-height: 200px;
  max-width: 200px;
}
<!DOCTYPE HTML>
    <html>
        <head>
            <!-- Tabulator -->
            <link href="https://unpkg.com/tabulator-tables@4.2.7/dist/css/tabulator.min.css" rel="stylesheet">
            <script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.2.7/dist/js/tabulator.min.js"></script>
        </head>
        <body>
            <div id="table"></div>
        </body>
    </html>

ar selectedCount = function() {
  var selectedRows = $("#example-table").tabulator("getSelectedRows").length;
  $('#rowCount').text(selectedRows);
}

$("#example-table").tabulator({
  height: 205,
  layout: "fitColumns", //fit columns to width of table (optional)
  columns: [ {title: 'Name', field: 'name'},
            {title: 'Surname', field: 'surname'}
  ],
});

var tabledata = [
            {name: 'John', surname: 'Miller'},
            {name: 'Mike', surname: 'Tyson'},                        
        ];

//load sample data into the table
$("#example-table").tabulator("setData", tabledata);


selectedCount();
<div id="example-table"></div>

<p>
  Number of selected rows = <span id="rowCount"></span>
</p>

As per my understanding, Please check the documentation provided to make the 
columns to fit for the parent container http://tabulator.info/examples/4.4?#fittowidth


Please check the working example in the following https://jsfiddle.net/gzx72ukh/

目前您有 table 的两个显示选项之一:

  • 如果您在 table 上指定高度,它将占据该高度并且任何溢出都将得到正确管理并允许滚动,如果没有足够的行来填充 table 它仍然占用相同数量的 space,您会看到 table 背景
  • 如果您没有在 table 上指定高度,它假定您希望它占据与行数一样多的高度,并且 table 的外部 div =] 将处理滚动

所以在您的场景中,您可以考虑更改背景颜色以使其更适合,但没有办法隐藏额外的 space。

在即将推出的 4.6 版(2020 年初)中,将有一种新的显示模式,它是两者的混合体,它允许 table 展开直到溢出,然后处理内部滚动table