如何使用jquery在codeigniter中进行Filter搜索

时间:2016-10-25 03:44:11

标签: jquery ajax codeigniter datatables

我的codeigniter视图中有一个数据表。我想根据用户选择或输入的搜索或过滤器显示数据表中的数据。 我正在使用jquery重新加载数据。请帮忙。

谢谢!

这是我的形式gui enter image description here

我的控制器

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->namadept). " ";
            $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();
    }
}

这是我的观点:

      <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">
                                <?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">
                                    <?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>

0 个答案:

没有答案