DataTable服务器端处理PHP

时间:2018-12-06 01:19:00

标签: javascript php datatable datatables

我正在使用DataTable插件,我想实现服务器端处理,问题是我遇到了错误(请参见下文)。我真的不知道我的错误是什么。如何解决此问题,为什么会出现此错误?

  

DataTables警告:表id =零售商表-请求的未知参数'12',用于第0行第12列。有关此错误的更多信息,请参见http://datatables.net/tn/4

示例JSON输出:

  

{“绘制”:0,“ recordsTotal”:100,“ recordsFiltered”:100,“数据”:[[“” 23“,” 4D PLUS“,”“,”“,”“,”“,” “,”“,”“,”“,”“,” 16“],[” 24“,” AARON MARK A. ABALOS / ERLINDA A. ABALOS“,”“,”“,”“,”“,” “,”“,”“,”“,”“,” 16“],[” 3835“,” AB SANDOVAL“,”“,”“,”“,”“,”“,”“,”“, “”,“”,“ 12”]]}

HTML表格:

<table class="table table-striped dataTable no-footer" id="retailer-table">
    <thead>
       <th>#</th>
       <th>Retailer Name</th>
       <th>Retailer Type</th>
       <th>Street</th>
       <th>Barangay</th>
       <th>District</th>
       <th>Town</th>
       <th>Province</th>
       <th>Outlet Name</th>
       <th>Mobile</th>
       <th>Email</th>
       <th>Coordinator</th>
    </thead>
</table>

JS数据表:

$('#retailer-table').DataTable({
    "searching": true,
    "bLengthChange": false,
    "scrollY":"500px",
    "scrollX":"300px",
    "scrollCollapse": true,
    "pageLength": 10,
    "autoWidth": false,
    "processing": true,
    "serverSide": true,
    "ajax": {
        url: "admin-retailer-table-data.php",
        type: "POST"
    },
    "columnDefs": [
        { "width": "30px", "targets": 0 },
        { "width": "200px", "targets": 1 },
        { "width": "150px", "targets": 2 },
        { "width": "150px", "targets": 3 },
        { "width": "150px", "targets": 4 },
        { "width": "120px", "targets": 5 },
        { "width": "110px", "targets": 6 },
        { "width": "150px", "targets": 7 },
        { "width": "150px", "targets": 8 },
        { "width": "80px", "targets": 9 },
        { "width": "80px", "targets": 10 },
        { "width": "150px", "targets": 11 }
    ],
    "language": {
        "emptyTable": "No data available in table",
        "zeroRecords": "No data available in table",
        "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..."
    },
    dom: 'Bfrtip',
    buttons: [
        'csv', 'excel', 'pdf'
    ]
});

PHP代码:

$request = $_REQUEST;

$columns = array(
    0 => 'ContactID',
    1 => 'FileAs',
    2 => 'RetailerType',
    3 => 'PresStreet',
    4 => 'PresBarangay',
    5 => 'PresDistrict',
    6 => 'PresTown',
    7 => 'PresProvince',
    8 => 'Landmark',
    9 => 'Mobile',
    10 => 'Email',
    11 => 'Coordinator'
);

$sql = "SELECT ContactID, FileAs, RetailerType, PresStreet, PresBarangay, PresDistrict, PresTown, PresProvince, Landmark, Mobile, Email, Coordinator FROM tblContacts WHERE ContactType='Retailer' AND tblContacts.Deleted != '1' ORDER BY FileAs LIMIT 100";
$result = mysqli_query($conn, $sql);

$totalData = mysqli_num_rows($result);

$totalFilter = $totalData;

$data = array();

while($row = mysqli_fetch_array($result)){
    $subdata = array();
    $subdata[] = $row[0];
    $subdata[] = $row[1];
    $subdata[] = $row[2];
    $subdata[] = $row[3];
    $subdata[] = $row[4];
    $subdata[] = $row[5];
    $subdata[] = $row[6];
    $subdata[] = $row[7];
    $subdata[] = $row[8];
    $subdata[] = $row[9];
    $subdata[] = $row[10];
    $subdata[] = $row[11];
    $data[] = $subdata;
}

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

print json_encode($json_data);

0 个答案:

没有答案