【问题标题】:Display data in table on ajax success [duplicate]在ajax成功的表中显示数据[重复]
【发布时间】:2019-03-27 07:32:57
【问题描述】:

我正在从 ajax 提交表单,并且在 ajax 成功后我想显示表 tr td 中的所有数据。

以下是我在 ajax 成功时得到的回复,我想在 ajax 成功时加载表格,但它显示的是空白表格。

{"raildata":null,"killdata":[{"id":146,"acct_id":1885,"AcctNo":"UP2357"},{"id":145,"acct_id":1885,"AcctNo":"UP2357"}]}

以下是我尝试过的 jquery ajax 代码,但表单提交成功后它不显示数据。

$(document).ready(function(){
    $('#killfrm').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '<?= Router::url(['controller' => 'Killsheets', 'action' => 'addKillsheet']) ?>',
            data: $('form').serialize(),
            beforeSend: function(){
                $('#ibox1').children('.ibox-content').toggleClass('sk-loading');
            },
            success: function(response) {
                    var trHTML = ''; 
                    $(response).each(function (i,value) {

                        trHTML += response.killdata.map(function(killdata) {
                          return '<tr class="gradeA"><td>' + killdata.id + '</td><td>' + killdata.AcctNo + '</td></tr>';
                        });

                        trRailHTML += response.raildata.map(function(raildata) {
                          return '<tr class="gradeA"><td>' + raildata.rail_no + '</td><td>' + raildata.scale_no + '</td><td><button title="View" class="btn btn-default btn btn-xs tblbtn">View</button></td></tr>';
                        }); 

                    });
                $('#txtcount').val(sum);    
                $('#listRail').html(trRailHTML);                                 
                $('#listKill').html(trHTML);

        },
            error: function(response) {         
                console.log(response);          
            }        
        });
    });
});

下面是我的 HTML 表格

 <table class="table table-bordered">
                                        <thead>
                                           <tr>
                                            <th>Sheet#</th>
                                            <th>Acc #</th>
                                             <th>Action</th>
                                          </tr>
                                        </thead>                                         
                                        <tbody id="listKill"> </tbody>
                                  </table>

【问题讨论】:

  • 为什么不使用数据表? datatables.net
  • 你能帮我解决这个问题吗?当我在文本框模糊事件上尝试相同的代码时,它会为我工作,但在 ajax 成功后相同的代码将无法工作。 bcz 我正在提交表单,提交后我想在表格中显示所有数据库数据。

标签: php jquery ajax


【解决方案1】:

你可以通过$.each: 脚本:

$(document).ready(function(){
    $('#killfrm').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '<?= Router::url(['controller' => 'Killsheets', 'action' => 'addKillsheet']) ?>',
            data: $('form').serialize(),
            beforeSend: function(){
                $('#ibox1').children('.ibox-content').toggleClass('sk-loading');
            },
            success: function(response) {
                    let trHTML = ''; 
                    let killData = response.killdata;
                    let raildata = response.raildata;

                    $.each(killData, function(kill) {
                        let killid = kill.id;
                        let killacct_id = kill.acct_id;
                        let killAcctNo = kill.AcctNo;
                        trHTML += '<tr>';
                        trHTML += '<td>'+killid+'</td>';
                        trHTML += '<td>'+killacct_id+'</td>';
                        trHTML += '<td>'+killAcctNo+'</td>';
                        trHTML += '</tr>';
                    });


                $('#listRail').html(raildata);                                 
                $('#listKill').html(trHTML);

            },
            error: function(response) {         
                console.log(response);          
            }        
        });
    });
});

您会在#listKill 的id 中获得结果html trHTML

html:

<table class="table table-bordered">
<thead>
<tr>
<th>Sheet#</th>
<th>Acc #</th>
<th>Action</th>
</tr>
</thead>                                         
<tbody id="listKill">result goes here </tbody>
</table>

您可以从response.objectkey = get value获取任何响应数据

【讨论】:

    【解决方案2】:

    要在 ajax 响应中获取 json 数据,您需要传递 ajax 选项:

    dataType:"json"
    

    请看下面的例子:

    $.ajax({
            type: 'post',
            url: 'Your Url',
            data: $('form').serialize(),
            dataType:'json',
            success: function(response) {
              response == here you get object you can get any value by object key
    
            }
           });
    

    您的 json 响应:

    {"raildata":null,"killdata":[{"id":146,"acct_id":1885,"AcctNo":"UP2357"},{"id":145,"acct_id":1885,"AcctNo":"UP2357"}]}
    

    您可以通过使用response 变量得到结果,如下所示

    console.log(response.killdata);
    console.log(response.raildata);
    

    如果您有任何帮助,请尝试以上方法

    【讨论】:

    • 是的,我错过了输入数据类型:json,非常感谢。但是如何用我添加的新数据刷新表,因为它没有添加新的响应
    • 好的,我会为此添加新的答案..
    【解决方案3】:

    表体是这样附加的:

    Assin id 到你的桌子

    <table class="table table-bordered" id='my_table'>
    

    添加此行以添加新行

    $("#my_table tbody").append(trHTML);
    

    【讨论】:

    • 已经尝试过,但在我的情况下不起作用
    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2019-11-29
    • 2020-06-09
    • 2013-07-26
    相关资源
    最近更新 更多