duandi5328 2018-07-22 15:16
浏览 49

在codeigniter模块(HMVC)中调用Datatables Ajax

I'm recently developing a simple php app for a small company, trying to use HMVC codeigniter (MX extension) and tried a first ajax call in the view for data-tables, but it shows no data. Already checked that function in controller and db are working and i receive a var_dump with data. Here are my files: MODULE MODEL

class Mdl_categorie extends 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();
}
}

MODULE CONTROLLER

<?php

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

class Anagrafiche extends 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);
}
}

MODULE VIEW FILE

<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条回答 默认 最新

  • duanhanglekr37902 2018-11-25 16:47
    关注

    if you look at our html code find that after </thead> close you have blank <tbody></tbody> but you should like following:

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

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