如何在jquery数据表中追加行

时间:2015-02-17 07:10:39

标签: php ajax datatable

我想将行追加到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
    }

1 个答案:

答案 0 :(得分:0)

试试这个 -

定义数据表初始化 -

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

供参考 - Server Side Processing