【问题标题】:How to solve search in Codeigniter with datatable如何使用数据表解决 Codeigniter 中的搜索
【发布时间】:2017-01-15 10:43:11
【问题描述】:

我在标签搜索中遇到问题,当我输入一些字符时,它会显示一条错误消息

我的控制器:

public function ajaxlist()
    {
    $list = $this->eee_model->get_datatables();
    $data = array();
    $no=0;
    foreach ($list as $q) {
        $no++;
        $row = array();
        $row[] = $no;
        $row[] = $q->nofile;
        $row[] = $q->est_no ."/".$q->file_no;
        $row[] = $q->number;
        $row[] = $q->nobilut;
        $row[] = $q->nounit;
        $row[] = $q->zone;
        $row[] = $q->subz;
        $row[] = '<a class="btn btn-sm btn-primary" href="'.site_url($q->file).'" "><i class="fa fa-file-pdf-o"></i></a>';
        $row[] = '<a class="btn btn-sm btn-primary" href="'.site_url("estebyan/edit/".$q->id).'" title="Edit" target="_blank" "><i class="fa fa-edit"></i> edit</a>';
        $row[] =  '<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$q->id."'".')"><i class="fa fa-trash"></i> delete</a>';             

        $data[] = $row;
    }

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

我的模特:

var $table = 'Edata';
var $column_order = array(null, 'number','nofile','zone','subz','nobilut'); //set column field database for datatable orderable
var $column_search = array('number','nofile','zone','subz','nobilut'); //set column field database for datatable searchable 
var $order = array('id' => 'asc'); // default order 
public function __construct()
{
    parent::__construct();
    $this->load->database();
}



private function _get_datatables_query()
{

    $this->db->from($this->table);

    $i = 0;

    foreach ($this->column_search as $item) // loop column 
    {

        if(isset($_POST['search']['value']) ? $_POST['search']['value'] : null)
      // 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();
}

我的观点:

              <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>nofile</th>
                        <th>fileno</th>
                        <th>number</th>
                        <th>nobulit</th> 
                        <th>unit</th> 
                        <th>Zone</th> 
                        <th>Sub</th> 
                        <th>attach</th> 
                        <th >edit</th> 
                        <th >delete</th> 
                    </tr>
                </thead> 
                <tbody>

                </tbody>
            </table> 

我的脚本:

    table = $('#table').DataTable({ 


    "processing": true, //Feature control the processing indicator.
    "serverSide": true, //Feature control DataTables' server-side processing mode.
    "order": [], //Initial no order.
    // Load data for the table's content from an Ajax source
    "ajax": {
        "url": "<?php echo site_url('estebyan/ajaxlist')?>",
        "type": "POST"
    },

    //Set column definition initialisation properties.
    "columnDefs": [
    { 
        "targets": [ 0 ], //first column / numbering column
        "orderable": false, //set not orderable
    },
    ],

}); 

显示所有视图数据,但搜索不起作用!

【问题讨论】:

    标签: mysql codeigniter datatables codeigniter-2 codeigniter-3


    【解决方案1】:
    $.ajax({
            type: "post",
            url: "your url",
            dataType: "json",
            success: function(data){ // return json encode data
    
                if(data.length > 0){
                    tabledata = [];
                    for (var i = 0; i < data.length; i++) {
                        tabledata.push([ // push data in array as per your column
                            data[i].name,
                            data[i].other_value,
                            data.[i].other_value]);
                    }
                            if (table != null) table.fnDestroy();
                    table = $('#tablename').dataTable({
                        "data": tabledata, // add your data array it will create datatable with search and pagination and other things
                    });
                }                           
            },
            error: function(){
    
            }
        });
    

    如果您没有 datatable.js 和 datatable.css,请先包含并尝试此代码

    【讨论】:

      【解决方案2】:

      您的 datatables_query 中的订单数组中没有任何内容,请在处理订单之前尝试这样写。

      $column_order = $this-&gt;column_order;

      【讨论】:

        猜你喜欢
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 2013-03-23
        • 2013-12-03
        相关资源
        最近更新 更多