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

时间:2018-04-16 20:26:12

标签: php jquery ajax bootstrap-table

我的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按钮时,它会更新我的一个会话值,而我的表依赖于该会话值。会话值更改后,如果我重新加载页面,将显示新值,但我必须强制刷新页面。我只想刷新表格。有什么建议吗?

2 个答案:

答案 0 :(得分: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!');
    }

});

答案 1 :(得分: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!');
                    }

                });