Codeigniter和数据表搜索过滤器列问题

时间:2018-09-21 20:45:46

标签: php datatables

你好,我有一个小问题, 我跟随a tutorial用codeigniter制作了一个数据表,接下来我尝试用this tutorial实现一个搜索列。

我的观点:

<div id="page-wrapper">
    <div class="card">
        <div class="card-header corsi">
            <h3 class="page-header"><i class="fas fa-graduation-cap"></i> <?=$title?></h3>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-xl-12">
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12"> 
                    <table class="table table-striped table-bordered table-hover" id="scadenze_data" style="width:100%">
                        <thead>  
                            <tr>  
                                <th width="7%">Cod. Corso</th>
                                <th width="9%">Nome</th>  
                                <th width="10%">Cognome</th>
                                <th width="7%">Codice Fiscale</th>
                                <th width="7%">Azienda</th>
                                <th width="1%">Edizione</th>
                                <th width="1%">
                                    <select name="profilo" id="profilo" class="form-control">
                                        <option value="">Profilo Search</option>
                                        <?php 
                                            foreach($profili as $prof) {
                                                echo '<option value="'.$prof->CODICE.'">'.$prof->CODICE.'</option>';
                                            }
                                        ?>
                                    </select>                      
                                </th>
                                <th width="9%">Periodo dal</th>
                                <th width="9%">Periodo al</th>  
                                <th width="9%">N. Attestato</th> 
                                <th width="9%">Data attestato</th> 
                                <th width="9%">Scadenza</th>
                                <th width="9%">Attestati</th>  
                            </tr>  
                        </thead>
                    </table>  
                </div>  
            </div> 
        </div>
        <div class="card-footer">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-6">
                        <input class="btn btn-secondary" src="" value="<- Torna indietro">
                    </div>
                </div>  
            </div>
        </div>
    </div>              
</div>
<script type="text/javascript" language="javascript" > 
    $(document).ready(function(){  
        load_data();

        function load_data(is_profilo) {
            var dataTable = $('#scadenze_data').DataTable({ 
                "searchable": true,
                "Paginate": true, 
                "processing":true,  
                "serverSide":true,  
                "order":[],  
                "ajax":{  
                    url:"<?php echo base_url().'corsi/fetch_scadenze'; ?>",  
                    type:"POST",
                    data:{is_profilo:is_profilo}
                },  
                "columnDefs":[  
                    {  
                        "targets":[12],  
                        "orderable":false,  
                    },
                    {  
                        "targets":[6],  
                        "orderable":false,  
                    },
                ],
                "deferRender": true,
                "lengthMenu":[[10,25,50,100,-1],[10,25,50,100,"All"]],
                "pageLength":10,
                "pagingType": "full_numbers",
            }); 
        }

        $(document).on('change', '#profilo', function(){
            var profilo = $(this).val();
            $('#scadenze_data').DataTable().destroy();
            if(profilo != '') {
                load_data(profilo);
            } else {
                load_data();
            }
        });
    }); 
</script> 

我的控制器:

function fetch_scadenze(){ 
    $this->load->helper('form');
    $this->load->model("scadenze_model");
    $fetch_data = $this->scadenze_model->make_datatables();  
    $data = array();  
    foreach($fetch_data as $row) {  
        $dal = date("d/m/Y", strtotime($row->PERIODO_DAL));
        $al = date("d/m/Y", strtotime($row->PERIODO_AL));
        $att = date("d/m/Y", strtotime($row->DATA_ATTESTATO));
        $scadenza = date("d/m/Y", strtotime($row->DATA_SCADENZA));
        $sub_array = array(); 
        $sub_array[] = $row->CORSOCODICE;
        $sub_array[] = $row->NOME;  
        $sub_array[] = $row->COGNOME;
        $sub_array[] = $row->CF;
        $sub_array[] = $row->DENOMIN_RAG_SOC;
        $sub_array[] = $row->EDIZIONE;
        $sub_array[] = $row->CODICE;
        $sub_array[] = $dal;
        $sub_array[] = $al;
        $sub_array[] = $row->N_ATTESTATO_PROGRESSIVO;
        $sub_array[] = $att;
        $sub_array[] = $scadenza;
        $sub_array[] = "<div class='btn-group' role='group' aria-label='Button group with nested dropdown'>...</div>";  
        $data[] = $sub_array;  
    }  
    $output = array(  
        "draw" => intval($_POST["draw"]),  
        "recordsTotal" => $this->scadenze_model->get_all_data(),  
        "recordsFiltered" => $this->scadenze_model->get_filtered_data(), 
        "data" => $data
    );
    echo json_encode($output);
}

还有我的模态:

