简体   繁体   English

Rails jQuery dataTables页面上的多个表不起作用

[英]Rails jQuery dataTables multiple tables on a page not working

I've read the documentation about using dataTable concerning multiple tables on a page. 我已经阅读了有关使用dataTable的文档,该文档涉及页面上的多个表。 So, it should work. 因此,它应该工作。

I'm using Bootstrap with each table in a tab. 我在选项卡中的每个表上使用Bootstrap。 Each table displays some dataTable controls. 每个表都显示一些dataTable控件。

The first table shows tabletools for xls and pdf. 第一个表显示了用于xls和pdf的表格工具。 But, the 2nd, 3rd, nth tabs show all of the tabletools output (xls, pdf, copy, cdv, print, etc). 但是,第二,第三,第n个选项卡显示所有tabletools输出(xls,pdf,copy,cdv,print等)。 And none of the output types work except print. 除打印外,所有输出类型均不起作用。

Here is my jQuery: 这是我的jQuery:

$(document).ready(function () {

$('.dataTable').dataTable({
    "sDom": 'CT<"clear">lfrtip',
    "oTableTools": {
        "aButtons": [
            {
                "sExtends": "xls",
                "sButtonText": "Excel",
                "mColumns": "visible"
            },
            {
                "sExtends": "pdf",
                "sPdfOrientation": "landscape",
                "sPdfMessage": "Endeavor",
                "mColumns": "visible"
            }
        ],
        "sSwfPath": "/swf/copy_cvs_xls_pdf.swf"
    },
    "oColVis": {
        "buttonText": "Show/Hide",
        "aiExclude": [ 0 ],
        "bRestore": true,
        "sAlign": "left"
    },
    "bStateSave": true,
    "fnStateSave": function (oSettings, oData) {
        localStorage.setItem( 'DataTables_'+window.location.pathname, JSON.stringify(oData) );
    },
    "fnStateLoad": function (oSettings) {
        return JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname) );
    }
});

Here is the html table elements for the first 2 tables: 这是前2个表格的html表格元素:

<table class="display dataTable table table-striped table-bordered" id="dataTable1" style="" aria-describedby="dataTable1_info">
<table class="display dataTable table table-striped table-bordered" id="dataTable2" style="" aria-describedby="dataTable2_info">

Any help would be appreciated!! 任何帮助,将不胜感激!!

Problem is due to hidden elements having no dimensions. 问题是由于隐藏的元素没有尺寸。 If you are using auto size for datatable then when it checks the dimensions of parent , the hidden tab parents have no width and you are seeing irregularities in the rendering as a result 如果您对数据表使用自动调整大小,则当它检查parent的尺寸时,隐藏的选项卡父母没有宽度,因此您会看到渲染不规则

You could try several things: 您可以尝试以下几种方法:

  1. Set width in css and in datatable options OR 在CSS和数据表选项中设置宽度,或者
  2. Only initialize tables when tabs are selected and visible. 仅在选项卡被选中并可见时才初始化表。 (tabs shown event) (标签shown事件)
  3. Adjust tabs content elements css to be not hidden but placed offscreen so they still have dimensions 将标签内容元素css调整为不隐藏但置于屏幕外,以便它们仍具有尺寸

The last is easy to do with jQueryUI tabs and is common practice to resolve rendering width dependent widgets like google maps. 最后一个易于使用jQueryUI选项卡,并且是解决与渲染宽度相关的窗口小部件(如Google地图)的常用方法。 I'm not sure how bootstrap manages tabs content elements display 我不确定引导程序如何管理选项卡内容元素显示

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

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