Codeigniter模块(HMVC)中的数据表Ajax调用

时间:2018-07-22 15:16:26

标签: php datatables codeigniter-3 hmvc hmvc-codeigniter

我最近正在为一家小型公司开发一个简单的php应用程序,尝试使用HMVC codeigniter(MX扩展名),并在视图中尝试了对数据表的第一个ajax调用,但是没有显示任何数据。已经检查了控制器和数据库中的功能是否正常工作,我收到了带有数据的var_dump。这是我的文件: 模块型号

类Mdl_categorie扩展了CI_Model {

var $table ='categorie';
var $column_order = array('ID_anag_type', 'descr'); //set column field database for datatable orderable
var $column_search = array('descr'); //set column field database for datatable searchable just firstname , lastname , address are searchable
var $order = array('ID_anag_type' => 'desc'); // default order 

function __construct()
{
    parent::__construct();
}

private function _get_datatables_query()
{
    $this->db->from($this->table);
    $i = 0;

    foreach ($this->column_search as $item) // loop column
    {
        if($_POST['search']['value']) // if datatable send POST for search
        {

            if($i===0) // first loop
            {
                $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                $this->db->like($item, $_POST['search']['value']);
            }
            else
            {
                $this->db->or_like($item, $_POST['search']['value']);
            }

            if(count($this->column_search) - 1 == $i) //last loop
                $this->db->group_end(); //close bracket
        }
        $i++;
    }


    if(isset($_POST['order'])) // here order processing
    {
        $this->db->order_by($this->column_order[$_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();
}
}

模块控制器

<?php

defined('BASEPATH')或exit('不允许直接脚本访问');

Anagrafiche类扩展了MX_Controller {

function __construct()
{
    parent::__construct();
    $this->load->model('anagrafiche/mdl_categorie','mdl_categorie');
}

public function main (){
    $data['module']= 'anagrafiche';
    $data['view_file']='main';
    $data['_pg_title']='Anagrafiche';
    $data['_descr_title']='Tabelle riassuntive anagrfiche clienti/fornitori/vettori/...';
    echo Modules::run('template/operator_layout', $data);
}

public function categorie (){
    $data['module']= 'anagrafiche';
    $data['view_file']='categorie';
    $data['_pg_title']='Anagrafiche';
    $data['_descr_title']='Tipi di anagrafiche';
    echo Modules::run('template/operator_layout', $data);
}

public function ajax_categorie_list(){
    $list = $this->mdl_categorie->get_datatables();
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $categoria) {
        $no++;
        $row = array();
        $row[] = $categoria->ID_anag_type;
        $row[] = $categoria->descr;
        $data[] = $row;
    }

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

模块视图文件

<section class="content">
  <div class="row">
    <div class="col-xs-12">
      <div class="box">
        <div class="box-header">
          <h3 class="box-title"></h3>
             <div class="box-body">
              <div class="nav-tabs-custom">
        <ul class="nav nav-tabs">
          <li class="active"><a href="#tab_1" data-toggle="tab"><i class="fa fa-list"></i> Elenco</a></li>
          <li><a href="#tab_2" data-toggle="tab"><i class="fa fa-plus"></i> Aggiungi</a></li>
          <li><a href="#tab_3" data-toggle="tab">Tab 3</a></li>
        </ul>
        <div class="tab-content">
          <div class="tab-pane active" id="tab_1">
            <table id="anag_categorie" class="table table-bordered table-striped">
            <thead>
            <tr>
              <th>ID Catgeoria</th>
              <th>Nome Categoria</th>

            </tr>
            </thead>
            <tbody>


            </tbody>
            </table>
            <script type="text/javascript">
            $(document).ready(function(){
                $('#anag_categorie').DataTable( {
                        "processing": true,
                        "serverSide": true,
                        "ajax": {
                            "url": "http://sviluppoweb/campanini/anagrafiche/categorie/ajax_categorie_list",
                            "type": "POST"
                        },);

                var versionNo = $.fn.dataTable.version;
                alert(versionNo);
            });

            </script>

            <!-- 
            <script type="text/javascript" src="<?php echo base_url('application/modules/anagrafiche/views/js/categorie.js')?>"></script>
            //-->
          </div>
          <!-- /.tab-pane -->
          <div class="tab-pane" id="tab_2">
            The European languages are members of the same family. Their separate existence is a myth.
            For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ
            in their grammar, their pronunciation and their most common words. Everyone realizes why a
            new common language would be desirable: one could refuse to pay expensive translators. To
            achieve this, it would be necessary to have uniform grammar, pronunciation and more common
            words. If several languages coalesce, the grammar of the resulting language is more simple
            and regular than that of the individual languages.
          </div>
          <!-- /.tab-pane -->
          <div class="tab-pane" id="tab_3">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry.
            Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
            when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting,
            remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
            sheets containing Lorem Ipsum passages, and more recently with desktop publishing software
            like Aldus PageMaker including versions of Lorem Ipsum.
          </div>
          <!-- /.tab-pane -->
        </div>
        <!-- /.tab-content -->
      </div>
      <!-- nav-tabs-custom -->
    </div>
    <!-- /.col -->

             </div>
       </div>
        <!-- /.box-body -->
      </div>
      <!-- /.box -->
    </div>
    <!-- /.col -->
  </div>
  <!-- /.row -->
</section>
<!-- /.content -->

1 个答案:

答案 0 :(得分:0)

如果您查看我们的html代码,会发现在</thead>关闭之后,您有空白的<tbody></tbody>,但您应该执行以下操作:

'<tfoot>
    <tr>
       <th>ID Catgeoria</th>
        <th>Nome Categoria</th>
    </tr>
</tfoot>'