如何使用数据表CI发送2参数

时间:2017-11-13 01:26:41

标签: php codeigniter datatables

我的控制器:

function json_survey()
{
    header('Content-Type: application/json');
    $this->load->library('Datatables');
    $this->load->helper('datatables_helper');
    $this->datatables->SELECT('id_survey,judul_survey,status_survey,responden,detail_target,judul_kategori,judul_target');
    $this->datatables->add_column('action',tombol_survey('$1','$2'), 'id_survey,status_survey');
    $this->datatables->FROM('survey');

    return print_r($this->datatables->generate('json',''));
}

我的数据表助手:

function tombol_survey($id_survey,$status_survey)
{
    $ci = & get_instance();
    $html = '<span class="actions">';
    If($status_survey=='terbit'){ 
        $html .= '<a href="' . base_url() . 'subscriber/edit/' . $id_survey . '">Tutup</a>';
    }elseif($status_survey=='tertunda'){
        $html .= '<a href="' . base_url() . 'subscriber/edit/' . $id_survey . '">Tambah</a>';
    }else{
        $html .= 'Tutup | ';
    }
    $html .= '<a href="' . base_url() . 'subscriber/delete/' . $id_survey . '">Lihat | </a>';
    $html .= '<a href="' . base_url() . 'subscriber/delete/' . $id_survey . '">Hapus</a>';
    $html .= '</span>';

    return $html;
}

这是json没有错误,但参数2nd($status_survey)未包括在内,因此函数if始终返回false

2 个答案:

答案 0 :(得分:0)

You can achieve this by like below:

Your controller:

function json_survey()
    {
        header('Content-Type: application/json');
        $this->load->library('Datatables');
        $this->load->helper('datatables_helper');
        $this->datatables->SELECT('id_survey,judul_survey,status_survey,responden,detail_target,judul_kategori,judul_target');
        $dataids = array('id_survey'=> $1,'status_survey'=> $2);  //Make array and add params for survey and id and add it to the below function
        $this->datatables->add_column('action',tombol_survey($dataids), 'id_survey,status_survey');
        $this->datatables->FROM('survey');

        return print_r($this->datatables->generate('json',''));
    }


function tombol_survey($ids)
{
    $ci = & get_instance();
    if(!empty($ids)){
        $html = '<span class="actions">';
        if($ids['status_survey']=='terbit'){ 
            $html .= '<a href="' . base_url() . 'subscriber/edit/' . ($ids['id_survey']) ? ($ids['id_survey']) : ''. '">Tutup</a>';
        }elseif($ids['status_survey']=='tertunda'){
            $html .= '<a href="' . base_url() . 'subscriber/edit/' .($ids['id_survey']) ? ($ids['id_survey']) : ''. '">Tambah</a>';
        }else{
            $html .= 'Tutup | ';
        }
        $html .= '<a href="' . base_url() . 'subscriber/delete/' . ($ids['id_survey']) ? ($ids['id_survey']) : '' . '">Lihat | </a>';
        $html .= '<a href="' . base_url() . 'subscriber/delete/' . ($ids['id_survey']) ? ($ids['id_survey']) : '' . '">Hapus</a>';
        $html .= '</span>';

        return $html;
    }
}

答案 1 :(得分:0)

function tombol_survey($ids)
    {
        $ci = & get_instance();
        if(!empty($ids)){
            $html = '<span class="actions">';
            if($ids['status_survey']=='terbit'){ 
                $html .= '<a href="' . base_url() . 'survey/edit/'.$ids['id_survey'].'">Tutup2</a>';
            }else if($ids['status_survey']=='tertunda'){
                $html .= '<a href="' . base_url() . 'pertanyaan/tambah/'.$ids['id_survey'].'">Tambah</a>';
            }else{
                $html .= 'Tutup | ';
            }
            $html .= '<a href="' . base_url() . 'pertanyaan/tambah/' .$ids['id_survey']. '">Tambah</a>';
            $html .= '<a href="' . base_url() . 'pertanyaan/tambah/' .($ids['status_survey']) ? ($ids['status_survey']) : ''. '">Tambah</a>';
            $html .= '</span>';

            return $html;
        }
    }

我调试,现在包含了参数status_survey,但是如果仍然返回false