获取错误TypeError:a在数据表导出自定义函数中未定义

时间:2018-03-10 06:43:55

标签: datatable datatables

我需要导出datatable中的所有数据,所以为此我已经为导出按钮添加了ajax调用,当我点击导出按钮时,它会激活ajax调用并获取响应中的所有数据但之后在控制台中调用ajax我收到错误TypeError: a is undefined,任何人都可以帮我解决这个问题,这是我的代码

Jquery:

function cmpTable() {
        var buttonCommon = {
            exportOptions: {
                format: {
                    body: function (data, row, column, node) {
                        if (column == '1' || column == '2' || column == '3') {
                            var data = data.replace(/(<([^>]+)>)/ig, "")
                            data = data.replace('$', '');
                            return data.replace('%', '');
                        } else {
                            var data = data.replace(/(<([^>]+)>)/ig, "")
                            return data;
                        }
                    }
                }
            }
        };
        var branch = $("#branch").val();
        var soc_start_date = $("#soc_start_date").val();
        var soc_end_date = $("#soc_end_date").val();
        var ot_start_range = $("#ot_start_range").val();
        var ot_end_range = $("#ot_end_range").val();
        var ep_start_range = $("#ep_start_range").val();
        var ep_end_range = $("#ep_end_range").val();
        var st_start_range = $("#st_start_range").val();
        var st_end_range = $("#st_end_range").val();
        var hha_start_range = $("#hha_start_range").val();
        var hha_end_range = $("#hha_end_range").val();
        var total_start_range = $("#total_start_range").val();
        var total_end_range = $("#total_end_range").val();
        var msw_start_range = $("#msw_start_range").val();
        var msw_end_range = $("#msw_end_range").val();
        var sn_start_range = $("#sn_start_range").val();
        var sn_end_range = $("#sn_end_range").val();
        var icd_start_range = $("#icd_start_range").val();
        var icd_end_range = $("#icd_end_range").val();
        var pt_start_range = $("#pt_start_range").val();
        var pt_end_range = $("#pt_end_range").val();
        var ep_end_start_range = $("#ep_end_start_range").val();
        var ep_end_end_range = $("#ep_end_end_range").val();
        var random_episode = $("#random_episode").val();

        var page_length = 10;
        if (random_episode != '') {
            page_length = random_episode;
        }

        $('#cmp_table2').DataTable({
            dom: 'Bfrtip',
            searching: false,
            "pageLength": page_length,
            "processing": true,
            "serverSide": true,
            "aaSorting": [[ 3, "asc" ], [ 4, "asc" ]],
            "destroy":true,
            'ajax': {
                "url": '<?php echo site_url('adhoc/adhoc_data'); ?>',
                "type": 'POST',
                data: {
                    branch: branch,
                    soc_start_date:soc_start_date,
                    soc_end_date:soc_end_date,
                    ot_start_range:ot_start_range,
                    ot_end_range:ot_end_range,
                    ep_start_range:ep_start_range,
                    ep_end_range:ep_end_range,
                    st_start_range:st_start_range,st_end_range:st_end_range,
                    hha_start_range : hha_start_range,
                    hha_end_range:hha_end_range,total_start_range:total_start_range,
                    total_end_range:total_end_range,
                    msw_start_range:msw_start_range,msw_end_range:msw_end_range,
                      sn_start_range : sn_start_range,sn_end_range:sn_end_range,icd_start_range:icd_start_range,
                      icd_end_range:icd_end_range,pt_start_range:pt_start_range,pt_end_range:pt_end_range,
                      ep_end_start_range:ep_end_start_range,ep_end_end_range:ep_end_end_range,random_episode:random_episode
                  },
            },
            "columns": [
                { "data": "display_name"},
                { "data": "LastName"},
                { "data": "MRN"},
                { "data": "SOCDate"},
                { "data": "EpStart"},
                { "data": "EpEnd"},
                { "data": "ICD"},
                { "data": "PhyLastName"},
                { "data": "SNVisits"},
                { "data": "HHAVisits"},
                { "data": "MSWVisits"},
                { "data": "PTVisits"},
                { "data": "OTVisits"},
                { "data": "STVisits"},
                { "data": "TotVisits"},
            ],
            buttons: [
                $.extend(true, {}, buttonCommon, {
                    extend: 'excelHtml5', 
                    className: 'start_class', 
                    text: 'Export', 
                    title: 'Target Starting Episodes',
                    action: function (e, dt, node, config) {

                        $.ajax({
                        url: '<?php echo site_url('adhoc/adhoc_data'); ?>',
                        method: 'POST',
                        data: {
                            branch: branch,
                            soc_start_date:soc_start_date,
                            soc_end_date:soc_end_date,
                            ot_start_range:ot_start_range,
                            ot_end_range:ot_end_range,
                            ep_start_range:ep_start_range,
                            ep_end_range:ep_end_range,
                            st_start_range:st_start_range,st_end_range:st_end_range,
                            hha_start_range : hha_start_range,
                            hha_end_range:hha_end_range,total_start_range:total_start_range,
                            total_end_range:total_end_range,
                            msw_start_range:msw_start_range,msw_end_range:msw_end_range,
                            sn_start_range : sn_start_range,sn_end_range:sn_end_range,icd_start_range:icd_start_range,
                            icd_end_range:icd_end_range,pt_start_range:pt_start_range,pt_end_range:pt_end_range,
                            ep_end_start_range:ep_end_start_range,ep_end_end_range:ep_end_end_range,random_episode:random_episode,
                            type : 'all'
                        }
                        }).then(function (ajaxReturnedData) {
                        dt.rows.add(ajaxReturnedData.data).draw();
                        $.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);
                        });
                    }
                }),
            ]
        });
    }

