简体   繁体   English

数据表无法通过JQuery-UI选项卡正确工作

[英]Datatables not correctly working through JQuery-UI Tabs

I seem to have a problem with the way jQuery-UI tabs and Datatables work together. 我似乎对jQuery-UI选项卡和数据表协同工作的方式有疑问。 I want to create a table that is scrollable and has information from a database. 我想创建一个可滚动的表,并具有来自数据库的信息。

When I enter the page for the first time it looks like this . 当我第一次进入页面时,它看起来像这样
As you can see it looks like a normal table instead of one from Datatables. 如您所见,它看起来像一个普通表,而不是数据表中的一个。
When I reload the page again it looks like this . 当我再次重新加载页面时,它看起来像这样
This is roughly how I want it to look upon first load. 这大致就是我希望它在首次加载时的样子。

It also seems that it only works for the first table on the page. 似乎它仅适用于页面上的第一个表。

I tested this multiple times on multiple pages with the same results. 我在多个页面上对此结果进行了多次测试。

I'm assuming this problem is because of jQuery-UI seeing as on another page it work perfectly and when I load in the page without jQuery-UI it works aswell. 我以为这个问题是由于jQuery-UI在另一页上看到它工作正常而当我在没有jQuery-UI的页面上加载时它也能正常工作。

This is the code: 这是代码:

//jQuery-UI
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
//DataTables
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

    <script>
    $(function() {
        $('table.scrollable').DataTable({
          "scrollY": "240px",
          "scrollX": true,
          "scrollCollapse": true,
          "paging": false
        });
    });
        </script>

The HTML is just a standard table loaded in through a while loop from a database. HTML只是通过数据库的while循环加载的标准表。

To decrease loading times I've set up jQuery-UI in a way that it loads seperate files when a tab is clicked so that no unnecessary things are loaded at the start. 为了减少加载时间,我设置了jQuery-UI,使其在单击选项卡时加载单独的文件,这样一开始就不会加载不必要的内容。

Any piece of help would be much appreciated thanks! 任何帮助将不胜感激,谢谢!

EDIT: Wanted to make a JSFiddle showcasing my problems but it basically works perfect there so I'm guessing the problem comes from the fact that I load it as seperate pages. 编辑:想要使JSFiddle展示我的问题,但它在那里基本上可以完美地工作,所以我猜问题出在我将其作为单独的页面加载的事实。
JSFiddle 的jsfiddle

I would advise the following code, expanding upon my comment. 我会建议以下代码,以我的评论为准。

$(function() {
  $("#loading-voertuigen").show();

  if ($(window).width() <= 1600) {
    $('table.scrollable').DataTable({
      "scrollY": "240px",
      "scrollX": true,
      "scrollCollapse": true,
      "paging": false
    });
  } else {
    $('table.scrollable').DataTable({
      "scrollY": "240px",
      "scrollCollapse": true,
      "paging": false
    });
  }

  $(window).resize(function() {
    location.reload();
  });
});

You were using $(document).ready() and it is not needed. 您正在使用$(document).ready() ,不需要它。 The use of $(function(){}) already causes the jQuery to execute after the page is ready . $(function(){})已经使jQuery在页面ready后执行。

If need further help, please edit your post and include a more complete example. 如果需要进一步的帮助,请编辑您的帖子,并提供更完整的示例。

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

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