【发布时间】:2021-06-26 17:03:55
【问题描述】:
问题:我希望每次插入数据并从 sweetalert 弹出窗口中单击确定时,表会自动获取新插入的数据,并且应该已经在 升序。
我尝试过的解决方案: 我试过每次成功插入时都会走到表的末尾,这是不合适的,需要重新加载才能排序,我使用 .append() 来获取新数据。
我正在为我的表使用 DataTables 插件。
我现在的设置是每次插入数据时我都必须重新加载页面,我认为这不好,因为我使用的是 ajax。我正在使用location.href = "URL"
有什么办法可以在不刷新页面的情况下刷新页面吗?
添加用户.js
$(document).ready(function(){
$('#addStudent').click(function(e){
e.preventDefault();
Swal.fire({
title: "Are you sure?",
text: "New student will be added added!",
icon: "success",
showCancelButton: true,
allowEscapeKey : false,
allowOutsideClick: false
}).then((result) => {
if (result.isConfirmed) {
var valid = this.form.checkValidity();
if(valid){
// The studentNumberId should be the basis whether to know if the user is already existing
var studentNumberId = $('#studentNumberId').val();
var studentFullName = $('#studentFullName').val();
var studentPassword = $('#studentPassword').val();
var studentEmail = $('#studentEmail').val();
var studentYearBlock = $('#studentYearBlock').val();
e.preventDefault()
$.ajax({
type: 'POST',
url: 'includes/model_addStudent.php',
data: {studentNumberId: studentNumberId,studentFullName: studentFullName,studentPassword: studentPassword,studentEmail: studentEmail,
studentYearBlock: studentYearBlock},
success: function(data){
if(data === "true"){
// swal if the adding failed.
Swal.fire({
title: "Adding Failed",
text: "Student Already Existing",
icon: "warning"
}).then((result) => {
if (result.isConfirmed) {
$('#addModalStudent').modal('hide').find("input").val('');
}
});
}
else {
// it fetch without reloading but it adds at the bottom and needs to reload the sort the data. Solution that I tried //
/*
$('#tbl_manageStudents').append(`
<tr>
<td>${studentNumberId}</td>
<td>${studentFullName}</td>
<td>${studentEmail}</td>
<td>${studentYearBlock}</td>
</td>
<td>
<input type="submit" name="viewStudents" id="viewStudents" value="View" class="btn btn-info"
data-toggle="modal" data-target="#viewExistingStudents<?php echo $row["ID"];?>
<input type="submit" name="deleteRecord" id="deleteRecord" value="Delete" class="btn btn-danger"
data-toggle="modal" data-target="#deleteSelectedStudent<?php echo $row["ID"];?>
</td>
`);
*/
// swal if the user was successfully added
Swal.fire({
title: "Successfully Added!",
text: "New student has been added!",
icon: "success"
}).then((result) => {
if (result.isConfirmed) {
// current set up
location.href = "ManageStudents.php"
$('#addModalStudent').modal('hide').find("input").val('');
// $('#tbl_manageStudents').load("./helper/helper_tblManageStudent.php");
}
});
}
},
error: function(xhr, thrownError, ajaxOptions){
Swal.fire({
title: xhr,
text: thrownError,
icon: "info"
})
}
});
}
else {
Swal.fire({
title: "Error!",
text: "Invalid Form",
icon: "warning"
});
}
}
else {
Swal.fire(
'No Changes!',
'No New Student has been added.',
'info'
)
}
});
});
});
ManageUsers.php
<div class="container-sm table-responsive" id="tblManageStudent">
<table class="table table-striped table-hover table-condense" id="tbl_manageStudents">
<thead>
<tr>
<th scope="col">Student ID</th>
<th scope="col">Student Name</th>
<th scope="col">Student Email</th>
<th scope="col">Student Year and Block</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr>
<!-- Change the tbl to tbl_students -->
<?php
include 'helper/helper_tblManageStudent.php';
?>
</tbody>
</table>
</div>
helper_tblManageStudents.php
<?php
include 'includes/connection_operation.php';
$sql = "SELECT * FROM tbl_students ORDER BY ( 0 + student_id ) ASC";
$query = mysqli_query($conn,$sql);
if($query)
{
while($row = mysqli_fetch_assoc($query))
{
?>
<td><?php echo $row['student_id']; ?></td>
<td><?php echo $row['student_name']; ?></td>
<td><?php echo $row['student_email']; ?></td>
<td><?php echo $row['student_yrblock']; ?></td>
<td>
<input type="submit" name="viewStudents" id="viewStudents" value="View" class="btn btn-info"
data-toggle="modal" data-target="#viewExistingStudents<?php echo $row["ID"];?>">
<input type="submit" name="deleteRecord" id="deleteRecord" value="Delete" class="btn btn-danger"
data-toggle="modal" data-target="#deleteSelectedStudent<?php echo $row["ID"];?>">
</td>
</tr>
<?php
include './helper/helper_viewExistingStudents.php';
include './helper/helper_deleteSelectedStudent.php';
}
}
?>
到底有没有用新插入的数据重新加载helper_tblManageStudents.php的表而不刷新实际页面并获取数据?
【问题讨论】:
-
方法错误。使用 Datatable api 将新数据传递给插件,然后进行重绘。如果您插入自己的 html,则行 html 和数据的插件缓存对此一无所知
标签: jquery ajax datatables sweetalert2