如何在jquery数据表中追加行


How to append rows in jquery datatable

我想将行追加到数据表中。行通过ajax调用而来。

这是js代码:

$(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);
        }
    });
});

这是返回要附加的行的php代码:

$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
    }

试试这个-

定义数据表初始化-

$(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,
  });
});

HTML表格将为-

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

并且yourpage将是-

$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;

请记住,返回与表所需的值相同的值,否则必须手动处理它们。并尝试使用mysqliPDO而不是mysql

供参考-服务器端处理