我有一个在脚本中加载的数据表,如下所示:
<script>
function table() {
$.ajax({
url: 'tables/winnertable-all.php',
type: 'get',
data: { name: '<?php echo $name ?>', Type: '<?php echo $Type ?>' },
success: function(response){
$('.winnertable').html(response);
$('#newtable').DataTable();
}
});
}
table();
setInterval(table, 3600000);
table();
setInterval(table, 3600000);
$(document).ready(function() {
$('#newtable').DataTable();
} );
</script>
我想向第一列添加默认排序,我在数据表网站上找到了此代码作为示例:
$(document).ready(function() {
$('#example').DataTable( {
"order": [[ 3, "desc" ]]
} );
} );
我想在我的代码中实现这一点,但是在添加该行后我无法加载表格。 我尝试过:
<script>
function table() {
$.ajax({
url: 'tables/winnertable-all.php',
type: 'get',
data: { name: '<?php echo $name ?>', Type: '<?php echo $Type ?>' },
success: function(response){
$('.winnertable').html(response);
$('#newtable').DataTable();
"order": [[ 1, "desc" ]];
}
});
}
table();
setInterval(table, 3600000);
table();
setInterval(table, 3600000);
$(document).ready(function() {
$('#newtable').DataTable();
"order": [[ 1, "desc" ]];
} );
</script>
我知道他们的语法实现存在问题,但是我尝试了各种不同的方法,但无法使其正常工作。
最佳答案
一般来说,如果您想重新初始化DataTable以更改其基本设置,则需要使用destroy
标志。
但是,在您的情况下,我只需将所需的订单
设置添加到默认值
$.extend( true, $.fn.dataTable.defaults, {
order: [[ 3, "desc" ]]
})
如果您在页面加载时预先执行此操作,则所有表格的默认顺序将是 order: [[ 3, "desc"]]
。如果您出于某种原因不想更改默认值,您可以简单地利用 API,令人惊讶的是 order
方法
var table = $('#newtable').DataTable()
table.order( [[ 3, "desc" ]] ).draw()
关于javascript - 我的数据表问题的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57939678/