droxlzcgnr639823 2015-02-17 07:10
浏览 86

如何在jquery数据表中追加行

I want to append rows to datatable. Rows are coming through ajax calls.

This is js code:

$(document).ready(function() {
    $('#example').DataTable({
        "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
        "iDisplayLength": 100,
    });
    $.ajax({
        type: "GET",
        url: "server_processing.php",
        data: {start_from: 200},
        success: function(response) {
            $("#example").append(response);

        }
    });
});

And this is php code which is returning rows to append:

$start_from = $_GET['start_from'];
$sql = "SELECT * FROM `backplanechanneldecoder20141002` LIMIT $start_from,1";
$result = mysql_query($sql, $conn);
if ($result === FALSE)
{

}
else
{
    while ($row = mysql_fetch_array($result))
    {
        ?>

        <tr>
            <td><?php echo $row['Backplane_Connector'] ?></td>
            <td><?php echo $row['Total_len_in'] ?></td>
            <td><?php echo $row['Z0'] ?></td>
            <td><?php echo $row['Riser_1_material'] ?></td>
            <td><?php echo $row['Riser_1_len'] ?></td>
            <td><?php echo $row['Riser_1_len_in'] ?></td>
            <td><?php echo $row['BP_material'] ?></td>
            <td><?php echo $row['BP_backdrill'] ?></td>
            <td><?php echo $row['BP_layer'] ?></td>
            <td><?php echo $row['BP_len_in'] ?></td>
            <td><?php echo $row['Riser_2_material'] ?></td>
            <td><?php echo $row['Riser_2_len'] ?></td>
            <td><?php echo $row['Riser_2_len_in'] ?></td>
            <td><?php echo $row['Pair'] ?></td>
            <?php
            $file_array = explode("/", $row['files']);
            ?>
            <td>
                <?php
                $arry_length = count($file_array);
                $last_array = 0;
                foreach ($file_array as $file_row)
                {
                    if ($file_row != '')
                    {
                        $last_array++;
                        if ($arry_length == $last_array)
                        {
                            ?>
                            <a href="<?php echo $row['files'] ?>" ><?php echo $file_row; ?></a>
                            <?php
                        }
                    }
                    else
                    {
                        ?>
                         <a  class="sendmail" href="mailto:umair.malik@purelogics.net?Subject=Requesting measurement file for the following parameters&body=Backplane Connector = <?php echo $row['Backplane_Connector'] ?> %0ATotal len (in) = <?php echo $row['Total_len_in'] ?> %0AZ0 = <?php echo $row['Z0'] ?>%0ARiser 1 material = <?php echo $row['Riser_1_material'] ?>%0ARiser 1 len = <?php echo $row['Riser_1_len'] ?>%0ARiser 1 len (in) = <?php echo $row['Riser_1_len_in'] ?>%0ABP material = <?php echo $row['BP_material'] ?>%0ABP backdrill = <?php echo $row['BP_backdrill'] ?>%0ABP layer = <?php echo $row['BP_layer'] ?>%0ABP len (in) = <?php echo $row['BP_len_in'] ?>%0ARiser2 material = <?php echo $row['Riser_2_material'] ?>%0ARiser2 len = <?php echo $row['Riser_2_len'] ?>%0ARiser2 len (in) = <?php echo $row['Riser_2_len_in'] ?>%0APair = <?php echo $row['Pair'] ?>" >Click here to request</a>
                        <?php
                    }
                }
                ?>

            </td>
        </tr>

        <?php
    }
  • 写回答

1条回答 默认 最新

  • douhui5953 2015-02-17 07:28
    关注

    Try with this -

    Define the datatable initialisation -

    $(document).ready(function(){
      var oTable = $('#tableid').dataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "yourpage.php",
        "aLengthMenu": [[10, 25, 50, 75, -1], [10, 25, 50, 75, "All"]],
        "iDisplayLength": 10,
      });
    });
    

    The HTML table will be -

    <table id="tableid">
        <tr>
           <td>Sr</td>
           <td>Name</td>
           .....
        </tr>
    </table>
    

    And yourpage will be -

    $start  = $_GET['start'];
    $length = $_GET['length']; 
    $sql = "SELECT * FROM backplanechanneldecoder20141002 LIMIT $start,$length"; 
    $result = mysql_query($sql, $conn); 
    $response = array();
       $response['recordsTotal']    = //needed values;
       $response['recordsFiltered'] = //needed values;
       if ($result) {
           while ($row = mysql_fetch_array($result))
           {
               $temp = array();
               $temp['sr'] = //a serial number;
               ....
           }
           $response[] = $temp
       }
       echo json_encode($response);
       exit;
    

    Remember to return same values as table needed or you have to process them manually. And try to use mysqli or PDO instead of mysql.

    For reference - Server Side Processing

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算