使用服务器端处理的多搜索列数据表

问题描述 投票:0回答:1

我想使用数据表服务器端进行单独搜索,所有功能与datatables.net中的示例相同,但是当我在列过滤器框中搜索时,它只是处理并且不会改变,任何人都可以帮助我吗?谢谢你:)

为了方便阅读,我将在下面编写我的代码:

控制器

public function pm()
    {
        $data['title'] = "PM";
        $username = $this->session->userdata('username');
        $data['user'] = $this->db->select('*')->from('mst_user')->join('mst_pegawai', 'mst_pegawai.username=mst_user.username', 'left')->where('mst_user.username', $username)->get()->row_array();

        $data['dataPM'] = $this->db->get('mfaktur');
        if ($this->form_validation->run() == false) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/navbar', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('master/pm', $data);
            $this->load->view('templates/footer');
        }
    }
    // Get data perusahaan
    public function get_data_pm()
    {
        $list = $this->pm->get_datatables();
        $data = array();
        $no = $_POST['start'];

        foreach ($list as $field) {
            // $tmbl2 = '<a href="" class="success p-0" data-toggle="modal" data-target="#ModalUpdate"' . $row(['id']) . '"><i class="ft-edit-2 font-medium-3 mr-2"></i></a>';
            $tmbl = "<button href='' class='btn btn-success mb-1' data-toggle='modal' data-backdrop='static' data-keyboard='false' data-target='#ModalUpdate'>Update</button>";
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = $field->kdJenisTransaksi;
            $row[] = $field->fgPengganti;
            $row[] = $field->nomorFaktur;
            $row[] = $field->masaPajak;
            $row[] = $field->tahunPajak;
            $row[] = $field->tanggalFaktur;
            $row[] = $field->npwpPenjual;
            $row[] = $field->alamatPenjual;
            $row[] = $field->namaPenjual;
            $row[] = $field->npwpLawanTransaksi;
            $row[] = $field->namaLawanTransaksi;
            $row[] = $field->alamatLawanTransaksi;
            $row[] = $field->referensi;
            $row[] = $field->jumlahDpp;
            $row[] = $field->jumlahPpn;
            $row[] = $field->jumlahPpnbm;
            $row[] = $field->isWapu;
            $row[] = $field->isSupplierReg;
            $row[] = $field->statusApproval;
            $row[] = $field->statusFaktur;
            $row[] = "<a href='' class='btn btn-success' data-toggle='modal' data-id='" . $field->id . "' data-target='#ModalUpdate2'>Update</a>";

            $data[] = $row;
        }
        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->pm->count_all(),
            "recordsFiltered" => $this->pm->count_filtered(),
            "data" => $data,
        );
        //output dalam format JSON
        echo json_encode($output);
    }

型号

defined('BASEPATH') or exit('No direct script access allowed');

// Export PM
class Pmmaster extends CI_Model
{
    public $table_user = 'mfaktur'; //nama tabel dari database id

    public $column_order = array(null, 'kdJenisTransaksi', 'fgPengganti', 'nomorFaktur', 'masaPajak', 'tahunPajak', 'tanggalFaktur', 'npwpPenjual', 'alamatPenjual', 'namaPenjual', 'npwpLawanTransaksi', 'namaLawanTransaksi', 'alamatLawanTransaksi', 'referensi', 'jumlahDpp', 'jumlahPpn', 'jumlahPpnbm', 'isWapu', 'isSupplierReg', 'statusApproval', 'statusFaktur'); //field yang ada di table user

    public $column_search = array('kdJenisTransaksi', 'fgPengganti', 'nomorFaktur', 'masaPajak', 'tahunPajak', 'npwpPenjual', 'alamatPenjual', 'namaPenjual', 'npwpLawanTransaksi', 'namaLawanTransaksi', 'alamatLawanTransaksi', 'referensi', 'isWapu', 'isSupplierReg', 'statusApproval', 'statusFaktur'); //field yang diizin untuk pencarian

