duanjia7912 2019-04-29 17:33
浏览 32

使用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?

  • 写回答

1条回答 默认 最新

  • douzong5473 2019-04-29 21:53
    关注

    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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输