控制器操作:

if(is_use_azure()) {
                    $otherdb->select('tb_Branch.description as display_name,LastName,MRN,SOCDate,EpStart,EpEnd,ICD,PhyLastName,SNVisits,HHAVisits,MSWVisits,PTVisits,
                OTVisits,STVisits,TotVisits');
                    $otherdb->from('tb_Episode, tb_Branch');
                    if ($is_random_episodes) {
                            $otherdb->order_by("RAND()");
                    } else {
                            $otherdb->order_by($column_name,$order_dir);
                    }
                    $otherdb->limit($limit,$offset);
                    if($where !='') {
                            $otherdb->where($where,NULL, FALSE);
                    }

                    if($branch !='') {  
                        //$otherdb->where_in('BranchID',$_POST['branch'],false);
                    }
                    $episode_data = $otherdb->get();
                    $episode_data = $episode_data->result_array();


                    $otherdb->select('count(*) as total');
                    $otherdb->from('tb_Episode, tb_Branch');
                    if($where !='') {
                            $otherdb->where($where,NULL, FALSE);
                    }
                    if($branch !='') {
                        $otherdb->where_in('BranchID',$_POST['branch'],false);
                    }  
                    $count_data = $otherdb->get();
                    //echo $otherdb->last_query(); die;
                    $count_data = $count_data->result_array();
                } else {
                    $this->db->select('tb_Branch.description as display_name,LastName,MRN,SOCDate,EpStart,EpEnd,ICD,PhyLastName,SNVisits,HHAVisits,MSWVisits,PTVisits,
                OTVisits,STVisits,TotVisits');
                    $this->db->from('tb_Episode, tb_Branch');
                    if ($is_random_episodes) {
                            $this->db->order_by("RAND()");
                    } else {
                            $this->db->order_by($column_name,$order_dir);
                    }

            if(isset($_POST['type']) && $_POST['type'] == 'all') {

            } else {
            $this->db->limit($limit,$offset);
            }

                    if($where !='') {
                            $this->db->where($where,NULL, FALSE);
                    }

                    if($branch !='') {
                            $this->db->where_in('BranchID',$_POST['branch']);
                    }
                    $episode_data = $this->db->get();
                    $episode_data = $episode_data->result_array();

                    $this->db->select('count(*) as total');
                    $this->db->from('tb_Episode, tb_Branch');
                    if($where !='') {
                            $this->db->where($where,NULL, FALSE);
                    }
                    if($branch !='') {
                            $this->db->where_in('BranchID',$branch);
                    }
                    $count_data = $this->db->get();
                    $count_data = $count_data->result_array();
                }


        $random_episode = $count_data[0]['total'];
        if(isset($_POST['random_episode']) && (int)$_POST['random_episode'] && $_POST['random_episode'] != 0) {
            $random_episode = $_POST['random_episode'];
        }
        $total_data = $random_episode;
        if(isset($_POST['type']) && $_POST['type'] == 'all') {
            echo json_encode(array('draw'=>$_POST['draw'],'recordsTotal'=>$total_data,'recordsFiltered'=>$total_data,'data'=>$episode_data));
        } else {
            echo json_encode(array('draw'=>$_POST['draw'],'recordsTotal'=>$total_data,'recordsFiltered'=>$total_data,'data'=>$episode_data));
        }
        die;

0 个答案:

没有答案