使用 ajax 的数据是否可以在 CodeIgniter 中实时显示而无需刷新页面视图?
Can data using ajax be made live realtime in CodeIgniter without having to refresh the page view?
我有我的代码,这个数据是来自数据库的实时数据,我想让它在不刷新页面的情况下显示实时数据。
showDataEmploye();
function showDataEmploye() {
$.ajax({
url: "<?php echo site_url('general_data/hr_data/ajaxGetEmploye'); ?>",
method: "POST",
dataType: 'JSON',
success: function(data) {
table.clear();
$.each(data, function(i, item) {
table.row.add([
item.gender,
item.total,
]).draw();
});
}
})
}
以及如何制作?
页面加载后,document.ready您可以执行以下操作。
$('document').ready(function(){
setTimeout(function() {
showDataEmploye()
}, 1000);
});
这将每秒调用一次,而且几乎是实时的。
除此之外,在 Ajax 调用 success/error/complete 你可以再次调用自己。
function showDataEmploye() {
$.ajax({
url: "<?php echo site_url('general_data/hr_data/ajaxGetEmploye'); ?>",
method: "POST",
dataType: 'JSON',
success: function(data) {
table.clear();
$.each(data, function(i, item) {
table.row.add([
item.gender,
item.total,
]).draw();
});
showDataEmploye();
}
})
}
可能会有更好的答案,这是 2 个选项。
虽然这可能不是最佳做法,但仍然有效...
我有我的代码,这个数据是来自数据库的实时数据,我想让它在不刷新页面的情况下显示实时数据。
showDataEmploye();
function showDataEmploye() {
$.ajax({
url: "<?php echo site_url('general_data/hr_data/ajaxGetEmploye'); ?>",
method: "POST",
dataType: 'JSON',
success: function(data) {
table.clear();
$.each(data, function(i, item) {
table.row.add([
item.gender,
item.total,
]).draw();
});
}
})
}
以及如何制作?
页面加载后,document.ready您可以执行以下操作。
$('document').ready(function(){
setTimeout(function() {
showDataEmploye()
}, 1000);
});
这将每秒调用一次,而且几乎是实时的。
除此之外,在 Ajax 调用 success/error/complete 你可以再次调用自己。
function showDataEmploye() {
$.ajax({
url: "<?php echo site_url('general_data/hr_data/ajaxGetEmploye'); ?>",
method: "POST",
dataType: 'JSON',
success: function(data) {
table.clear();
$.each(data, function(i, item) {
table.row.add([
item.gender,
item.total,
]).draw();
});
showDataEmploye();
}
})
}
可能会有更好的答案,这是 2 个选项。
虽然这可能不是最佳做法,但仍然有效...