使用Codeigniter中的数据表加载表

In Codeigniter I am loading a table using dynamic values in array.In controller I have load the table using load view.

$.ajax({
            type: "POST",
            url: baseUrl + 'reports/getsalary',
            data: data,
            success: function (data) {
                var obj = jQuery.parseJSON(data);
                // console.log(obj);
                if (obj.status == 'error') {
                    $("#reports").html(obj.msg);
                } else {
                  $("#salarylist").DataTable();
                    $("#reports").html(obj.rendered);
                }
            }
        });

In controller I am having the code,

$html= $this->load->view("reports/partial/salaryreport", $data, TRUE);

                $result['status']='success';
                $result['rendered']= "".$html."";   

            echo json_encode($result);
            exit;

I already loaded the table with data.How can I convert the table to datatable?

If you want to use ajax to populate a DataTable table then you will need to initialize the DataTable and follow their api to properly get data from the server. This means rendering a json response in a manner that DataTables can understand.

Here is an example: https://datatables.net/examples/data_sources/server_side

With codeigniter you can use: https://github.com/IgnitedDatatables/Ignited-Datatables

If you have any issues setting up IgnitedDatatables there are many resources here on stack.

Here is a breif example from my own code using IgnitedDatatables:

$('#projects').DataTable({
                destroy: true,
                processing: true,
                serverSide: true,
                pageLength: 5,
                lengthMenu: [5, 10, 20, 50, 100],
                autoWidth: false,
                ajax: {
                    url: base_path + 'projects/get_table/' + category,
                    type: 'POST',
                    dataSrc: function (json) {
                        if (typeof json.status !== 'undefined' && json.status == 'error') {
                            toastr.error(json.msg);
                            $('#projects').DataTable().destroy();
                            $('#projects').hide();
                        } else {
                            return json.data;
                        }
                    }
                },
                columns: [
                    {data: 'checkbox', orderable: false, searchable: false},
                    {data: 'name'},
                    {data: 'created'},
                    {data: 'last_modified'},
                    {data: 'actions', orderable: false, searchable: false}
                ]
            });

PHP:

Please note: I've modified the IgnitedDatatables library for my own code so these methods might not work exactly as they are shown below for you using the default library.

public function get_table($category = null) {
    if (!$this->input->is_ajax_request()) {
        exit('No direct script access allowed');
    }
    $this->load->library('datatables');
    $this->load->model('backend/datatables_model');
    $this->datatables
            ->select('id, name, s_order, created, created_by, last_modified, last_modified_by, published, media_resource_link')
            ->from('projects')
            ->default_order('s_order', 'DESC')
            ->edit_column('name', array($this->datatables_model, 'callback_info_projects'), 'id, name, media_resource_link')
            ->edit_column('created', array($this->datatables_model, 'callback_created_modified'), 'id, created, created_by')
            ->edit_column('last_modified', array($this->datatables_model, 'callback_created_modified'), 'id, last_modified, last_modified_by')
            ->add_column('actions', array($this->datatables_model, 'callback_actions_projects'), 'id, published');
    if ($this->ion_auth->is_admin()) {
        $this->datatables->add_column('checkbox', '<input name="u[]" type="checkbox" value="%s">', 'id');
    } else {
        $this->datatables->add_column('checkbox', '%s', 's_order');
    }
    if (!is_null($category)) {
        $this->datatables->where('category', $category);
    }
    $this->response->output($this->datatables->generate())->json();
}