您好我尝试使用codeingiter创建带有数据表的自定义过滤器,但是在提交参数时自定义搜索无效,但默认过滤器正在成功运行。我尝试使用浏览器检查网络但没有发现错误http alredy 200
我的模型代码
public function get_datatables_query()
{
//add custom filter parameter
$this->db->select('*');
$this->db->from('surat_masuk');
$this->db->join('kategori', 'kategori.cat_id = surat_masuk.surat_kategori_id');
$this->db->join('dinas', 'dinas.id_dinas = surat_masuk.surat_dinas_id');
$this->db->join('users', 'users.id = surat_masuk.surat_user_id');
$this->db->join('status_surat', 'status_surat.status_surat_id = surat_masuk.surat_status');
$this->db->where('status_surat_id',2);
if($this->input->post('index'))
{
$this->db->like('surat_id_index', $this->input->post('index'));
}
if($this->input->post('nomor'))
{
$this->db->like('surat_no', $this->input->post('nomor'));
}
if($this->input->post('kode'))
{
$this->db->like('surat_kode', $this->input->post('kode'));
}
if($this->input->post('kategori'))
{
$this->db->where('surat_kategori_id', $this->input->post('kategori'));
}
$i = 0;
//
foreach ($this->column_search as $item) // loop column
{
if(isset($_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)]);
}
}
///Tampilkan Semua data
public 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();
}
public function count_filtered()
{
$this->get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from('surat_masuk');
return $this->db->count_all_results();
}
public function get_list_kategori()
{
$this->db->select('surat_kategori_id');
$this->db->from('surat_masuk');
$this->db->order_by('surat_kategori_id','asc');
$query = $this->db->get();
$result = $query->result();
$kategori = array();
foreach ($result as $row)
{
$kategori[] = $row->surat_kategori_id;
}
return $kategori;
}
我的控制器
function index()
{
$this->load->model('admin_kategori/Kategori_model');
$all_kategori = $this->Surat_masuk_model->get_list_kategori();
$opt = array('' => 'Semua Kategori');
foreach ($all_kategori as $all_kategori) {
$opt[$all_kategori] = $all_kategori;
}
$data['kategori'] = form_dropdown('',$opt,'','id="kategori" class="form-control select2"');
$data['surat_masuk'] = $this->Surat_masuk_model->get_all_surat_masuk_join();
$data['content'] = 'admin_surat_masuk/v_index';
$this->load->view('template/v_index',$data);
}
public function ajax_list()
{
$list = $this->Surat_masuk_model->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $surat) {
$no++;
$row = array();
$row[] = $no;
$row[] = $surat->surat_id_index;
$row[] = $surat->surat_no;
$row[] = $surat->surat_kode;
$row[] = $surat->surat_kategori_id;
$row[] = $surat->cat_name;
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->Surat_masuk_model->count_all(),
"recordsFiltered" => $this->Surat_masuk_model->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
我的Javascript
<script>
var table;
$(document).ready(function() {
//datatables
table = $('#tabel_search').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('admin_surat_masuk/ajax_list')?>",
"type": "POST",
"data": function ( data ) {
data.surat_id_index = $('#index').val();
data.surat_no = $('#nomor').val();
data.surat_kode = $('#kode').val();
data.surat_kategori_id = $('#kategori').val();
}
},
//Set column definition initialisation properties.
"columnDefs": [
{
"targets": [ 0 ], //first column / numbering column
"orderable": false, //set not orderable
},
],
});
$('#btn-filter').click(function(){ //button filter event click
table.ajax.reload(); //just reload table
});
$('#btn-reset').click(function(){ //button reset event click
$('#form-filter')[0].reset();
table.ajax.reload(); //just reload table
});
});
</script>
我的表单搜索自定义
<form id="form-filter" class="form-horizontal">
<div class="form-group">
<div class="col-sm-1">
<input type="text" class="form-control" placeholder="Index" name="index" id="index">
</div>
<div class="col-sm-2">
<input type="text" class="form-control" placeholder="Nomor Surat" name="nomor" id="nomor">
</div>
<div class="col-sm-2">
<input type="text" class="form-control" placeholder="Kode" name="kode" id="kode">
</div>
<label for="LastName" class="col-sm-1 control-label">Kategori</label>
<div class="col-sm-4">
<?php echo $kategori ?>
</div>
<div class="col-sm-2">
<button type="button" id="btn-filter" class="btn btn-primary">Cari</button>
<button type="button" id="btn-reset" class="btn btn-default">Reset</button>
</div>
</div>
</form>
我的表格HTML
<table id="tabel_search" class="table table-striped table-bordered" cell-spacing="0">
<thead>
<tr>
<th class="">No</th>
<th class="">Index</th>
<th>Nomor</th>
<th>Kode</th>
<th>Kategori</th>
<th>Nama Kategori</th>
</tr>
</thead>
<tbody>
</tbody>
</table>