我正在使用jQuery DataTables。在ajax请求之后,我调用the ajax.reload()
方法来刷新我的dataTable,但是我丢失了当前页面。它返回第一页。
这是我的DataTables init:
datatable_appointments = $('#datatable_appointments').DataTable({
"responsive" : true,
"processing" : true,
"serverSide": true,
"bSort" : false,
"ajax": function(data, callback, settings) {
// make a regular ajax request using data.start and data.length
$.get(URL_API + URL_APPOINTMENTS_STATE + '/', {
limit: data.length,
offset: data.start,
search: data.search.value
}, function(res) {
if (res.error) {
callback({
recordsTotal: 0,
recordsFiltered: 0,
data : ""
});
} else {
data_appointments = res.data;
// map your server's response to the DataTables format and pass it to
// DataTables' callback
callback({
recordsTotal: res.total_data,
recordsFiltered: res.total_data,
data: res.data
});
$("#datatable_appointments > tbody > tr > td").addClass("text-center");
$("#datatable_appointments > tbody > tr > td").css("vertical-align", "middle");
$("#datatable_appointments > thead > tr > th").addClass("text-center");
}
});
},
"columns" : [
{ "data": "service_name"},
{ "data": "doctor_info"},
{ "data": "patient_name", render: function (data, type, row, meta) {
return row.name + " " + row.surname;
}},
{ "data": "appointment_date", render: function (data, type, row, meta) {
var formattedDate = new Date(row.appointment_date);
var day = formattedDate.getDate();
var month = formattedDate.getMonth();
var hours = (formattedDate.getHours().toString().length == 1) ? '0' + formattedDate.getHours() : formattedDate.getHours();
hours += ':';
hours += (formattedDate.getMinutes().toString().length == 1) ? formattedDate.getMinutes() + '0' : formattedDate.getMinutes();
return day + ' ' + months[month] + ', ' + hours;
}},
{ "data": "state", render: function (data, type, row, meta) {
if (row.state == '0')
return '<i class="fa fa-circle font-red-mint"></i><br />İptal';
else if (row.state == '1')
return '<i class="fa fa-circle font-green-jungle"></i><br />Onaylı';
else if (row.state == '2')
return '<i class="fa fa-circle font-grey-salsa"></i><br />Beklemede';
}},
{ "data": "actions", render: function (data, type, row, meta) {
if (row.state == '2')
return '<div class="btn-group btn-group-circle">' +
'<button type="button" onclick="setState(\''+row.id+'\', \'1\');" class="btn btn-outline green btn-sm">Onayla</button>' +
'<button type="button" onclick="setState(\''+row.id+'\', \'0\');" class="btn btn-outline red btn-sm">İptal Et</button>' +
'</div>';
else
return '';
}}
],
"sPaginationType": "full_numbers",
"language": {
"emptyTable": "Kayıt bulunamadı.",
"sZeroRecords": "Bu kriterde sonuç bulunamadı.",
"search": "Bul",
"sProcessing" : "<img src='/assets/global/img/loading-spinner-blue.gif' />",
"sInfo": "Toplam <b>_TOTAL_</b> veri bulunmaktadır.",
"sLengthMenu": "Gösterilen kayıt sayısı _MENU_",
"sInfoFiltered": "",
"sInfoEmpty":"",
"paginate": {
"sPrevious": "Önceki",
"sNext": "Sonraki",
"sFirst" : "İlk",
"sLast" : "Son"
}
}
});
我的setState
方法在同一个js文件中:
function setState(id, state) {
$.ajax({
url : URL_API + URL_APPOINTMENTS_STATE + '/' + id,
type : 'put',
data : {'state' : state},
dataType : 'json',
success : function (response) {
if (response.error) {
} else {
datatable_appointments.ajax.reload();
}
}
});
}
答案 0 :(得分:16)
您需要传递用于保持当前页面的参数:
datatable_appointments.ajax.reload( null, false )
答案 1 :(得分:-1)
$(document).ready(function() {
$('#table_id').DataTable( {
stateSave: true
} );
});
任何使用jQuery DataTables并想要执行任务的人,例如重新加载页面,或编辑,删除,将面临丢失当前页面并导航到第一页的问题,此摘要将帮助他停留在同一页面上,从而避免了从当前页面返回到第一页。