带有Ignited-Datatables CodeIgniter的Jquery DataTable [英] Jquery DataTable with Ignited-Datatables CodeIgniter

查看:152
本文介绍了带有Ignited-Datatables CodeIgniter的Jquery DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:


我终于找到了此问题的解决方案。如果您遇到与我相同的
问题,您可以尝试访问此将 sServerMethod 更改为POST 。



使用 sServerMethod的JSON响应 GET(获取正确的JSON数据) ,但仍停留在加载服务器数据,如图像)

  {
sEcho:0,
iTotalRecords:10,
iTotalDisplayRecords:10,
aaData:[
[
Munauwar,
Syed,
Mr,
6012345678,
0000-00-00,
basikal
],
[

Je Wei,
Mr,
6012345678,
0000-00-00,
motor
]
],
sColumns:first_name,last_name,salutation,number,birthday,group_name}

使用 sServerMethod 的JSON响应



  {
sEcho :1,
iTotalRecords:10,
iTotalDisplayRecords:0,
aaData:[],
sColumns:first_name,last_name,salutation, number,birthday,group_name}

这是我的javascript代码

  $('#table1')。dataTable({
bProcessing:true,
bServerSide:true,
sPaginationType:bootstrap,
sAjaxSource:config.base_url +contact / popup_contact,
sServerMethod:POST

}

我在联络人控制器中的功能

  function popup_contact()
{
$ this-> datatables
- > select('first_name,last_name,salutation,number,birthday,group_name')
- > from('tb_contact')
- > join('tb_contact_group','tb_contact.contact_group_id = tb_contact_group.contact_group_id');

echo $ this-> datatables-> generate();

}


解决方案

方法仍然不工作,那么它因为你已经设置:$ config ['csrf_protection'] = true; // in your config of Codeigniter



只需在fnServerData调用中添加aoData.push第一行:

 fnServerData:function(sSource,aoData,fnCallback){
aoData.push({name:'<?php echo $ this-> security-> get_csrf_token_name ();?>',value:'<?php echo $ this-> security-> get_csrf_hash();?>'});
$ .ajax({
'dataType':'json',
'type':'POST',
'url':sSource,
'data' :aoData,
'success':fnCallback
});
}


Update:

I finally found the solution for this problem. If you face the same problem as mine, you can try visit this link

I am having problem when want to integrate Jquery DataTables with CodeIgniter Ignited-Datatables library

When I use the default DataTables sServerMethod property which is "GET", I got the json response with data from my php. However since CodeIgniter use post, I stuck at loading server data although the function return me correct json output.

So I follow this guide to change the sServerMethod to "POST". Now I dont stuck at loading server data, but I dont get the data that I need.

JSON response using sServerMethod GET (get correct json, but stuck at loading server data as in the image)

{
"sEcho": 0,
"iTotalRecords": 10,
"iTotalDisplayRecords": 10,
"aaData": [
    [
        "Munauwar",
        "Syed",
        "Mr",
        "6012345678",
        "0000-00-00",
        "basikal"
    ],        
    [
        "Mak",
        "Je Wei",
        "Mr",
        "6012345678",
        "0000-00-00",
        "motor"
    ]
],
"sColumns": "first_name,last_name,salutation,number,birthday,group_name"}

JSON response using sServerMethod POST

{
"sEcho": 1,
"iTotalRecords": 10,
"iTotalDisplayRecords": 0,
"aaData": [],
"sColumns": "first_name,last_name,salutation,number,birthday,group_name"}

This is my javascript code

$('#table1').dataTable({
        "bProcessing": true,
        "bServerSide": true,            
        "sPaginationType": "bootstrap",
        "sAjaxSource": config.base_url + "contact/popup_contact",
        "sServerMethod": "POST"

    });

My function in contact controller

function popup_contact()
{
    $this->datatables
         ->select('first_name,last_name,salutation,number,birthday,group_name')
         ->from('tb_contact')
         ->join('tb_contact_group', 'tb_contact.contact_group_id = tb_contact_group.contact_group_id');          

    echo $this->datatables->generate();             

}

解决方案

If the method above still doesn't work, then its because you have set: $config['csrf_protection'] = true; // in your config of Codeigniter

Simply add the aoData.push line first thing in your fnServerData call:

"fnServerData": function(sSource, aoData, fnCallback) {
            aoData.push({name: '<?php echo $this->security->get_csrf_token_name(); ?>', value: '<?php echo $this->security->get_csrf_hash(); ?>'});
                $.ajax({
                    'dataType': 'json',
                    'type': 'POST',
                    'url': sSource,
                    'data': aoData,
                    'success': fnCallback
                });
            }

这篇关于带有Ignited-Datatables CodeIgniter的Jquery DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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