首页 文章

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

提问于
浏览
0

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

class 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

已定义('BASEPATH')或退出('不允许直接访问脚本');

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

    如果你看看我们的html代码,发现在 </thead> 关闭后你有空白 <tbody></tbody> 但你应该喜欢以下内容:

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

相关问题