【问题标题】:Error: DataTables warning: table id = example1 - Cannot reinitialize DataTable错误:DataTables 警告:table id = example1 - 无法重新初始化 DataTable
【发布时间】:2020-01-10 14:25:00
【问题描述】:

你还好吗?

我遇到了问题,需要你的帮助。

我的应用程序显示此错误消息:“DataTables 警告:table id = example1 - 无法重新初始化 DataTable。有关此错误的详细信息,请参阅 http://datatables.net/tn/3”。

我在某些地方读到,仅启动一次“数据表”很重要,但找不到好的解决方案。我已经尝试过很多方式,但在大多数情况下,我都错过了我的语言(巴西葡萄牙语)的翻译。我的代码如下。

你能告诉我我做错了什么吗?

提前谢谢大家!

<script>
  $(document).ready(function() {
    $('#example1').DataTable( {
        "oLanguage": {
          "sEmptyTable": "Nenhum registro encontrado",
          "sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
          "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
          "sInfoFiltered": "(Filtrados de _MAX_ registros)",
          "sInfoPostFix": "",
          "sInfoThousands": ".",
          "sLengthMenu": "_MENU_ resultados por página",
          "sLoadingRecords": "Carregando...",
          "sProcessing": "Processando...",
          "sZeroRecords": "Nenhum registro encontrado",
          "sSearch": "Pesquisar",
          "oPaginate": {
              "sNext": "Próximo",
              "sPrevious": "Anterior",
              "sFirst": "Primeiro",
              "sLast": "Último"
          },
          "oAria": {
              "sSortAscending": ": Ordenar colunas de forma ascendente",
              "sSortDescending": ": Ordenar colunas de forma descendente"
          },
          "select": {
              "rows": {
                  "_": "Selecionado %d linhas",
                  "0": "Nenhuma linha selecionada",
                  "1": "Selecionado 1 linha"
              }
          }
        }, 

        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ],
        language: {
            buttons: {
                copyTitle: 'Copiado para área de transferência',
                copyKeys: 'Pressione <i>ctrl</i> ou <i>\u2318</i> + <i>C</i> para copiar os dados da tabela para a área de transferência. <br> <br> Para cancelar, clique nesta mensagem ou pressione Esc.',
                copySuccess: {
                    _: '%d linhas copiadas',
                    1: '1 linha copiada'
                }
            }
        }

    } );
    // Setup - add a text input to each footer cell
    $('#example1 thead tr').clone(true).appendTo( '#example1 thead' );
    $('#example1 thead tr:eq(1) th').each( function (i) {

        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Procurar '+title+'" />' );

        $( 'input', this ).on( 'keyup change', function () {
            if ( table.column(i).search() !== this.value ) {
                table
                    .column(i)
                    .search( this.value )
                    .draw();
            }
        } );
    } );

    var table = $('#example1').DataTable( {

        orderCellsTop: true,

    } );
} );
</script>

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    就像你说的,你初始化$('#example1').DataTable(..... 两次。

    这里

      $(document).ready(function() {
        $('#example1').DataTable( {
      .....
    

    这里

    var table = $('#example1').DataTable( {
    
        orderCellsTop: true,
    
    } );
    

    所以错误说你不能这样做,你应该把orderCellsTop: true放在第一个初始化过程中。

    【讨论】:

    • 我这样做了,但后来它停止按列过滤,这对我的应用程序来说是必需的。
    【解决方案2】:

    如果在第一次调用DataTable() 时设置orderCellsTop 属性不是一个选项,那么您可能必须销毁现有表并初始化一个新表。

    首先使用$.fn.dataTable.isDataTable() 检查表是否已存在,如果存在,则使用table.destroy(); 将其销毁,然后使用您需要的任何属性创建一个新表。

    所有这些都在page linked in the error 上进行了解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 2018-04-24
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多