[英]Tabulator table not view properly
單擊按鈕時我有一個按鈕將顯示表格(制表符)但問題是單擊按鈕時表格顯示不正確,表格是視圖但內容未顯示,我需要觸發操作,例如調整窗口大小或按 F12來顯示內容。
當我嘗試在 jsfiddle 中測試時,顯示標題但不顯示內容(需要調整窗口大小以顯示數據)
我的嘗試是這樣的:
$(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 中查看,查看這里
有人能告訴我為什么會這樣嗎?
這可能是因為您在隱藏表格時創建了表格。 Tabulator 在繪制時需要可見,因為 Virtual DOM 需要計算組成表格的元素的長度才能正確布局表格。 DOM 元素在第一次變得可見之前沒有維度,這可能導致表中的某些損壞。
解決此問題的最簡單方法是在表可見后立即在表上調用重繪函數。
table.redraw(true);
此主題在 Tabulator 網站上的快速入門指南中有詳細介紹。
它在 Chrome 中沒有發生的原因是因為 chrome 實現了 ES8 ResizeObserver 功能,制表符可以使用該功能來確定表格何時改變形狀或變得可見。 此功能在其他瀏覽器中尚不可用。
我猜table.redraw(true);
適用於制表符 4.0 及更高版本,如果您有較低版本的制表符,請使用table.redraw();
. 我也發生過這種情況,在升級到版本 4 之前我使用了tableRefernc.redraw()
方法並且它運行良好,但是在升級到版本 4 后它運行不佳,所以我將其更改為tableRefernc.redraw(true)
和它運作良好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.