简体   繁体   English

用不同的脚本文件重新初始化数据表

[英]Reinitialize datatables with different script file

I have multiple datatables and created one js in where they have similar setup. 我有多个数据表,并在其中具有类似设置的地方创建了一个js。 I use the class of all tables instead each id of my tables. 我使用所有表的类而不是表的每个ID。

What doesnt work is if i want to create a specific jquery for each datatables where they have differences . 如果我想为每个有差异的数据表创建一个特定的jQuery,那是行不通的。 Some columns are needed not to be selected while others need to be . 有些列不需要选择,而另一些则需要。

So I used the Id for each table but it says cannot reinitialize. 因此,我为每个表使用了ID,但是它说无法重新初始化。 I also tried using bdestroy :true but one is being disabled 我也尝试使用bdestroy :true但其中之一被禁用

Here is my uniform script where i use the class : 这是我使用类的统一脚本:

<script type="text/javascript">         
$(document).ready(function () {
$('.datatables').dataTable(     
    {        
       'initComplete': function(settings){
      var api = new $.fn.dataTable.Api(settings);

      api.columns().header().each(function(column){
         if($(column).text() === 'Company'){
            $(column).text("Apple");
         }
          if($(column).text() === 'Note'){
            $(column).text("Description");
         } }); },
     "pagingType": "full_numbers",
             "oSearch": {"bSmart": false},
  dom: '<"coll"B><"search"f><"table"t><"info" i><"list"l><"pag"p>',

buttons: [{          
        extend: 'collection',
        text: 'Esporti',        
  buttons: [ {
            extend: 'excel',
            text: 'Excel',
            exportOptions: {
                modifier: {
                    selected: true
                            }}},{
            extend: 'pdf',
            text: 'PDF',
            exportOptions: {
                modifier: {
                    selected: true
                            }}} ]}, ]
        });  });
</script>

Here is the script for each page where i use the table id of each tables : 这是我使用每个表的表ID的每个页面的脚本:

<script>
$(document).ready(function () { 
$('#circuiti_table').dataTable({
      select: {
        style: 'multi',
        selector: ' tr>td:nth-child(4), tr>td:nth-child(5), tr>td:nth- child(6), tr>td:nth-child(7)'
      } });  }); 
    </script>

thanks in advance 提前致谢

Instead of using a combination of class name to specify common options and ID to specify unique options I would recommend a different approach. 我建议使用其他方法,而不是使用类名的组合来指定通用选项,而使用ID来指定唯一选项。

Use $.fn.dataTable.defaults object to specify common options for all tables. 使用$.fn.dataTable.defaults对象为所有表指定通用选项。 Then on each page, initialize a table using ID but only include options that are different. 然后在每个页面上,使用ID初始化表,但仅包含不同的选项。

For example: 例如:

// jQuery DataTables: Common settings
$.extend( true, $.fn.dataTable.defaults, {
    "searching": false,
    "ordering": false
} );

// jQuery DataTables: Individual table
$(document).ready(function() {
    $('#example').DataTable();
} );

See Setting defaults for more information. 有关更多信息,请参见设置默认值

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

相关问题 DataTables无法重新初始化或需要插件 - DataTables cannot reinitialize or plugin is required 数据表-显示更多/详细信息-重新初始化问题 - Datatables - show extra / detailed information - reinitialize issue 如何在HTML文档中重新初始化脚本? - How to reinitialize a script in an HTML document? DataTables - 从不同JavaScript文件中创建的HTML表中删除DataTable - DataTables - Remove DataTables from HTML Table created in different JavaScript File DataTables在以下情况下不起作用<script>'s are placed at the end of file - DataTables doesn't work when <script>'s are placed at the end of file 如何使用 MVC 中的 ajax 使用从服务器新获取的数据重新初始化数据表 - How to reinitialize dataTables with newly fetched data from server using ajax in MVC 如何使用数据表 Jquery 插件销毁表并使用新的更新数据重新初始化同一个表 - how to destroy table and reinitialize same table with new updated data using Datatables Jquery plugin jQuery Datatables从局部视图填充多个表-“无法重新初始化”错误 - jquery Datatables multiple tables populated from partial view - 'Cannot Reinitialize' error 当我在页面已经加载后加载脚本时重新初始化 dom - reinitialize dom when i load a script after the page already loaded 不同输入中的数据表过滤器 - datatables filter in different input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM