【问题标题】:DataTables warning: table id=table_especie - Requested unknown parameter '0' for row 0, column 0DataTables 警告:表 id=table_especie - 请求第 0 行第 0 列的未知参数“0”
【发布时间】:2017-06-17 01:17:44
【问题描述】:

我知道这被问了数百次,但我认为我的案例非常具体,需要了解 jQuery 的人来帮助,或者至少以前见过这个!

我有这段代码来构建一个名为“especie:”的表:

HTML

<table class="table" id="table_especie">
     <thead>
      <tr>
       <th></th>
       <th></th>
      </tr>
     </thead>
     <tbody>
     </tbody>
</table>

脚本 JS:

var tableEspecie= $('#table_especie').DataTable({
    "paging":   false,
    "info":     false,
    "order": [
        [2, "asc" ],
        [3, "asc"],
        [1, "asc"]
    ],
    "columnDefs": [
        { "visible": false, "targets": 0 },
        { "visible": false, "targets": 2 },
        { "visible": false, "targets": 3 }
    ],

    "drawCallback": function () {
        var api = this.api();
        var rows = api.rows( {page:'current'} ).nodes();
        var last=null;

        api.column(2, {page:'current'} ).data().each( function ( especie, i ) {
            if ( last !== especie) {
                $(rows).eq( i ).before(
                    '<tr class="especie info"><td colspan="4">'+especie+'</td></tr>'
                );

                last = especie;
            }
        } );

        $("#table_especie thead").remove();
        $("#table_especie tfoot").remove();
    }
});

var populateEspecieShowName = function(data) {
    $('#animal_especie_name').text(data[0].name);
};


var populateEspecieTable = function(data) {
    var animais = [];

    $.each(data, function(id_animal, animal){
        animais.push([
            animal.id_animal,
            animal.nome_animal + ': ' + '<br>' + animal.notas_animal,
            animal.foto_animal
        ]);
    });

    $('#table_especie').dataTable().fnClearTable();
    $('#table_especie').dataTable().fnAddData(animais);
    $('#table_especie').dataTable().fnDraw();
};

$('#table_especie tbody').on( 'click', 'tr', function () {
    var animalId = $('#table_especie').DataTable().row(this).data();

    if (animalId !== undefined)
        $.route('animal/' + animalId[0]);
});

$('#table_especie_search').keyup(function(){
    $('#table_especie').DataTable().search($(this).val(), false, true).draw() ;
});

基本上,它使用数据库中的数据构建表!每次我从表“especies”到表“especie”。该错误听起来像是“especie”有问题。我应该改变什么以使错误消失?它仍然构建表,但我之前收到此错误。谢谢!!!

【问题讨论】:

  • 我对您要做什么感到有些困惑。我看到您删除了标题,这会破坏 DataTables。您只是想创建动态列吗?
  • 是的。所以基本上我正在创建一个应用程序,它将从数据库(MySQL)中获取数据并动态显示它。这是一个从收容所中展示动物的应用程序,因此它需要能够从本地数据库中获取数据,以防添加/删除更多动物。

标签: javascript jquery html datatable


【解决方案1】:

解决方案:

"columnDefs": [{
    "defaultContent": "-",
    "targets": "_all"
}],

【讨论】:

  • 虽然这可能会回答这个问题,但一些 cmets 和/或解释会很好。这也将帮助其他用户。
  • 现在它不显示错误但它不建表,错误,使用 Chrome 调试时是:jquery.dataTables.min.js:65 Uncaught TypeError: Cannot read property ' aDataSort' 的未定义在 W (jquery.dataTables.min.js:65) 在 ya (jquery.dataTables.min.js:70) 在 e (jquery.dataTables.min.js:91) 在 HTMLTableElement. ( jquery.dataTables.min.js:93) 在 Function.each (jquery.min.js:2) 和更多...
猜你喜欢
  • 2019-01-25
  • 1970-01-01
  • 2020-11-30
  • 2016-09-24
  • 1970-01-01
  • 2021-06-08
  • 2014-09-07
  • 1970-01-01
相关资源
最近更新 更多