简体   繁体   English

如何正确居中手持台

[英]How to center a handsontable properly

I am using handsontable and I would like to show a table centered in the page or a div.我正在使用 handsontable,我想显示一个以页面或 div 为中心的表格。

I have managed to do this by changing the width of the container div after the table is initiated (see snippet).我设法通过在表启动后更改容器 div 的宽度来做到这一点(请参阅代码段)。 However this seems cumbersome.不过这看起来很麻烦。

 $(document).ready(function () { var container = document.getElementById('basic_example'); var hot = new Handsontable(container, { data: Handsontable.helper.createSpreadsheetData(10, 12), colHeaders: true, rowHeaders: true, afterInit: function () { $("#basic_example").css("width", $("#basic_example .wtHider").css("width")) } }); });
 #basic_example { margin-left: auto; margin-right: auto; }
 <link href="http://handsontable.com//styles/main.css" rel="stylesheet"> <link href="http://handsontable.com//bower_components/handsontable/dist/handsontable.full.min.css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://handsontable.com//bower_components/handsontable/dist/handsontable.full.min.js"></script> <div id="basic_example"></div>

Is there an easy/better way of centering a handsontable?有没有一种简单/更好的方法来使手持台居中?

Using your work-around, I would recommend using afterRender instead of afterInit in order to update the size of your table whenever the handsontable is rendered.使用您的变通方法,我建议使用 afterRender 而不是 afterInit 以便在呈现 handsontable 时更新表格的大小。

This would be especially useful if the user changes the size of the screen, if you allow the addition of columns or rows, or if there are other possibilities of the table changing sizes after the original initialization.如果用户更改屏幕的大小,如果您允许添加列或行,或者如果在原始初始化之后表格更改大小的其他可能性,这将特别有用。

Based on the earlier answers, I used the afterRender (and afterChange) events as follows:根据之前的答案,我使用了 afterRender(和 afterChange)事件,如下所示:

  afterRender: function () {
    let wtHider = document.querySelector('#tableContainer .wtHider');
    container.style.width = wtHider.style.width
  },

However, when you hide a column, it seems that afterRender is fired too early, before the element with the wtHider class is updated.但是,当您隐藏一列时,似乎在更新具有 wtHider 类的元素之前 afterRender 被触发得太早了。

Adding a delay of 0 ms solves this issue:添加 0 ms 的延迟可解决此问题:

   afterRender: function () {
    let wtHider = document.querySelector('#tableContainer .wtHider');
    setTimeout(() => {
      container.style.width = wtHider.style.width
    }, 0);
  },

Note: Tested using chromium version 87.0.4280.66, Ubuntu 20.04 and handsontable 8.2.0注意:使用 Chromium 版本 87.0.4280.66、Ubuntu 20.04 和 handsontable 8.2.0 进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM