Search code examples
codeigniterjquery-jtable

How to using jtable plugin in codeigniter?


I'm trying to use jtable plugin in framework codeigniter but i got a problem. I'm confused how to pass variable from view (jtable javascript code) to controller and to pass json_encode from controller to view.

Here are my code.

in my view page(Attendance_view.php).

[html code]

<input style="width:100px" type="text" id="from" name="from" value="<?php echo date("Y-m")."-01";?>">

[js code]

//Prepare jTable
var base_url ="<?=base_url()?>";
$('#TableContainer').jtable({
    title: 'Attendance',
    paging: true,
    sorting: true,
    defaultSorting: 'month ASC',
    selecting: true, //Enable selecting
    multiselect: true, //Allow multiple selecting
    selectingCheckboxes: true, //Show checkboxes on first column

    actions: {
        listAction: '<?=base_url()?>index.php/Attendance_controller/listRecord',
        createAction: '<?=base_url()?>index.php/Attendance_controller/create',
        updateAction: '<?=base_url()?>index.php/Attendance_controller/update',
        deleteAction: '<?=base_url()?>index.php/AttendanceAbsensi_controller/delete'
    },
            ....//another field here
});
//Load attendance from server
$('#TableContainer').jtable('load',{
        month:$("#from").val()
});

in my controller(Attendance_controller.php)

function listRecord()
{
    $this->load->model('Attendance_action');
    $jTableResult=$this->Attendance_action->list_record();
    $data['jTableResult']= json_encode($jTableResult);
    $this->load->view('Attendance_view',$data['jTableResult']);

}

in my model (Attendance_action.php)

function list_record()
{
    //get post variable
    $date=$this->input->post('month');  // i can't get the value.

    //Get record count
    $result = //my query here[select "some data" from "mytable" where month='$date']
    $recordCount = mysql_num_rows($result);


        //Add all records to an array
        $rows = array();
        while($row = mysql_fetch_array($result))
        {
            $rows[] = $row;
        }

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $recordCount;
        $jTableResult['Records'] = $rows;
        return $jTableResult;
}

When i load the controller page, the error message from jtable occured "An error occured while communicating to the server". Please help. thanks.


Solution

  • i want to share my code. Now the problem fixed. I just change the controller code like this. Add "exit();" in controller.

    function index() 
    {
        $this->load->view('Attendance_view');
    
    }
    
    function listRecord()
    {
        $this->load->model('Attendance_action');
        $jTableResult=$this->Antendance_action->list_record();
        print_r(json_encode($jTableResult));
        exit();
    }