表不是通过PHP排序,而是在HTML内部排序
我有一个由php创建的html表,我想对这个表进行排序,但没有成功。 如果我在html中创建了表,那么排序就会起作用表不是通过PHP排序,而是在HTML内部排序,php,html,Php,Html,我有一个由php创建的html表,我想对这个表进行排序,但没有成功。 如果我在html中创建了表,那么排序就会起作用 <!DOCTYPE html> <html> <head> <title>Teste</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!DOCTYPE html>
<html>
<head>
<title>Teste</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
<div class="container mb-3 mt-3" id="inicio">
</div>
<script>
$(document).ready(function(){
load_list();
$('.mydatatable').DataTable();
function load_list()
{
var action = "data";
$.ajax({
url: "teste.php",
method:"POST",
data:{action:action},
success:function(data)
{
$('#inicio').html(data);
}
})
}
});
</script>
</body>
</html>
睾丸
$(文档).ready(函数(){
加载列表();
$('.mydatatable').DataTable();
函数加载列表()
{
var action=“数据”;
$.ajax({
url:“teste.php”,
方法:“张贴”,
数据:{action:action},
成功:功能(数据)
{
$('#inicio').html(数据);
}
})
}
});
php中的表示例:
<?php
if(isset($_POST["action"]))
{
if($_POST["action"]=="data")
{
$output = '
<table class="table table-striped table-bordered mydatatable" style="width: 100%">
<thead>
<tr>
<th>Tittle</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
</tr>
<tr>
<td>date2</td>
</tr>
</tbody>
</table>
';
echo $output;
}
?>
我认为$('.mydatatable').DataTable();在错误的位置,我尝试了我的选项,但只有当表格在html页面中时才起作用。有人能帮我吗?初始化数据表时,表html不存在
$(document).ready(function(){
load_list();
function load_list()
{
var action = "data";
$.ajax({
url: "teste.php",
method:"POST",
data:{action:action},
success:function(data)
{
$('#inicio').html(data);
//move this to here
$('.mydatatable').DataTable();
}
})
}
由于
ajax
中的第一个A表示异步
,然后是对$('.mydatatable').DataTable()的调用代码>发生在通过ajax加载实际数据之前。您应该将对DataTable
的调用移动到success
callback:
success:function(data)
{
// note the order - first you load `html`
$('#inicio').html(data);
// after that you have a `.mydatatable` selector available
$('.mydatatable').DataTable();
}
很好,谢谢。我尝试了$('.mydatatable').DataTable()的很多位置,但没有在内部成功。