weixin_33688840 2016-02-13 08:24 采纳率: 0%
浏览 151

将数据附加到Tbody AJAX

I have a foreach statement that is adding the data from a DB to my table rows

HTML

<table id="myTable" class="display table center-table" width="100%" >
   <thead>  
          <tr>  
            <th>Product #</th>  
            <th>Alternate #</th>  
            <th>Description</th>  
            <th>On Hand</th>  
        <th>Condition</th>
          </tr>  
        </thead>  

    <tbody id="productResults"> </tbody>

</table>

PHP

$query =  $sql . " limit " . $start . "," . $perPage; 
  $data = $db_handle->runQuery($query);

  if(empty($_GET["rowcount"])) {
  $_GET["rowcount"] = $db_handle->numRows($sql);
  }
  $pages  = ceil($_GET["rowcount"]/$perPage);
    $output = '';
  if(!empty($data)) {
  $formval = '<input type="hidden" class="pagenum" value="' . $page . '" /><input type="hidden" class="total-page" value="' . $pages . '" />';
  foreach($data as $k=>$v) {
  $output .= '<tr><td>' . $formval . $data[$k]["wuno_product"] . '</td>';
  $formval = '';
  $output .= '<td>' . $data[$k]["wuno_alternates"] . '</td>';
  $output .= '<td>' . $data[$k]["wuno_description"] . '</td>';
  $output .= '<td>' . $data[$k]["wuno_onhand"] . '</td>';
  $output .= '<td>' . $data[$k]["wuno_condition"] . '</td></tr>';     
}
}
echo $output;

Then in my user view I am adding the data back to the page like this,

JQUERY

<script>

(function($) {
$(document).ready(function(){
    function getresult(url) {
    $.ajax({
          url: url,
          type: "GET",
          data:  { rowcount:$("#rowcount").val() },
          beforeSend: function(){
          $('#loader-icon').show();
        },
        complete: function(){
            $('#loader-icon').hide();
        },
        success: function(data){/* convert to dom element */
            $("#productResults").append( data.toElement() );
        },
        error: function(){}             
   });
}

    $(window).scroll(function(){
        if ($(window).scrollTop() == $(document).height() - $(window).height()){
            if($(".pagenum:last").val() <= $(".total-page").val()) {
                var pagenum = parseInt($(".pagenum:last").val()) + 1;
                getresult('<?php echo $assetPath; ?>?page='+pagenum);
            }
        }
    }); 
});
})( jQuery );

</script>

But when it shows up in my page it shows up outside of the table and does not have the html added to it. It looks like this in the page,

8855K5MS21026-B2111212M39029/5-1171313Q4559PROD CODE: 4057911RESTOCKING CHARGE11TAS8732-1C277TEST REPORTS6690H549(W) LAMP CKT99MEC-1MF1 FEET DB9M/F CABLE11T1-GMIL-L-25567E1 GAL JUG2121WL-322914VOLT T-1 BULB100100

Which was the data from the DB just all together... Why would this happen and how can I fix it? It shows up above the table instead.

  • 写回答

1条回答 默认 最新

  • ~Onlooker 2016-02-13 08:37
    关注

    firstly, you are attempting to add to a table, but the first portion of your generated code is a form element outside the table structure; any modern browser is going to spot the mistake and add in the assumed missing

      </table> 
    

    which is why the response is outside a table view.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog