动态自定义过滤器无法在codeigniter中工作

时间:2016-10-24 02:20:30

标签: jquery ajax codeigniter datatables

我有一个带有自定义过滤器和数据表的codeigniter表单。我正在使用jquery,ajax来加载数据。我所期望的是,当我选择公司列表时,我只想选择加载数据表的公司。但现在发生的是数据表中的所有数据加载,好像没有过滤器一样。请帮忙。

谢谢

这是我的控制器

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

 class Assetfilter extends CI_Controller {

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

        $this->load->model('user');
        $this->load->model('assetfiltermdl');
        $this->load->model('worklistmdl');
        $this->load->helper('url'); 
        $this->load->helper('date');
        $this->load->library('session');
        $this->load->library('email');
        $this->load->model('email_ctrl');
        $this->datauser = $this->session->userdata('logged_id');
        $this->load->library('form_validation');
        $this->load->library('My_PHPMailer');
        //Do your magic here
    }

    //update 20 oct 2016 list kary.

    public function kary_reload()
    {
        $list = $this->assetfiltermdl->get_listkary();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $listkary) {
            $no++;
            $row = array();
            $row[] = trim($listkary->idnik) . " ";
            $row[] = trim($listkary->kodenik). " ";
            $row[] =  rtrim($listkary->namakary). " ";
            $row[] = rtrim($listkary->namaper). " ";
            $row[] = trim($listkary->iddept). " ";
            $row[] = trim($listkary->jabatan). " ";
            $row[] = trim($listkary->flagstatus). " ";
            $row[] = trim($listkary->flagstatus). " ";


            /*$row[] = '<a href="<?php echo base_url(' . "'" . 'asset/karyawandetailform' . "'" .'); ?>/<?php echo $listkary[' . "'" . 'idnik' . "'" . '] ; ?>" class="btn btn-primary"> Detail</a>
                <a href="<?php echo base_url(' . "'" . 'asset/karyawaneditform' . "'" . '); ?>/<?php echo $listkary[' . "'" . 'idnik' . "'" .'] ; ?>" class="btn btn-primary">Detail</a>
                <a href="<?php echo base_url(' . "'" . 'asset/karyawandeleform' . "'" . ');    ?>/<?php echo $listkary[' . "'" . 'idnik' . "'" .'] ; ?>" class="btn btn-primary" onClick="javascript:return confirm(' . "'" . 'Apakah Anda Sudah Yakin ?' . "'" . ')"  >Del</a>'; */

            $data[] =  ($row);
        }

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

        $output = str_replace('\r'," ", $output );



        echo json_encode($output);
    }
 }

这是我的模特

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Assetfiltermdl extends CI_Model {

    var $kary_table = 'listkary';
    var $kary_column_order = array(null, 'update_date'); //set column field database for datatable orderable
    var $kary_column_search = array('idper','iddept','namakary','kodenik'); //set column field database for datatable searchable 
    var $kary_order = array('update_date' => 'desc'); // default order 

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

    }

//update 20 oct 2016

  public function get_listkary()
    {
      $this->_get_listkary_query();
      echo $this->db->last_query();
      if($_POST['length'] != -1)
      $this->db->limit($_POST['length'], $_POST['start']);
      $query = $this->db->get();
      return $query->result();
    }

  private function _get_listkary_query()
    {

      //add custom filter here
      if($this->input->post('idper'))
      {
        $this->db->where('idper', $this->input->post('idper'));
      }
      if($this->input->post('iddept'))
      {
        $this->db->where('iddept', $this->input->post('iddept'));
      }
      //if($this->input->post('idsubdept'))
      //{
      //  $this->db->like('LastName', $this->input->post('LastName'));
      //}
      if($this->input->post('kodenik'))
      {
        $this->db->like('kodenik', $this->input->post('kodenik'));
      }
      if($this->input->post('namakry'))
      {
        $this->db->like('namakary', $this->input->post('namakry'));
      }

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

      $i = 0;

      foreach ($this->kary_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->where($item, $_POST['search']['value']);
          }
          else 
          {
            if($i===1) // second loop (iddept)
            {
                $this->db->where($item, $_POST['search']['value']);
            }
            else
            {
                $this->db->like($item, $_POST['search']['value']);  
            }   
          }

          if(count($this->kary_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->kary_column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } 
        else if(isset($this->kary_order))
        {
            $order = $this->kary_order;
            $this->db->order_by(key($order), $order[key($order)]);
        }

    }


  private function _get_listkary_count()
    {

      //add custom filter here
      if($this->input->post('idper'))
      {
        $this->db->where('idper', $this->input->post('idper'));
      }
      if($this->input->post('iddept'))
      {
        $this->db->where('iddept', $this->input->post('iddept'));
      }
      //if($this->input->post('idsubdept'))
      //{
      //  $this->db->like('LastName', $this->input->post('LastName'));
      //}
      if($this->input->post('kodenik'))
      {
        $this->db->like('kodenik', $this->input->post('kodenik'));
      }
      if($this->input->post('namakry'))
      {
        $this->db->like('namakary', $this->input->post('namakry'));
      }

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

      $i = 0;

      foreach ($this->kary_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->where($item, $_POST['search']['value']);
          }
          else 
          {
            if($i===1) // second loop (iddept)
            {
              $this->db->where($item, $_POST['search']['value']);
            }
            else
            {
              $this->db->like($item, $_POST['search']['value']);  
            } 
          }

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

    }    

  public function count_all()
    {
        $this->_get_listkary_count();
        return $this->db->count_all_results();
    }

  function count_filtered()
    {
        $this->_get_listkary_count();
        $query = $this->db->get();
        return $query->num_rows();
    }
}

