数据表和Ajax数据格式化? [英] Datatables and ajax data formatting?

查看:106
本文介绍了数据表和Ajax数据格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据表,并且希望能够发送AJAX请求以获取我的数据.

I am using Datatables and I want to be able to send an AJAX request to get my data.

我的jQuery-

$('.valid-tags').DataTable( {
    "ajax": {
        "url": "/ajax/getValidTags.php",
        "type": "POST",
        "data": {
            ruleID: ruleID
        }
    }
} );

这是从ajax请求返回的数据-

{"data":["K":2,"B":1,"C":2]}

我希望在自己的行中的标签名称下看到"K","B","C".

Im expecting to see 'K', 'B', 'C' under tag name in their own rows.

我的数据表还没有加载任何数据吗?

My Datatables doesn't load any data though?

我需要能够将每个键值对包装在其自己的数组中,这样-

I need to be able to wrap each key value pair in its own array so this -

{"data":["K":2,"B":1,"C":2]}

将会-

{"data":[["K":2],["B":1],["C":2]]}

这是构建它的PHP(我在哪里将键值包装在对象中,就像上面那样)?-

This is the PHP that builds it (where do i wrap the key values in an object so it like the above)?-

$validTagsArray = array();
$validArray = array();

foreach ($cursor as $key => $value) {
  foreach ($value['AutoFix'] as $k => $v) {
    $x = 0;
    foreach ($v as $key => $value) {
      $x++;
      $validValueCount = $validTagsArray[$k] = $x;
    }
  }
}

$validArray['data'] = array($validTagsArray);


echo json_encode($validArray);

推荐答案

更改返回的JSON格式,如下所示.有关更多信息,请参见数据源类型.

Change the format of the JSON that you're returning as shown below. See Data source types for more information.

{
    "data": [
        [ "K", 2 ],
        [ "B", 1 ],
        [ "C", 2 ]
    ]
}

如下所示更改初始化选项.

Change your initialization options as shown below.

$('.valid-tags').DataTable( {
    "ajax": {
        "url": "/ajax/getValidTags.php",
        "type": "POST",
        "data": {
            ruleID: ruleID
        }
    },
    "columnDefs": [{
       "targets": 2,
       "render": function(data, type, full, meta){
          return '<button type="button">Manage</button>';
       }
    }]
} );

有关代码和演示,请参见此jsFiddle .

See this jsFiddle for code and demonstration.

这篇关于数据表和Ajax数据格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