在将数据插入数据库后尝试刷新此数据表时遇到了一些麻烦。我希望数据表在javascript ajax上刷新,但是我不希望整个页面刷新。
我是数据表的新手,似乎无法弄清楚该怎么做。
任何帮助将不胜感激。
JavaScript
$.ajax({
url: 'post_project_definition',
type: 'GET',
data: {
discID: $("#disciplinesProjDef").val(),
discComment: $("#txtDiscipComment").val(),
},
success: () => {
swal({
type: 'success',
title: 'Your comment has been added successfully'
});
}
})
$.getJSON('selectProjectDefinitionDescription', function (data) {
$.each(data, function (i, item) {
ProjDefTable.push([data[i].id,data[i].definition_name, data[i].description,
"<input type='button' class='btn btn-danger' id='" + data[i].id + "delete' value='-'></input>"+
"<script>"+
" $('#"+data[i].id+"delete').on('click', () => {" +
" $.ajax({"+
" url: 'delete_project_definition',"+
" type: 'GET',"+
" data: { "+
" project_def_id: '"+data[i].id+"' "+
" },"+
" });"+
" });" +
"</script>"
]);
var table = $("#projDiscComTable").DataTable();
table.clear().draw();
table = $("#projDiscComTable").DataTable({
data: ProjDefTable,
columns: [
{title: "ID"},
{ title: "Discipline Name" },
{ title: "Discipline Description" },
{ title: "Delete" },
],
destroy: true
});
})//end else
});
});
PHP
public function selectProjectDefinitionDescription()
{
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$type = $session_data['usertype'];
$project_id = $session_data['project_id'];
$stuff = $this->Projects_Model->load_Project_Definition_Discipline($project_id);
echo $stuff;
}
HTML
<table id="projDiscComTable" class="table table-hover display">
<thead>
<tr>
<th>ID</th>
<th>Disciple</th>
<th>Discipline Comment</th>
<th>Delete</th>
</tr>
</thead>
</table>
答案 0 :(得分:1)
我并没有使用DataTable
,但是我相信使用它们是为了轻松地为您的HTML表提供各种功能,例如排序等。 (Reference)
来到这里,我相信您的范围是基于AJAX响应刷新表的内容,而不必刷新整个页面。
您可以使用jQuery的append()
和remove()
函数来做到这一点。一个简单的算法就是这样:
<div id="definition-row-sample">
<tr class="definition-row">
<td class="definition-id"></td>
<td class="definition-name"></td>
<td class="definition-description"></td>
<td class="definition-delete"></td>
</tr>
</div>
.definition-row
隐藏起来,使其不会显示在页面中。$('.definition-row').remove()
删除所有行,然后遍历数据并将其附加到表中的每一行: $.each(data, function (i, item) {
$('#definition-row-sample').find('.definition-id').text(data[i].id);
$('#definition-row-sample').find('.definition-name').text(data[i].definition_name);
$('#definition-row-sample').find('.definition-description').text(data[i].description);
//then simply append it to the table
let content = $('#definition-row-sample').find('.definition-row').clone();
$('#projDiscComTable').append(content);
});
对于删除,您可以定义一个普通的函数,例如:
function definition_delete(id) {
$.ajax({
url: 'delete_project_definition',
type: 'GET',
data: {
project_def_id:id
}
});
}
document.on('click', '.definition-delete', function(){
let id = $(this).parent().find('.definition-id').text();
definition_delete(id);
});
我还没有测试代码,但是我确信您能理解并实现它,因为我也使用了与我的项目之一相似的逻辑。
答案 1 :(得分:1)
插入注释后,仅刷新表怎么样??
$('#projDiscComTable').DataTable().ajax.reload();
$.ajax({
url: 'post_project_definition',
type: 'GET',
data: {
discID: $("#disciplinesProjDef").val(),
discComment: $("#txtDiscipComment").val(),
},
success: () => {
$('#projDiscComTable').DataTable().ajax.reload();
swal({
type: 'success',
title: 'Your comment has been added successfully'
});
}
})
不确定这是否符合您的要求,但是您仍然可以在插入评论后刷新它。
答案 2 :(得分:0)
我发现了这一点,我成功地在ajax中重新绘制了表格,从我所看到的来看,它似乎可以正常工作。但是,谢谢您的所有回答。以后我会用你们的建议。
var ProjDefTable = [];
$('#btnDiscipCommPost').on('click', () => {
$.ajax({
url: 'post_project_definition',
type: 'GET',
data: {
discID: $("#disciplinesProjDef").val(),
discComment: $("#txtDiscipComment").val(),
},
success: () => {
swal({
type: 'success',
title: 'Your comment has been added successfully'
});
$.getJSON('selectProjectDefinitionDescription', function (data) {
$.each(data, (i) => {
ProjDefTable.push([data[i].id,data[i].definition_name, data[i].description,
"<input type='button' class='btn btn-danger' id='" + data[i].id + "delete' value='-'></input> <span id='" + data[i].id + "deleted' style='display: none; font-weight: 700;'>DELETED</span>"+
"<script>"+
" $('#"+data[i].id+"delete').on('click', () => {" +
" $.ajax({"+
" url: 'delete_project_definition',"+
" type: 'GET',"+
" data: { "+
" project_def_id: '"+data[i].id+"' "+
" },"+
" success: () => {"+
" var x = document.getElementById('" + data[i].id + "delete');" +
" x.style.display = 'none';" +
" var y = document.getElementById('" + data[i].id + "deleted');" +
" y.style.display = 'block';" +
" } "+
" });"+
" });" +
"</script>"
]);
var table = $("#projDiscComTable").DataTable();
table.clear().draw();
table = $("#projDiscComTable").DataTable({
data: ProjDefTable,
columns: [
{title: "ID"},
{ title: "Discipline Name" },
{ title: "Discipline Description" },
{ title: "Delete" },
],
destroy: true
});
})//end else
});
var ProjDefTable = [];
}
})
});