调用Ajax后,数据表无法重新加载

问题描述 投票:0回答:2

在我的Ajax调用成功后,我想用我的php部分返回的新值重新加载我的bootstrap表。

<table id="gelirtableid" data-toggle="table" data-url="gelir-getdata.php" data-classes="table table-hover" data-striped="true"
                        data-pagination="true" data-page-list=[20, 40, 75, 100] data-search="true">
                            <thead>
                                <tr >
                                    <th data-sortable="true" data-field="tarih">Tarih</th>
                                    <th data-sortable="true" data-field="Toplam">Toplam</th>                                    
                                </tr>
                            </thead>
</table>

我的php脚本从mysql中获取数据

<?php 
include "dbcon.php";
if($_POST["gelirtablosecimi"]){
        $gelirtabloadi = $_POST["gelirtablosecimi"];
        $_SESSION["gelirtabloadi"] = $gelirtabloadi;
}

$gelirtabloadi = $_SESSION["gelirtabloadi"];
$gelirgunluktoplam = $db->prepare("select tarih, hasilat + visa + butce_ici + hisse_satis + sosyal_konut + elektrik + haberlesme + iller_bank + diger AS Toplam from $gelirtabloadi");
$gelirgunluktoplam->execute();
$results = $gelirgunluktoplam->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo $json;

 ?>

和我的Ajax电话

$("#gelirgetir").click(function() {
                    var gelirtablosecimi = $("#select1").val();
                    if (gelirtablosecimi) {
                        $.ajax({
                            type: "POST",
                            url: "gelir-getdata.php",
                            data: {
                                "gelirtablosecimi": gelirtablosecimi
                            },
                            success: function(result) {                                                                
                                notifyUser('success', 'Başarılı!', 'Tablo başarıyla güncellendi');
                                location.reload();
                            },
                            error: function(result) {
                                notifyUser('error', 'Hata', 'error');
                            }
                        });
                    } else {
                        notifyUser('info', 'Dikkat', 'Tablo seçimi yapmadınız!');
                    }

                });

我是Ajax调用的新手,可能问题在于我的ajax部分。如你所见,我在电话会议成功后正在与location.reload();打交道。我尝试重装.container#gelirtableid,但没有任何对我有用。基本上,当我按下#gelirgetir按钮时,它会更新我的一个会话值,而我的表依赖于该会话值。会话值更改后,如果我重新加载页面,将显示新值,但我必须强制刷新页面。我只想刷新表格。有什么建议吗?

php jquery ajax bootstrap-table
2个回答
0
投票

替换您的表代码,以便您有一个表体。我们稍后会替换它。

<table id="gelirtableid" data-toggle="table" data-url="gelir-getdata.php" data-classes="table table-hover"
       data-striped="true"
       data-pagination="true" data-page-list=[20, 40, 75, 100] data-search="true">
    <thead>
    <tr>
        <th data-sortable="true" data-field="tarih">Tarih</th>
        <th data-sortable="true" data-field="Toplam">Toplam</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>

在您的函数中,您需要通过jQuery中的parseJSON函数从响应中提取数据。然后,您需要为桌面构建内容。这是一个例子。

$("#gelirgetir").click(function () {
    var gelirtablosecimi = $("#select1").val();
    if (gelirtablosecimi) {
        $.ajax({
            type: "POST",
            url: "gelir-getdata.php",
            data: {
                "gelirtablosecimi": gelirtablosecimi
            },
            success: function (result) {
                notifyUser('success', 'Başarılı!', 'Tablo başarıyla güncellendi');
                var content = '';
                $.each(result, function(i, data) {
                    content += '<tr>';
                    content += '<td>'+result[i].tarih+'</td>';
                    content += '<td>'+result[i].Toplam+'</td>';
                    content += '</tr>';
                });

                $('#gelirtableid tbody').html(content);
            },
            error: function (result) {
                notifyUser('error', 'Hata', 'error');
            }
        });
    } else {
        notifyUser('info', 'Dikkat', 'Tablo seçimi yapmadınız!');
    }

});

0
投票

当我从上面的解决方案得到的错误是关于JSON.parse()$.each希望解析JSON数据。以下代码就像魅力一样。

<table id="gelirtableid" data-toggle="table" data-url="gelir-getdata.php" data-classes="table table-hover" data-striped="true"
                            data-pagination="true" data-page-list=[20, 40, 75, 100] data-search="true">
                                <thead>
                                    <tr >
                                        <th data-sortable="true" data-field="tarih">Tarih</th>
                                        <th data-sortable="true" data-field="Toplam">Toplam</th>                                    
                                    </tr>
                                </thead>
</table>

Ajax调用:

$("#gelirgetir").click(function() {
                    var gelirtablosecimi = $("#select1").val();
                    if (gelirtablosecimi) {
                        $.ajax({
                            type: "POST",
                            url: "gelir-getdata.php",
                            data: {
                                "gelirtablosecimi": gelirtablosecimi
                            },
                            success: function(result) {
                                var content = '';
                                    var obj = JSON.parse(result);
                                    $.each(obj, function(i, data) {                                        
                                        content += '<tr>';
                                        content += '<td>'+obj[i].tarih+'</td>';
                                        content += '<td>'+obj[i].Toplam+'</td>';
                                        content += '</tr>';
                                    });

                                    $('#gelirtableid tbody').html(content);                                                             
                                notifyUser('success', 'Başarılı!', 'Tablo başarıyla güncellendi');                                
                            },
                            error: function(result) {
                                notifyUser('error', 'Hata', 'error');
                            }
                        });
                    } else {
                        notifyUser('info', 'Dikkat', 'Tablo seçimi yapmadınız!');
                    }

                });
© www.soinside.com 2019 - 2024. All rights reserved.