4
I am creating a table and need to give option to sort the table by the field you prefer. I tried to use the datatable the data is filled correctly in the table but when I click on a header to sort, filter, or any other function of the datatable the table data vanishes and the message appears:
No data available in table
My codes:
HTML
<div class="table-responsive">
<table id="tableTarefas" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Nome</th>
<th>Grau</th>
<th>Data</th>
<th>Descrição</th>
<th>Tempo de execução</th>
<th>Informações adicionais</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
JS
$("#tableTarefas").dataTable();
$.ajax({
type: "get",
url: rootUrl + "tarefas/listAll" + updat,
dataType: "json",
success: function(data)
{
$("#tableTarefas").find("tbody tr").remove();
data.result.forEach(function(tarefa)
{
row= "<tr>"
+ "<td><a id='edit' href='#' data-id='" + tarefa.id_tar + "'>"+ tarefa.non_tar + "</a></td>"
+ "<td>"+ tarefa.gra_tar + "</td>"
+ "<td>" + tarefa.datf_tar + "</td>"
+ "<td>" + tarefa.des_tar + "</td>"
+ "<td>" + tarefa.tee_tar + "</td>"
+ "<td>" + tarefa.inf_tar + "</td>"
+ "</td></tr>";
$("#tableTarefas > tbody:last").append(row);
});
},
error: function(result)
{
$("#errorLoad").html(getErrorMessage(result.responseText));
$("#errorLoad").show();
$("#tableTarefas").find("tbody tr").remove();
}
});
Web service return: (2 Rows)
{
"result":[
{
"id_tar":"24",
"non_tar":"trtyr",
"gra_tar":"10",
"tee_tar":"00:00:00",
"dati_tar":"2015-11-14",
"datf_tar":"0000-00-00",
"datd_tar":"2015-11-15",
"des_tar":"",
"con_tar":"0",
"idu_tar":"1",
"idud_tar":null,
"inf_tar":"",
"pts_tar":"0"
},
{
"id_tar":"42",
"non_tar":"asdas",
"gra_tar":"1",
"tee_tar":"00:00:00",
"dati_tar":"2015-11-15",
"datf_tar":"0000-00-00",
"datd_tar":"2015-11-15",
"des_tar":"",
"con_tar":"0",
"idu_tar":"1",
"idud_tar":null,
"inf_tar":"",
"pts_tar":"0"
}
]
}
Does anyone know how to solve this problem?