    public $order = array('id' => 'asc'); // default order

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    private function _get_datatables_query()
    {

        $this->db->from($this->table_user);
        $where = "branch_id = CASE WHEN '" . $this->session->userdata('branch_id') . "' = '11' THEN branch_id ELSE '" . $this->session->userdata('branch_id') . "' END";
        $this->db->where($where);

        $i = 0;
        // looping awal
        foreach ($this->column_search as $item) {
            // jika datatable mengirimkan pencarian dengan metode POST
            if ($_POST['search']['value']) {
                // looping awal
                if ($i === 0) {
                    $this->db->group_start();
                    $this->db->like($item, $_POST['search']['value']);
                } else {
                    $this->db->or_like($item, $_POST['search']['value']);
                }

                if (count($this->column_search) - 1 == $i) {
                    $this->db->group_end();
                }
            }
            $i++;
        }

        if (isset($_POST['order'])) {
            $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)]);
        }
    }

    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($this->table);
        return $this->db->count_all_results();
    }

}

查看

    <!-- BEGIN : Main Content-->
    <div class="main-content">
        <div class="content-overlay"></div>
        <div class="content-wrapper">
            <div class="row">
                <div class="col-12">
                    <div class="content-header"><?=$title?></div>
                    <p class="content-sub-header mb-1">Data <b>PM </b>dalam bentuk tabel.</p>
                </div>
            </div>
            <!--Hoverable Rows Starts-->
            <section id="hoverable-rows">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title"><i class="fas fa-fw fa-table mr-2"></i>Data PM</h4>
                        </div>
                        <div class="row">
                            <div class="flash-updateStatus" data-flashupdatestatus="<?=$this->session->flashdata('message');?>"></div>
                            <?php
                                $message = $this->session->flashdata('message');
                                if (isset($message)) {
                                    $this->session->unset_userdata('message');
                                }
                                ?>
                        </div>
                        <div class="card-content">
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table id="tabel_all_pm" class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                                <th>No</th>
                                                <th>kdJenisTransaksi</th>
                                                <th>fgPengganti</th>
                                                <th>nomorFaktur</th>
                                                <th>masaPajak</th>
                                                <th>tahunPajak</th>
                                                <th>tanggalFaktur</th>
                                                <th>npwpPenjual</th>
                                                <th>alamatPenjual</th>
                                                <th>namaPenjual</th>
                                                <th>npwpLawanTransaksi</th>
                                                <th>namaLawanTransaksi</th>
                                                <th>alamatLawanTransaksi</th>
                                                <th>referensi</th>
                                                <th>jumlahDpp</th>
                                                <th>jumlahPpn</th>
                                                <th>jumlahPpnbm</th>
                                                <th>isWapu</th>
                                                <th>isSupplierReg</th>
                                                <th>statusApproval</th>
                                                <th>statusFaktur</th>
                                                <th>Aksi</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>No</th>
                                                <th>kdJenisTransaksi</th>
                                                <th>fgPengganti</th>
                                                <th>nomorFaktur</th>
                                                <th>masaPajak</th>
                                                <th>tahunPajak</th>
                                                <th>tanggalFaktur</th>
                                                <th>npwpPenjual</th>
                                                <th>alamatPenjual</th>
                                                <th>namaPenjual</th>
                                                <th>npwpLawanTransaksi</th>
                                                <th>namaLawanTransaksi</th>
                                                <th>alamatLawanTransaksi</th>
                                                <th>referensi</th>
                                                <th>jumlahDpp</th>
                                                <th>jumlahPpn</th>
                                                <th>jumlahPpnbm</th>
                                                <th>isWapu</th>
                                                <th>isSupplierReg</th>
                                                <th>statusApproval</th>
                                                <th>statusFaktur</th>
                                                <th>Aksi</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
    <!--Hoverable rows Ends-->
        </div>
    </div>
<!-- END : End Main Content-->