if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class Scadenze_model extends CI_Model  
{  
    var $table = "corso co";  
    var $select_column = array("cu._ID","ut.NOME","ut.COGNOME","ut.CF","az.DENOMIN_RAG_SOC","co.EDIZIONE","pr.CODICE","co.CORSOCODICE","co.PERIODO_DAL","co.PERIODO_AL","cu.N_ATTESTATO_PROGRESSIVO","co.DATA_ATTESTATO","co.DATA_SCADENZA","cu.FILE");
    var $order_column = array("co.CORSOCODICE","ut.NOME","ut.COGNOME","ut.CF","az.DENOMIN_RAG_SOC","co.EDIZIONE","pr.CODICE","co.PERIODO_DAL","co.PERIODO_AL","cu.N_ATTESTATO_PROGRESSIVO","co.DATA_ATTESTATO","co.DATA_SCADENZA", null);

    function make_query()  
    {  
        $this->db->select($this->select_column);  
        $this->db
            ->from($this->table)
            ->join("corsi_utenti cu", "co._ID = cu.ID_CORSO", "left")
            ->join("azienda az", "cu.ID_AZIENDA = az._ID", "left")
            ->join("utente ut", "cu.ID_UTENTE = ut._ID", "left")
            ->join("profilo pr", "co.ID_PROFILO = pr._ID", "left");

        if(isset($_POST["is_profilo"])) 
        {
            //echo "<script type='text/javascript'>console.log('prova');</script>";
            $this->db->or_like("pr.CODICE", $_POST["is_profilo"]);
            //$this->db->or_like("pr.CODICE", $_POST["is_profilo"]["value"]);
            //$query .= "product.category = '".$_POST["is_category"]."' AND ";
        }
        if(isset($_POST["search"]["value"]))  
        {   //echo "<script>alert('There are no fields to generate a report');</script>";
            $this->db->like("co.CORSOCODICE", $_POST["search"]["value"]);
            $this->db->or_like("ut.NOME", $_POST["search"]["value"]);
            $this->db->or_like("ut.COGNOME", $_POST["search"]["value"]);
            $this->db->or_like("ut.CF", $_POST["search"]["value"]);
            $this->db->or_like("az.DENOMIN_RAG_SOC", $_POST["search"]["value"]);
            $this->db->or_like("co.EDIZIONE", $_POST["search"]["value"]);
            $this->db->or_like("pr.CODICE", $_POST["search"]["value"]);
            $this->db->or_like("co.PERIODO_DAL", $_POST["search"]["value"]);
            $this->db->or_like("co.PERIODO_AL", $_POST["search"]["value"]);
            $this->db->or_like("cu.N_ATTESTATO_PROGRESSIVO", $_POST["search"]["value"]);
            $this->db->or_like("co.DATA_ATTESTATO", $_POST["search"]["value"]);
            $this->db->or_like("co.DATA_SCADENZA", $_POST["search"]["value"]);
        } 
        if(isset($_POST["order"]))  
        {  
            $this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);  
        } else {  
            $this->db->order_by('co.DATA_ATTESTATO', 'DESC');
        }  
    }  
    function make_datatables()
    {
        $this->make_query();

        // $this->db->limit(10,1);
        if($_POST['length'] != -1)  
        {  
            $this->db->limit($_POST['length'], $_POST['start']);  
        }
        $query = $this->db->get();  
        return $query->result();  
    }  
    function get_filtered_data(){  
        $this->make_query();  
        $query = $this->db->get();  
        return $query->num_rows();  
    }       
    function get_all_data()  
    {  
        $this->db->select("*");  
        $this->db->from($this->table);  
        return $this->db->count_all_results();  
    }


    function insert_log($activity, $module){
        $id = $this->session->userdata('user_id');

        $data = array(
            'fk_user_id' => $id,
            'activity'   => $activity,
            'module'     => $module,
            'created_at' => date('Y\-m\-d\ H:i:s A')
        );
        $this->db->insert('activity_log', $data);
    }

    //FACCIO UPDATE DELLA TABELLA corsi_utenti in FILE        
    function set_url_attestato($id, $file){
        $this->db->set('FILE', $file);
        $this->db->where('_ID', $id);
        $this->db->update('corsi_utenti');        
    }
}

当我选择下拉列表以选择“ profilo”时,我的数据表会刷新但不起作用。

谢谢。

2 个答案:

答案 0 :(得分:0)

您没有正确绑定事件侦听器。试试这个:

_, a, _, b = [1,2,3,4]

答案 1 :(得分:0)

删除load_data函数上的serverSide。

<script type="text/javascript" language="javascript" > 
$(document).ready(function(){  
    load_data();

    function load_data(is_profilo) {
        var dataTable = $('#scadenze_data').DataTable({ 
            "searchable": true,
            "Paginate": true, 
            "processing":true,  
            //"serverSide":true,  <--serverside block all
            "order":[],  
            "ajax":{  
                url:"<?php echo base_url().'corsi/fetch_scadenze'; ?>",  
                type:"POST",
                data:{is_profilo:is_profilo}
            },  
            "columnDefs":[  
                {  
                    "targets":[12],  
                    "orderable":false,  
                },
                {  
                    "targets":[6],  
                    "orderable":false,  
                },
            ],
            "deferRender": true,
            "lengthMenu":[[10,25,50,100,-1],[10,25,50,100,"All"]],
            "pageLength":10,
            "pagingType": "full_numbers",
        }); 
    }

    $(document).on('change', '#profilo', function(){
        var profilo = $(this).val();
        $('#scadenze_data').DataTable().destroy();
        if(profilo != '') {
            load_data(profilo);
        } else {
            load_data();
        }
    });
});