php - 使用 Ajax 和 Codeigniter 的 DataTable 问题
问题描述
在通过单击每列的标题或对其进行搜索进行过滤后,我在加载数据表的内容时遇到了一些问题,表的数据或内容消失了,只有在重新加载页面后才会重新出现。
这是我的 DataTable 初始化和配置:
<script>
$(function () {
$("#example1").DataTable();
});
//Portugues
$('#example1').DataTable({
"language": {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registos",
"sZeroRecords": "Não foram encontrados resultados",
"sEmptyTable": "Nenhum dado dísponivel nesta tabela",
"sInfo": "A mostrar registos de _START_ a _END_ de um total de _TOTAL_ registos",
"sInfoEmpty": "Mostrando registos de 0 a 0 de um total de 0 registos",
"sInfoFiltered": "(filtrado de um total de registos _MAX_)",
"sInfoPostFix": "",
"sSearch": "Pesquisar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "A carregar...",
"oPaginate": {
"sFirst": "Primeiro",
"sLast": "Último",
"sNext": "Seguinte",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Ative para ordenar a coluna em ordem ascendente",
"sSortDescending": ": Ative para ordenar a coluna em ordem decrescente"
}
}
});
</script>
这是我的 Ajax 函数,它将数据返回到表中:
$( document ).ready(function() {
$.ajax({
type : "POST",
url : "<?php echo base_url(); ?>CRUD_Controller/crud_getDataAll/nivel/niveis",
cache: false,
contentType: false,
processData: false,
dataType: 'html',
success: function(data){
//alert("sucesso - " + data);
$('#example1 tbody').html(data);
},
error:function(xhr)
{
alert('Algo falhou, nao caregou a tabela. - ' + xhr.statusText);
}
});
});
这是我的控制器方法案例:
case 'niveis':
$rows = $this->Administracao_model->getAllData($table_name);
foreach($rows as $row) {
echo '<tr>';
echo '<td class="align-middle text-center"><a href="#">#NIV' . $row->id . '</a></td>';
echo '<td class="align-middle text-center"><span class="badge ' . $row->classe . '">' . $row->nome . '</span></td>';
($row->adicionar == 1) ? $adicionar = "Adicionar" : $adicionar = "";
($row->remover == 1) ? $remover = ", Remover" : $remover = "";
($row->alterar == 1) ? $alterar = ", Alterar" : $alterar = "";
($row->pesquisar == 1) ? $pesquisar = ", Pesquisar" : $pesquisar = "";
echo '<td class="align-middle text-center">' . $adicionar, $remover, $alterar, $pesquisar . '</td>';
if($row->id == 1){
echo '<td class="align-middle text-center" data-toggle="tooltip" data-placement="top" title="Não é possível remover o nível 0 do sístema."><a href="#" class="btn btn-danger text-white btn-sm disabled" disabled="disabled"><i class="fas fa-trash"></i></a></td>';
}else{
echo '<td class="align-middle text-center"><a href="#" class="btn btn-danger text-white btn-sm" onclick="showModalCerteza(' . $row->id . ')"><i class="fas fa-trash"></i></a></td>';
}
echo '</tr>';
}
break;
这是我的视图表:
<table id="example1" class="table table-sm table-bordered table-striped Responsive">
<thead>
<tr>
<th class="text-center">ID</th>
<th class="text-center">Nome</th>
<th class="text-center">Ações Permissíveis</th>
<th class="text-center">Ação</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th class="text-center">ID</th>
<th class="text-center">Nome</th>
<th class="text-center">Ações Permissíveis</th>
<th class="text-center">Ação</th>
</tr>
</tfoot>
</table>
提前非常感谢!
解决方案
推荐阅读
- ios - 如何播放scn模型的动画
- psql - PSQL:ROW_NUMBER 连续递增
- c# - 来自golang和c#的elixir函数的并行或异步任务
- c# - 如何在 web api 中返回相关实体,并从单独的 mvc 应用程序调用 web api
- javascript - 提交表单按钮出现错误“此页面正在运行”!
- python - 我正在尝试根据月份和年份从文件集合中读取特定数量的文件
- postgresql - 如何使用 python 格式化 postgres 查询输出
- javascript - 将动画列表与静态列表类似地对齐
- swift - 是否有一个 swift 数据结构可以包含可以是任何类型并且可以切换的 rawValue
- javascript - 获取登录屏幕的 typeError 反应本机?