如何解决数据表重新初始化

时间:2019-05-04 00:54:49

标签: php jquery ajax datatables

我已经解决了好几天的问题(准确地说是5天),我收到此错误。

  

DataTables警告:表ID =活动表-无法重新初始化DataTable。有关此错误的更多信息,请参见http://datatables.net/tn/3

这是我完整的Jquery代码,此代码在将数据表分离到一个函数中后就初始化了数据表,以便组织起来:

$(document).ready(function() { 
    initialize_table();
});

function initialize_table(){
    var salesman = $("#salesman-filter").val();
    var supervisor = $("#supervisor-filter").val();
    var startdate = $("#start-date").val();
    var enddate = $("#end-date").val();

    var columns = [
        { "width": "80px", "targets": 0 },
        { "width": "200px", "targets": 1 },
        { "width": "200px", "targets": 2 }, 
        { "width": "200px", "targets": 3 },
        { "width": "200px", "orderable": false, "targets": 4 },
        { "width": "200px", "targets": 5 },
        { "width": "100px", "targets": 6 },
        { "width": "110px", "targets": 7 },
        { "width": "150px", "orderable": false, "targets": 8 },
        { "width": "150px", "orderable": false, "targets": 9 },
        { "width": "150px", "orderable": false, "targets": 10 },
        { "width": "150px", "orderable": false, "targets": 11 },
        { "width": "150px", "orderable": false, "targets": 12 }
    ];

    var table = $('#activities-table').DataTable({
        "serverSide": true,
        "bprocessing": true,
        retrieve: true,
        "ajax": {
            url: "activities-data.php",
            type: "POST",
            data: {salesman:salesman, startdate:startdate, enddate:enddate, supervisor:supervisor},
            error: function(data){
                console.log(data);
            }
        },
        "columnDefs": columns,
        "language": {
            "emptyTable": "No data found",
            "zeroRecords": "No data found",
            "info": "Showing <b>_START_</b> to <b>_END_ of _TOTAL_</b> entries",
            "paginate": {
                "first":      "First",
                "last":       "Last",
                "next":       "Next",
                "previous":   "Previous"
            },
            search: "_INPUT_",
            searchPlaceholder: "Search..."
        }
    });
}

接下来这是我的PHP代码,在这里我获取数据表的数据,此代码仅返回一(1)个数据:

<?php 
include "session.php";
include "connect.php";

$params = $columns = $totalRecords = $data = array();
$params = $_REQUEST;


$salesman = $_POST["salesman"];
$supervisor = $_POST["supervisor"];
$startdate = $_POST["startdate"];
$enddate = $_POST["enddate"];

$whereCondition = $sqlTotal = $sqlRecord = ""; 

$columns = array(
    0 => 'tblCaf.CAFNo',
    1 => 'EmployeeName',
    2 => 'CustomerName',
    3 => 'ContactPerson',
    4 => 'Activity',
    5 => 'tblCaf.ActivityDate',
    6 => 'tblCaf.StartTime',
    7 => 'tblCaf.EndTime',
    8 => 'tblCaf.StartLocation',
    9 => 'tblCaf.EndLocation',
    10 => 'tblCaf.OtherConcern',
    11 => 'tblCaf.tblCaf.Remarks'
);

if(!empty($params['search']['value'])){
    $whereCondition  .= " AND 
        (tblCaf.CAFNo LIKE '%".$params['search']['value']."%'
        OR EmployeeName.FileAs LIKE '%".$params['search']['value']."%'
        OR CustomerName.FileAs LIKE '%".$params['search']['value']."%'
        OR ContactPerson.FileAs LIKE '%".$params['search']['value']."%')";
}
if(!empty($supervisor) && empty($salesman)){
    $sql = "SELECT TOP 50 EmployeeName.FileAs AS Emp, CustomerName.FileAs AS CUS, ContactPerson.FileAs AS CP, CAFNo, CAFDate, StartTime, EndTime, StartLocation, EndLocation, OtherConcern, tblCaf.Remarks
    FROM tblCaf
    LEFT OUTER JOIN tblContacts AS EmployeeName ON EmployeeName.ContactID = tblCaf.EmployeeID
    LEFT OUTER JOIN tblContacts AS CustomerName ON CustomerName.ContactID = tblCaf.CustomerID
    LEFT OUTER JOIN tblContacts AS ContactPerson ON ContactPerson.ContactID = tblCaf.ContactPersonID
    LEFT OUTER JOIN tblSalesmanSupervisor ON tblSalesmanSupervisor.SalesmanID = tblCaf.EmployeeID
    WHERE CAFDate BETWEEN :start AND :end AND SupervisorID = :filter";
}
else if(!empty($salesman)){
    $sql = "SELECT TOP 50 EmployeeName.FileAs AS Emp, CustomerName.FileAs AS CUS, ContactPerson.FileAs AS CP, CAFNo, CAFDate, StartTime, EndTime, StartLocation, EndLocation, OtherConcern, tblCaf.Remarks
    FROM tblCaf
    LEFT OUTER JOIN tblContacts AS EmployeeName ON EmployeeName.ContactID = tblCaf.EmployeeID
    LEFT OUTER JOIN tblContacts AS CustomerName ON CustomerName.ContactID = tblCaf.CustomerID
    LEFT OUTER JOIN tblContacts AS ContactPerson ON ContactPerson.ContactID = tblCaf.ContactPersonID
    LEFT OUTER JOIN tblSalesmanSupervisor ON tblSalesmanSupervisor.SalesmanID = tblCaf.EmployeeID
    WHERE CAFDate BETWEEN :start AND :end AND EmployeeID = :filter";
}
else {
    $sql = "SELECT TOP 50 EmployeeName.FileAs AS Emp, CustomerName.FileAs AS CUS, ContactPerson.FileAs AS CP, CAFNo, CAFDate, StartTime, EndTime, StartLocation, EndLocation, OtherConcern, tblCaf.Remarks
    FROM tblCaf
    LEFT OUTER JOIN tblContacts AS EmployeeName ON EmployeeName.ContactID = tblCaf.EmployeeID
    LEFT OUTER JOIN tblContacts AS CustomerName ON CustomerName.ContactID = tblCaf.CustomerID
    LEFT OUTER JOIN tblContacts AS ContactPerson ON ContactPerson.ContactID = tblCaf.ContactPersonID
    LEFT OUTER JOIN tblSalesmanSupervisor ON tblSalesmanSupervisor.SalesmanID = tblCaf.EmployeeID
    WHERE CAFDate BETWEEN :start AND :end";
}

