首页 > 解决方案 > 如何从表中删除表过滤器搜索 - 使用 codeigniter 3 的 ajax

问题描述

所以我尝试了这个教程:https ://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html

我的数据显示如下:数据和功能外观的图像

我只想显示表格并使用图片中绿色标志上的搜索功能。但是,当我按照教程进行操作时,会显示一些功能(例如:图片中的红色和蓝色标志)。

我在哪里可以更改以去除图片中的红色特征并将图片中的蓝色标志特征移动到绿色标志?

我仍在学习 ajax 的工作原理。对不起,如果我的解释太复杂。但是,我希望你明白......希望你能帮助我......

这是我在模型中使用的函数 - detkelModel.php

var $table = 'detail_keluarga';
    var $column_order = array('nama', 'hubungan', 'personilid', 'lahir', null); //set column field database for datatable orderable
    var $column_search = array('nama', 'hubungan', 'personilid', 'lahir'); //set column field database for datatable searchable just nama, hubungan, personilid, lahir are searchable
    var $order = array('id' => 'desc'); // default order

    private function _get_datatables_query()
    {

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

        $i = 0;

        foreach ($this->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->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();
    }

这是我对表格和搜索栏的看法 - detkel.php

<div class="navbar navbar-expand navbar-white navbar-light">
    <!--left navbar / Data Keluarga label-->
    <div class="col-sm-6 float-left">
        <h5>Data Keluarga Personil</h5>
    </div>
    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">
        <!-- Navbar Search, filter, plus data -->
        <li class="nav-item">
            <div class="form-inline">
                <div class="input-group" data-widget="search-bar">
                </div>
            </div>
        </li>
        <!-- Filter Dropdown Menu -->
        <li>
        </li>
        <!-- Plus data dropdown menu -->
        <li>
            <a class="btn btn-default" data-toggle="dropdown" href="#">
            </a>
            <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
            </div>
        </li>
    </ul>
</div>
<div class="card">
    <div class="table-responsive p-0">
        <table id="table" class="table table-hover text-nowraps">
            <thead>
                <tr>
                    <th>Nama</th>
                    <th>id personil</th>
                    <th>Hubungan</th>
                    <th>Tanggal Lahir</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>

这是我认为的脚本

<script type="text/javascript">
    var save_method; //for save method string
    var table;
    $(document).ready(function() {
        //datatables
        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('DetkelController/ajax_list') ?>",
                "type": "POST"
            },
            //Set column definition initialisation properties.
            "columnDefs": [{
                "targets": [-1], //last column
                "orderable": false, //set not orderable
            }, ],
        });
    });
</script>

这是我的控制器DetkelController.php中的 ajax_list() :

public function ajax_list()
{
    $list = $this->dk->get_datatables();
    $data = array();
    //$no = $_POST['start'];
    foreach ($list as $dk) {
        //$no++;
        $row = array();
        $row[] = $dk->nama;
        $row[] = $dk->hubungan;
        $row[] = $dk->personilid;
        $row[] = $dk->lahir;

        //add html for action
        $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person(' . "'" . $dk->id . "'" . ')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
                <a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person(' . "'" . $dk->id . "'" . ')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';

        $data[] = $row;
    }

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

标签: phpjqueryajaxcodeignitertablefilter

解决方案


推荐阅读