数据服务器端使用Json / Ajax示例工作(但不是CRUD按钮)

时间:2018-04-16 21:51:01

标签: php jquery json ajax datatable

简介:我正在使用json,ajax和ssp.class.php做服务器端的datatable.net Jquery插件。我有它的工作,但试图制作可以编辑和删除的按钮。我能够在没有插件的情况下做到这一点(但由于某种原因,我目前难倒)。

代码:

   $(document).ready(function() {
        $('#example').DataTable( {
           // "pagingType": "scrolling",
            "processing": true,
            "serverSide": true,

                "ajax": {
                    "url": "server.php",
                    "type": "POST"
                },
         });

     }); 
    </script>
      <body>

    <table id="example" class="display" style="width:100%" class="table table-striped table-bordered table-hover table-condensed">
     <thead class="thead-inverse">
    <tr>
    <th><a class="column_sort" id="id" href="?order=id&sort=<?php echo $sort; ?>">
            ID
    </th>
    <th><a class="column_sort" id="first_name" data-order="<?php echo $sort;?>" href="?order=first_name&sort=<?php echo $sort; ?>">First Name </a>
    </th>
    <th><a class="column_sort" id="last_name" href="?order=last_name&sort=<?php echo $sort; ?>">Last Name
    </a>
    </th>
    <th><a class="column_sort" id="position" href="?order=position&sort=<?php echo $sort; ?>">Position
    </a>
    </th>
    <th class="hidden-xs"><a class="column_sort" id="date" href="?order=date&sort=<?php echo $sort; ?>">Date </a>
    </th>
    <th class="hidden-xs"><a class="column_sort" id="updated" href="?order=updated&sort=<?php echo $sort; ?>">Updated
    </a> </th>
    <th>Action</th>
    </thead> </tr>
            <tbody>

            </tbody>
        </table>
        </div>          
    <?php

    $orderby="";
    $sort="";
    $sort = isset($_GET['sort']) ? $_GET['sort'] : 'ASC';
    $sort = ($sort == 'ASC') ? 'DESC': 'ASC';

    $order  = array("first_name","last_name", "date", "position", "updated"); 
    $key     = array_search($sort, $order); 
    $orderby = $order[$key];
    $records = mysqli_query($con, "SELECT * FROM employees ORDER BY $orderby $sort");

    $data=array();
    while ($row = mysqli_fetch_array($records, MYSQLI_ASSOC)) { 
        $row['delete_button']='<button type="button" class="btn btn-warning">Delete</button>';
        $data[]=$row;
    }
    $requestData= $_REQUEST;

    $count=mysqli_query($con, "SELECT * FROM employees"); 
    $totalData= $count->num_rows;
    $totalFiltered=$totalData;

    $json_data = array(
                      "draw" => intval(isset($_GET['draw'])), 
                      "recordsTotal"    => intval( $totalData ), 
                      "recordsFiltered" => intval( $totalFiltered ),
                      "data"            => $data //How To Retrieve This Data
                     );

    echo json_encode($json_data);
    ?>

错误: DataTables警告:table id = example - 第0行第6列请求的未知参数“6”。有关此错误的详细信息,请参阅http://datatables.net/tn/4

已经有一个类似的话题,似乎没有人有任何线索。任何帮助,将不胜感激。提前致谢。

1 个答案:

答案 0 :(得分:0)

这是解决方案。我不得不使用columnDef:

$(document).ready(function() {
var asc = true;
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "server.php",
"type": "POST"
},

columnDefs: [{
"searchable": true
targets: -1,
defaultContent: '<button type="button">Delete</button>'
}],
});
});