制表符 table 无法正确查看

Tabulator table not view properly

我有一个按钮,当单击按钮时会显示 table(制表符),但问题是当单击按钮时,table 无法正确显示,table 是视图,但是内容未显示,我需要触发操作,如调整大小 window 或按 F12 以显示内容。

点击按钮时,不显示任何内容

当我按 F12 时,内容视图但是缺少 header(红线)

当我尝试在 jsfiddle 中进行测试时,显示了 header 但没有显示内容(需要调整大小 window 才能显示数据)

我尝试过的是这样的:

$(document).on('click', '#testing', function(){
 $('#example-table').show();
});

//define some sample data
 var tabledata = [
  {id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
  {id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
  {id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
  {id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
  {id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
 ];
 
 //create Tabulator on DOM element with id "example-table"
var table = new Tabulator("#example-table", {
  height:205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
  data:tabledata, //assign data to table
  layout:"fitColumns", //fit columns to width of table (optional)
  columns:[ //Define Table Columns
   {title:"Name", field:"name", width:150},
   {title:"Age", field:"age", align:"left", formatter:"progress"},
   {title:"Favourite Color", field:"col"},
   {title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
  ],
  rowClick:function(e, row){ //trigger an alert message when the row is clicked
   alert("Row " + row.getData().id + " Clicked!!!!");
  },
});
<link href="https://unpkg.com/tabulator-tables@4.1.4/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.1.4/dist/js/tabulator.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<button id='testing'>Click this button !!</button>
<div id="example-table" style='display:none'></div>

你也可以检查 jsfiddle,检查 here

谁能告诉我为什么会这样?

这可能是因为您创建 table 时它是隐藏的。 Tabulator 在绘制时需要可见,因为 Virtual DOM 需要计算组成 table 的元素的长度才能正确布局 table。 DOM 元素在首次显示之前没有尺寸,这可能会导致 table.

中的某些损坏

最简单的方法是在 table 可见后立即调用重绘函数。

table.redraw(true);

Tabulator 网站上的 Quickstart Guide 中详细介绍了该主题。

它在 Chrome 中没有发生的原因是因为 chrome 实现了 ES8 ResizeObserver 功能,制表符可以使用该功能来确定 table 何时改变形状或变得可见。此功能在其他浏览器中尚不可用。

我猜 table.redraw(true); 适用于 tabulator 4.0 及以上版本,如果你有较低版本的 tabulator 使用 table.redraw();。 我也遇到过,在升级到版本 4 之前我使用了 tableRefernc.redraw() 方法并且效果很好,但是在升级到版本 4 之后效果不佳,所以我将其更改为 tableRefernc.redraw(true) 并且它效果很好。