douguxun6866 2014-07-12 06:26
浏览 36

如何将删除行添加到dataTables jquery而不是HTML / PHP和页面刷新?

I am using the dataTables jQuery plugin to display a table in a wordpress site with data from a custom table and it works well.

I have added select check boxes and once a row or rows have been selected and the Delete button is clicked the form calls an external non-wordpress file to delete the rows corresponding to the IDs in the GET (for me to see the data being passed).

I know dataTables can do this for me instead but I am not good enough at JS to implement it can anyone help?

Also I know I should be using the correct Wordpress $wpdb delete but just found this easier as I am used to doing things outside of wordpress.

Here are the pertinent parts of the files I am using.

<form action="<?php echo plugins_url('process/process-form-delete.php',__FILE__) ; ?>" method="get">
                <table id ="my-parts">
                <thead>
                  <tr>
                    <th>Select</th>
                    <th>Part Image</th>
                    <th>Part Name</th>
                    <th>Vehicle</th>
                    <th>Seller Type</th>
                    <th>Location</th>
                    <th>Price</th>
                    <th>Date</th>
                    <th>View</th>
                  </tr>
                </thead>
                <tbody>
               <?php 
                   foreach ($rows as $row) { 
                           $part_details = pl_get_part_details($row->pl_part_ID); 
                       ?>
                        <tr id="<?php echo $row->my_part_ID; ?>">
                        <td><input name="selector[]" type="checkbox"  value="<?php echo $row->my_part_ID; ?>"  /></td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                        <td> Stuff Here </td>
                          </tr>
                          <?php
                    } ?>
                  </tbody>
                  </table>
                  <button class="btn btn-primary"  name="submit_mult" type="submit">Delete</button>
                  </form>

Process-form-delete.php

<?php
require_once ('../config/dbwpconfig.php');
$number = $_GET['my-parts_length'];
$i=0;
while ($i < $number) { 
$data = $_GET['selector'];
if (isset($data[$i])) {
    $sql = "DELETE FROM wp_pl_my_parts WHERE my_part_ID =  :partID";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':partID', $data[$i], PDO::PARAM_INT);  
    $stmt->execute();


}
$i++;
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>

jQuery to get table to become a datatables table

jQuery(document).ready( function() {
  jQuery('#my-parts').dataTable( {
    "aaSorting": [[2,'asc']],
    "aoColumnDefs": [
      { "bSortable": false, "aTargets": [ 0,8 ] }
    ]
         } );

} );
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?