dongwen1909 2018-09-21 20:45 采纳率: 0%
浏览 63
已采纳

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

Hello i have a little problem, i followed a tutorial to make a datatable with codeigniter and next i tried to implement a search Column with this tutorial.

My view:

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

My controller:

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);
}

And my Modal:

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');        
    }
}

My datatable refreshes but doesn't work when i select the dropdown to choose the "profilo".

Thx.

  • 写回答

2条回答 默认 最新

  • dstjh46606 2018-09-23 23:37
    关注

    Remove serverSide on load_data function.

    <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();
            }
        });
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题