$sqlTotal .= $sql;
$sqlRecord .= $sql;

if(isset($whereCondition) && $whereCondition != ""){
    $sqlTotal .= $whereCondition;
    $sqlRecord .= $whereCondition;
}

$sqlRecord .= " ORDER BY ". $columns[$params['order'][0]['column']] ." " . $params["order"][0]['dir'];

if(!empty($supervisor) && empty($salesman)){
    $resultTotal = $conn->prepare($sqlTotal, $cursor);
    $resultTotal->bindValue(":start", $startdate);
    $resultTotal->bindValue(":end", $enddate);
    $resultTotal->bindValue(":filter", $supervisor);
    $resultTotal->execute();
}
else if(!empty($salesman)){
    $resultTotal = $conn->prepare($sqlTotal, $cursor);
    $resultTotal->bindValue(":start", $startdate);
    $resultTotal->bindValue(":end", $enddate);
    $resultTotal->bindValue(":filter", $salesman);
    $resultTotal->execute();
}
else{
    $resultTotal = $conn->prepare($sqlTotal, $cursor);
    $resultTotal->bindValue(":start", $startdate);
    $resultTotal->bindValue(":end", $enddate);
    $resultTotal->execute();
}

$totalData = $resultTotal->rowCount();

$data = array();
$subdata = array();

$activities = "";

if($totalData > 0){
    while($row = $resultTotal->fetch()){
        $subdata[] = $row["CAFNo"];
        $subdata[] = $row["CAFNo"];

        $subdata[] = strtoupper($row["Emp"]);
        $subdata[] = strtoupper($row["CUS"]);
        $subdata[] = strtoupper($row["CP"]);

        $multiplesql = $conn->prepare("SELECT ActivityID FROM tblCafActivity WHERE CAFNo = :caf", $cursor);
        $multiplesql->bindValue(":caf", $row["CAFNo"]);
        $multiplesql->execute();

        $multiplecount = $multiplesql->rowCount();  

        if($multiplecount > 0){
            while($multiplerow = $multiplesql->fetch()){
                $activitysql = $conn->prepare("SELECT ActivityDescription FROM tblActivity WHERE ActivityID = :act", $cursor);
                $activitysql->bindValue(":act", $multiplerow["ActivityID"]);
                $activitysql->execute();
                $activitycount = $activitysql->rowCount();

                if($activitycount > 0){
                    while($activityrow = $activitysql->fetch()){
                        $activities = strtoupper($activityrow["ActivityDescription"]) . "<br/>";
                    }
                }
            }
        }

        $subdata[] = $activities;
        $subdata[] = date("F j, Y", strtotime($row['CAFDate']));
        $start = new DateTime($row['StartTime']);
        $startformat = $start->format('H:i:s a');
        $subdata[] = $startformat;


        if(empty($row['EndTime'])){
            $subdata[] = "";
        }
        else {
            $end = new DateTime($row['EndTime']);
            $endformat = $end->format('H:i:s a');
            $subdata[] = $endformat;
        }

        $subdata[] = $row['StartLocation'];
        $subdata[] = $row['EndLocation'];

        $subdata[] = strtoupper($row["OtherConcern"]);
        $subdata[] = strtoupper($row["Remarks"]);

        $data[] = $subdata;
    }
}

$json_data = array(
    "draw" => intval($params['draw']),
    "recordsTotal" => intval($totalData),
    "recordsFiltered" => intval($totalData),
    "data" => $data,
    "sql" => $sql
);

print json_encode($json_data);

?>

我尝试添加以下内容,但错误仍然不断出现

  

检索:是
  销毁:正确

是否可以解决此问题?并同时避免?

1 个答案:

答案 0 :(得分:0)

您应该销毁表并重新初始化

添加此代码:。

$('#activities-table').dataTable().fnDestroy();

之前:

var table = $('#activities-table').DataTable({.....