简体   繁体   English

jQuery DataTable aaColomn无法正常工作

[英]jQuery DataTable aaColomn not working

I use DataTable to display some of my data on my page, using ajax server side. 我使用DataTable在我的页面上显示我的一些数据,使用ajax服务器端。

The js code is like below: js代码如下:

 $('#apply').DataTable({ "bProcessing": true, "aoColumn": [ null, {"mData": "booking_no"}, null, {"mData": "dep_date"}, null, null, null, null, null, null ], "aoColumnDefs": [ { "aTargets": [0], "sClass": "details-control", "bSortable": false, "sDefaultContent": "" }, { "aTargets": [3, 4, 5, 6, 8, 9], "bVisible": false } ], "aaSorting": [[3, 'asc']], "bServerSide": true, "sAjaxSource": "<?php echo plugins_url('mybookings.php', __FILE__); ?>" }); 

The ajaxsource mybooking.php returns values like this: ajaxsource mybooking.php返回如下值:

 { "sEcho": 0, "iTotalRecords": 4, "iTotalDisplayRecords": 4, "aaData": [{ "aid": "1", "booking_no": "24****90", "source": "Booking", "dep_date": "2016\\/**\\/12", "ali_acc": "ty****om", "ali_name": "*\友", "contact": "", "status": "\已\确\认", "statement": "final", "remark": "\大\客\户" }, { "aid": "2", "booking_no": "93****70", "source": "Agoda", "dep_date": "2016\\/**\\/03", "ali_acc": "ty****om", "ali_name": "*\友", "contact": "", "status": "\已\提\交", "statement": "", "remark": "test" }, { "aid": "3", "booking_no": "93****86", "source": "Agoda", "dep_date": "2016\\/**\\/04", "ali_acc": "ty****om", "ali_name": "*\友", "contact": "", "status": "\已\提\交", "statement": "", "remark": "" }, { "aid": "4", "booking_no": "93****35", "source": "Agoda", "dep_date": "2016\\/**\\/30", "ali_acc": "ty****om", "ali_name": "*\友", "contact": "", "status": "\已\提\交", "statement": "", "remark": "" }] } 

The page return a warning: " DataTables warning (table id = 'apply'): Requested unknown parameter '1' from the data source for row 0 ". 该页面返回一个警告:“ DataTables warning(table id ='apply'):从第0行的数据源请求未知参数'1'

I tried and find out that the warning would be gone only if I change the format of aaData from json object into arrays. 我试过并发现只有当我将aaData的格式从json对象更改为数组时,警告才会消失。 That is to say, mybookings.php must return data like this: 也就是说,mybookings.php必须返回如下数据:

 { "sEcho": 0, "iTotalRecords": 4, "iTotalDisplayRecords": 4, "aaData": [ ["1", "24****90", "Booking", "2016\\/**\\/12", "ty****om", "*\友", "", "\已\确\认", "final", "\大\客\户"], ["2", "93****70", "Agoda", "2016\\/**\\/03", "ty****om", "*\友", "", "\已\提\交", "", "test"], ["3", "93****86", "Agoda", "2016\\/**\\/04", "ty****om", "*\友", "", "\已\提\交", "", ""], ["4", "93****35", "Agoda", "2016\\/**\\/30", "ty****om", "*\友", "", "\已\提\交", "", ""] ] } 

I need to use json objects for some of my purpose, But it seems like the aaColumn.mData parameter does not work. 我需要使用json对象来达到我的目的,但似乎aaColumn.mData参数不起作用。 Does anyone know the solution? 有谁知道解决方案?

You are using DataTable not dataTable . 您正在使用DataTable而不是dataTable Those are different. 那些是不同的。

Javascript: 使用Javascript:

$('#apply').DataTable({
    "processing": true,

    "columnDefs": [
        {
            "targets": [0],
            "className": "details-control",
            "orderable": false,
            "defaultContent": ""
        },
        {
            "targets": [3, 4, 5, 6, 8, 9],
            "visible": false
        }
    ],
    "order": [[3, 'asc']],
    "serverSide": true,
    "ajax": "<?php echo plugins_url('mybookings.php', __FILE__); ?>"
});

PHP: PHP:

<?
$result = array();
$result_json = array();
foreach($db as $db_record)
{
  $result[] = $db_record->dataTable_col1;
  $result[] = $db_record->dataTable_col2;
  $result_json["data"][] = $result;
  $result = array();
}
echo json_encode($result_json);
?>

EDIT 编辑

Here are the links for reference for difference between dataTable() & DataTable() 以下是dataTable()DataTable()之间差异的参考链接

https://www.datatables.net/forums/discussion/22577/datatable-or-datatable https://datatables.net/manual/api#Accessing-the-API https://www.datatables.net/forums/discussion/22577/datatable-or-datatable https://datatables.net/manual/api#Accessing-the-API

Finally I use dataTable 1.10.11 instead of 1.9.0, and jquery 1.12.0 instead of 1.7.1 to solve this problem. 最后我使用dataTable 1.10.11而不是1.9.0,而jquery 1.12.0而不是1.7.1来解决这个问题。 Both dataTable() and DataTable() are working. dataTable()和DataTable()都正常工作。

In fact, this is not a perfect solution. 实际上,这不是一个完美的解决方案。 Because newer version does not works good on jQuery makeEditable plugin (an old plugin I ever used, problems with add and remove functions). 因为较新的版本在jQuery makeEditable插件(我曾使用的旧插件,添加和删除功能的问题)上不起作用。 I am just a new programmer, I only have to load different version of plugins on different pages when needed. 我只是一个新的程序员,我只需要在不同的页面上加载不同版本的插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM