【问题标题】:automatically updates the table without refreshing/reloading the page自动更新表格而不刷新/重新加载页面
【发布时间】:2021-03-30 00:16:58
【问题描述】:

我想问一下如何在不重新加载或刷新的情况下获取表中显示的数据的最新更新。数据的插入已经很好,它已经插入但是我看到页面仍在重新加载。我仍然是使用客户端的 ajax 和 sweetalert 的新手。

添加用户.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){
                    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){
                                Swal.fire({
                                    title: "Successfully Added!",
                                    text: "New student has been added!",
                                    icon: "success"
                                }).then(function() {
                                    // how to append the buttons here? it doesn't append or how do I append the entire page so that there will be a button after inserting a data in the table (see the image provided below)
                                    <tr>
                                       <td>${studentNumberId}</td>
                                       <td>${studentFullName}</td>
                                       <td>${studentEmail}</td>
                                       <td>${studentYearBlock}</td>
                                       </tr>
                                });
                            },
                            error: function(xhr, thrownError, ajaxOptions){
                                Swal.fire({
                                    title: "Successfully Added!",
                                    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'
                )
            }
        });   
            });      
    });

这是我桌子的代码。

table.php

<?php
include 'includes/connection_operation.php';
$sql = "SELECT * FROM tbl_students";
$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';
        }
    }   
?>

有没有一种方法,而不是附加表数据,我只附加整个文件?我不能附加按钮,只能附加表格数据。

【问题讨论】:

  • 如果没有看到实际页面,很难提供实际代码作为回复给您。但是逻辑是这样的:一旦您确认插入的数据,使用 javascript 将相同的项目添加到页面,因为您使用的是 jquery,类似于 $('container').append('
    All the new records values
    ');
  • @Jesse 您好,先生,如果之前缺少一些信息,我已经添加/更新了更多信息。

标签: jquery ajax sweetalert2


【解决方案1】:

您只需将其添加到页面中,实际上就这么简单。像这样的东西(因为你使用的是 jquery)应该让你开始:

我将不得不对您的页面布局方式做出假设。假设它是一个带有“mytable”类的表。

在您的success: function(data){ 部分中:

$('.mytable').append(`
    <tr>
        <td>${studentNumberId}</td>
        <td>${studentFullName}</td>
        <td>${studentEmail}</td>
        <td>${studentYearBlock}</td>
    </tr>
`);

这应该让您开始理解这个概念。

【讨论】:

  • 请问,是否可以加载表而不是追加?我已将我的表分隔到另一个文件中,并且在该文件中我确实有一个可以修改或删除的操作。你给的那个正在工作,但没有附加操作按钮。有没有办法在不手动输入学生编号等详细信息的情况下附加整个文件/表?
  • 将表格加载到 div 中。这可以通过给 div 一个 ID 然后使用 $('#mydiv).html('...) 而不是 append 来完成。
  • @T3.0 请问.html('...')里面是什么
  • 上述答案中“追加”的内容:$('#mydiv').html(' ${studentNumberId} ${ studentFullName} ${studentEmail} ${studentYearBlock} )
猜你喜欢
  • 2011-05-17
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2011-04-08
  • 2021-01-07
相关资源
最近更新 更多