/* End of file assetfiltermdl.php */
/* Location: ./application/models/assetfiltermdl.php */

这是我的观点

<div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h1> Data Karyawan </h1>
                    <h2> Informasi Data Karyawan </h2>
                    <p>
                        <a href ="<?php echo base_url('asset/karyawantambahform'); ?>" class="btn btn-primary">Tambah Karyawan Baru</a>   
                    </p>
              </div>

            </div>
            <div class="clearfix"></div>

          <div class = "row">

            <div class="col-md-12 col-sm-12 col-xs-12">
              <div class="x_panel">
                <div class="x_title">
                  <h2>Daftar Karyawan</h2>
                  <ul class="nav navbar-right panel_toolbox">
                    <li><a href="#"><i class="fa fa-chevron-up"></i></a>
                    </li>
                    <li class="dropdown">
                      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                    </li>
                    <li><a href="#"><i class="fa fa-close"></i></a>
                    </li>
                  </ul>
                  <div class="clearfix"></div>
                </div>
                <div class="x_content">

                <form id="form-filter" class="form-horizontal">
                    <div class="item form-group">
                        <label for="idper" class="col-sm-2 control-label">Perusahaan</label>
                        <div class="col-sm-4">
                            <select class="select2_single form-control" tabindex="-1" id="idper" name="idper">
                                <option>Pilih Perusahaan</option>
                                <?php foreach ($perushlov as $key => $value) { ?>
                                    <option value="<?php echo $value['idper']?>"><?php echo $value['namaper']?></option>}
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="item form-group" >
                        <label for="iddept" class="col-sm-2 control-label">Departemen :</label> 
                        <div class="col-sm-4">
                            <select class="select2_single form-control" tabindex="-1" id="iddept" name="iddept">
                                    <option >Pilih Departement</option>
                                    <?php foreach ($deptlov as $key => $value) { ?>
                                        <option value="<?php echo $value['iddept']?>"><?php echo $value['kodedept'] . ' - ' . $value['namadept'] . ' - ' . $value['namaper'] ?></option>}
                                    <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="item form-group">
                        <label for="kodenik" class="col-sm-2 control-label">Nama Karyawan</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="namakry">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label for="kodenik" class="col-sm-2 control-label">Kode NIK</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="kodenik">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label for="LastName" class="col-sm-2 control-label"></label>
                        <div class="col-sm-4">
                            <button type="button" id="btn-filter" class="btn btn-primary">Filter</button>
                            <button type="button" id="btn-reset" class="btn btn-default">Reset</button>
                        </div>
                    </div>
                 </form>
                 <!-- <table id="datatable-buttons" class="table table-striped table-bordered"> -->
                <table id="master_karyawan" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                 <thead>
                    <tr>
                        <th>Id</th>
                        <th>Kode Nik</th>
                        <th>Nama Karyawan</th>
                        <th>Perusahaan</th>
                        <th>Departemen</th>
                        <th>Jabatan</th>
                        <th>Aktif</th>
                        <th>Administrasi</th>
                    </tr>           
                </thead>
                <tbody> 
                </tbody>
                </table>
                </div>
              </div>
            </div>
          </div>
        </div>

<script type="text/javascript">

var table;

$(document).ready(function() {

    //datatables
    table = $('#master_karyawan').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 base_url('index.php/Assetfilter/kary_reload')?>",
            "type": "POST",
            "data": function ( data ) {
                data.perush = $('#idper').val();
                data.depart = $('#iddept').val();
                data.namakry = $('#namakry').val();
                data.kodenik = $('#kodenik').val();
            }
        },

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

        //$.fn.dataTable.ext.errMode = 'throw';

    });

    $('#btn-filter').click(function(){ //button filter event click
        table.ajax.reload(null,false);  //just reload table
    });
    $('#btn-reset').click(function(){ //button reset event click
        $('#form-filter')[0].reset();
        table.ajax.reload(null,false);  //just reload table
    });

});

</script>

1 个答案:

答案 0 :(得分:-2)

也许可以回答很晚但是看到这个链接希望可以解决你的问题

http://mbahcoding.com/tutorial/php/codeigniter/codeigniter-server-side-datatables-custom-filter.html

public function index()
    {
        $this->load->helper('url');
        $this->load->helper('form');

        $list = $this->assetfiltermdl->get_listkary();

        $opt = array('' => 'All List Karyawan');
        foreach ($karyawans as $karyawan) {
            $opt[$karyawan] = $karyawan;
        }

        $data['form_karyawan'] = form_dropdown('',$opt,'','id="karyawan" class="form-control"');
        $this->load->view('karyawan_view', $data);
    }

我希望这可以解释你的代码