数据表库在 CodeIgniter 3 中不起作用

Posted

技术标签:

【中文标题】数据表库在 CodeIgniter 3 中不起作用【英文标题】:Datatables library not working in CodeIgniter 3 【发布时间】:2016-04-22 05:54:52 【问题描述】:

我正在 CodeIgniter 3 上尝试数据表库,但它在 CI3 中不起作用。我已经在 CodeIgniter 2 中尝试过this example,它运行良好,但我希望在 CodeIgniter 3 上使用这个库。

我需要进行哪些更改才能使其与 CodeIgniter 3 一起使用?

【问题讨论】:

术语“数据表”是模棱两可的。在 .NET 中,它是一个表示内存数据表的类。在 JSF 和 Wicket 等基于组件的 MVC 框架中,它是一个基于集合动态呈现 html 表格的 UI 组件。对于 jQuery DataTables 插件,请使用 [jquery-datatables] 标签。 感谢您的建议! 在他们的论坛上查看this topic。 你用 Code Igniter 3 做过这个吗? @Tpojka .... 您遇到的错误是什么? 【参考方案1】:

在模型目录下创建 Person_model.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
     class Person_model extends CI_Model 
           var $table = 'persons';
           var $column = array('firstname','lastname','gender','address','dob');
          var $order = array('id' => 'desc');

    public function __construct()
    
        parent::__construct();
        $this->load->database();
    

     private function _get_datatables_query()
    
        $this->db->from($this->table);
        $i = 0;
        foreach ($this->column as $item)
        
            if($_POST['search']['value'])
                ($i===0) ? $this->db->like($item, $_POST['search']['value']) : $this->db->or_like($item, $_POST['search']['value']);
            $column[$i] = $item;
            $i++;
        

        if(isset($_POST['order']))
        
            $this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        
        else if(isset($this->order))
        
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        
    

    function get_datatables()
    
        $this->_get_datatables_query();
        if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->get();
        return $query->result();
    

    function count_filtered()
    
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    

    public function count_all()
    
        $this->db->from($this->table);
        return $this->db->count_all_results();
    

创建控制器文件 Person.php

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Person extends CI_Controller 

    public function __construct()
    
        parent::__construct();
        $this->load->model('person_model','person');
    

     public function index()
    
        $this->load->helper('url');
        $this->load->view('person_view');
    

    public function ajax_list()
    
        $list = $this->person->get_datatables();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $person) 
            $no++;
            $row = array();
            $row[] = $person->firstName;
            $row[] = $person->lastName;
            $row[] = $person->gender;
            $row[] = $person->address;
            $row[] = $person->dob;
            $data[] = $row;
        

        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->person->count_all(),
                        "recordsFiltered" => $this->person->count_filtered(),
                        "data" => $data,
                );
        //output to json format
        echo json_encode($output);
    

以及最后一次创建以供查看。视图目录下的 person_view.php

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Ajax CRUD with Bootstrap modals and Datatables</title>
    <link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css')?>" rel="stylesheet">
    <link href="<?php echo base_url('assets/datatables/css/dataTables.bootstrap.css')?>" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

  <div class="container">
    <table id="table" class="table table-striped table-bordered" cellspacing="0" >
      <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Gender</th>
          <th>Address</th>
          <th>Date of Birth</th>
          <th style="width:125px;">Action</th>
        </tr>
      </thead>
      <tbody>
      </tbody>

      <tfoot>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Gender</th>
          <th>Address</th>
          <th>Date of Birth</th>
          <th>Action</th>
        </tr>
      </tfoot>
    </table>
  </div>

  <script src="<?php echo base_url('assets/jquery/jquery-2.1.4.min.js')?>"></script>
  <script src="<?php echo base_url('assets/bootstrap/js/bootstrap.min.js')?>"></script>
  <script src="<?php echo base_url('assets/datatables/js/jquery.dataTables.min.js')?>"></script>
  <script src="<?php echo base_url('assets/datatables/js/dataTables.bootstrap.js')?>"></script>

  <script type="text/javascript">

    var save_method; //for save method string
    var table;
    $(document).ready(function() 
      table = $('#table').DataTable(

        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.

        // Load data for the table's content from an Ajax source
        "ajax": 
            "url": "<?php echo site_url('person/ajax_list')?>",
            "type": "POST"
        ,

        //Set column definition initialisation properties.
        "columnDefs": [
        
          "targets": [ -1 ], //last column
          "orderable": false, //set not orderable
        ,
        ],

      );
    );


    function reload_table()
    
      table.ajax.reload(null,false); //reload datatable ajax
    

  </script>
  </body>
</html>

创建表的 SQL :

创建数据库crud; 使用杂物; 创建表persons ( id int(11) 无符号非空 AUTO_INCREMENT, firstName varchar(100) 默认为空, lastName varchar(100) 默认为空, gender enum('male','female') 默认为空, address varchar(200) 默认为空, dob 日期默认为空, 主键 (id) ) ENGINE=InnoDB 默认字符集=utf8;

【讨论】:

以上是关于数据表库在 CodeIgniter 3 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

获取数据在 CodeIgniter 中不起作用

数据库事务在控制器 codeigniter 中不起作用

CodeIgniter 会话类在 Chrome 中不起作用

动态自定义过滤器在 codeigniter 中不起作用

忘记密码功能在 CodeIgniter 中不起作用

ng-click 在 ajax 数据表 + codeigniter 中不起作用