【发布时间】: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