gpt4 book ai didi

javascript - 添加数据后重新加载我的数据表

转载 作者:行者123 更新时间:2023-11-30 15:18:13 24 4
gpt4 key购买 nike

为什么我的数据表没有重新加载?。在我的情况下,我有一个名为 table_clinics 的数据表,我在我的 javascript 中声明了它是全局的,现在我有其他功能可以在数据表中添加数据,我想防止刷新,这就是我想要的原因添加新数据后要重新加载我的数据表。我做错了吗?

这是我的代码:

<script type="text/javascript">
var table_clinics;

$(document).ready(function() {
table_clinics = $('#dataTables-clinics').dataTable();
});

function show_clinics() {
$("#dataTables-clinics").dataTable().fnDestroy();
table_clinics = $('#dataTables-clinics').DataTable({
"ajax": {
"url": "<?php echo site_url('clinic_admin/myclinics')?>",
"type": "POST",
},
responsive: true,
bInfo: false,
});
}
</script>

现在我有一个从数据表添加数据的功能,成功后,我想刷新我的数据表。这是我的功能:我试过:table_clinics.ajax.reload(null,false); 但它没有重新加载我的数据表。我该怎么做?

function create_clinic() {
$('#btnCreateClinic').text('Saving...');
$('#btnCreateClinic').attr('disabled',true);


$.ajax({
url : siteurl+"clinic_admin/create_clinic",
type: "POST",
data: $('#frm_create_clinic').serialize(),
dataType: "JSON",
success: function(data)
{
if(data.status) {
table_clinics.ajax.reload(null,false);
alert('Added Sucessfuly');
}
else
{
for (var i = 0; i < data.inputerror.length; i++)
{
$('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error');
$('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]);
}
}
$('#btnCreateClinic').text('Save'); //change button text
$('#btnCreateClinic').attr('disabled',false); //set button enable


},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding data' + jqXHR+ textStatus +errorThrown);
$('#btnCreateClinic').text('Save');
$('#btnCreateClinic').attr('disabled',false);
}
});
}

也试过这个:

if(data.status) {
table_clinics = $("#dataTables-clinics").dataTable( { bRetrieve : true } );
table_clinics.fnDraw();
// Hide all tables
$('dataTables-clinics').hide();
// Show the refreshed
$('dataTables-clinics').show();
alert('Added Sucessfuly');
}

我也试过这个:然后我添加 https://cdn.datatables.net/plug-ins/1.10.15/api/fnReloadAjax.js .

if(data.status) {
table_clinics.fnReloadAjax('<?php echo site_url('"clinic_admin/myclinics"')?>');
table_clinics.fnReloadAjax();
alert('Added Sucessfuly');
}

最佳答案

重新加载你的数据表,试试这个

 $('#dynamic-table').DataTable().ajax.reload();  // id of table

关于javascript - 添加数据后重新加载我的数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44172536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com