<div class="modal fade text-left" id="ModalUpdate2" tabindex="-1" role="dialog" aria-labelledby="ModalUpdateLabel8" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header bg-success">
                <h4 class="modal-title" id="ModalUpdateLabel8"><i class="ft-user-plus mr-2"></i>Update Status Faktur</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true"><i class="ft-x font-medium-2 text-bold-700"></i></span>
                </button>
            </div>
            <form class="" action="<?=base_url('master/updateStatus');?>" method="post">
                <div class="modal-body">
                <div class="form-group">
                        <label><span style="color:red;">*</span> STATUS FAKTUR SAAT INI : </label>
                        <input type="hidden" class="form-control" id="id" name="id" readonly>
                        <input type="text" class="form-control" id="statusFaktur" name="statusFaktur" readonly>
                        <?=form_error('username', '<small class="text-danger">', '</small>')?>
                    </div>
                    <div class="form-group">
                        <label><span style="color:red;">*</span> STATUS FAKTUR DARI DJP : </label>
                        <!-- <input type="text" class="form-control" id="url" name="url" readonly> -->
                        <input type="text" class="form-control" id="statusFakturBaru" name="statusFakturBaru" readonly>
                        <?=form_error('username', '<small class="text-danger">', '</small>')?>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn bg-light-success" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-success">UPDATE</button>
                </div>
            </form>
        </div>
    </div>
</div>
    <!-- END : Modalbox Add new menu-->

Javascript

$(document).ready(function() {
  // Setup - add a text input to each footer cell
  $('#tabel_all_pm tfoot th').each(function() {
    var title = $(this).text();
    $(this).html('<input type="text" placeholder="Search ' + title + '" />');
  });

  var table = $('#tabel_all_pm').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": url +"master/get_data_pm",
    initComplete: function() {
      var api = this.api();

      // Apply the search
      api.columns().every(function() {
        var that = this;

        $('input', this.footer()).on('keyup change', function() {
          if (that.search() !== this.value) {
            that
              .search(this.value)
              .draw();
          }
        });
      });
    }
  });
});
javascript php jquery datatables server-side
1个回答
-1
投票

我的模型上的代码就像这样,它的工作原理!

    public function _get_datatables_query($nama, $username)
{
    $this->db2->from('VW_PIPING_DRAWING A');
    $this->db2->join('VW_USER_JOB B', 'A.PROJECT_NO = B.JOB_USER');
    $this->db2->join('VW_USER_MON C', 'B.ID_USER = C.ID_USER');
    $this->db2->where('C.NAMA_USER', $nama);
    $this->db2->where('C.USERNAME', $username);

    $searchValue = $_POST['search']['value'];

    if ($searchValue) {
        // Convert search value to uppercase and split into individual terms
        $searchTerms = explode(' ', strtoupper($searchValue));

        // Exact match condition for each term against each column
        $conditions = array();
        foreach ($searchTerms as $term) {
            $term = trim($term); // Remove any extra spaces around terms

            // Skip empty terms
            if (!empty($term)) {
                $or_conditions = array();
                foreach ($this->column_search as $item) {
                    $or_conditions[] = "UPPER($item) LIKE '$term%'";
                }
                $conditions[] = '(' . implode(' OR ', $or_conditions) . ')';
            }
        }

        $this->db2->where('(' . implode(' AND ', $conditions) . ')');
    }

    if (isset($_POST['order'])) {
        $columnIndex = $_POST['order']['0']['column'];
        $columnName = $this->column_order[$columnIndex];
        $orderDirection = $_POST['order']['0']['dir'];
        $this->db2->order_by($columnName, $orderDirection);
    }

    return $this->db2;
}


function get_datatables($nama,$username,$searchValue)
{
    $this->_get_datatables_query($nama,$username,$searchValue);
    if ($_POST['length'] != -1) {
        $this->db2->limit($_POST['length'], $_POST['start']);
    }
    $query = $this->db2->get();
    return $query->result();
}

public function count_filtered($nama,$username,$searchValue)
{
    $this->_get_datatables_query($nama,$username,$searchValue);
    $query = $this->db2->get();
    return $query->num_rows();
}

public function count_all($nama,$username,$searchValue)
{
    $this->_get_datatables_query($nama,$username,$searchValue);
    return $this->db2->count_all_results();
}
`
© www.soinside.com 2019 - 2024. All rights reserved.