How to filter results and reload page / view in CodeIgniter?

3

I have code in CodeIgniter which takes a value of select dropbox in a view and leads to a second view showing the results in a table. In that second view , I kept the dropbox and the submit ("Filter") button and would like the user to select another value from dropbox and filter, reloading the page with other results to show in the table. I did this, but the page is not reloading. I believe I should put something at the end of the URL to be different from every refresh or something, but for now without success. Any suggestions?

Model:

class TrackerModel extends CI_Model {
    function get_coordenadas($cliente, $filtro, $num, $offset){
        //SELECT * FROM 'tracker_coordenada','tracker_veiculo' WHERE tracker_coordenada.veiculo_codigo = tracker_veiculo.veiculo_codigo AND tracker_veiculo.cli_cod = $cliente
        $this->db->select('tracker_coordenada.*');
        $this->db->from('tracker_coordenada, tracker_veiculo');
        $this->db->where('tracker_coordenada.veiculo_codigo = tracker_veiculo.veiculo_codigo');
        $this->db->where('tracker_veiculo.cli_cod',$cliente);
        if($filtro !=NULL){
            $this->db->where('tracker_veiculo.descricao',$filtro);
        }
        $query = $this->db->get('',$num, $offset);
        return $query->result();
        //$sql = "SELECT tracker_coordenada.* FROM 'tracker_coordenada','tracker_veiculo' WHERE tracker_coordenada.veiculo_codigo = tracker_veiculo.veiculo_codigo AND tracker_veiculo.cli_cod = ".$cliente;
        //$resultado = $this->db->query($sql);
        //return $resultado->result_array();
    }
    function conta_coordenadas($cliente){
        //$query = $this->db->query("SELECT COUNT(ID) AS total FROM coordenadas WHERE news_id = $news_id");
        //$row = $query->row();
        //echo $row->total;
        $this->db->where('tracker_veiculo.cli_cod', $cliente);
        $this->db->where('tracker_veiculo.veiculo_codigo', 'tracker_coordenada.veiculo_codigo');
        $query = $this->db->count_all('tracker_coordenada');
        return $query;
    }
    function get_trackers($cliente){
        $this->db->select('*');
        $this->db->from('tracker_veiculo');
        $this->db->where('tracker_veiculo.cli_cod',$cliente);
        $query = $this->db->get();
        return $query->result();
    }
}

Controller:

<?php
class TrackerCtrl extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('tracker/TrackerModel');
        $this->load->model('FuncoesModel');
    $this->load->library('table');
    $this->load->language("label");
    $this->load->helper('form');
    }
    function index(){
    if ($this->FuncoesModel->VerificaPermissao("listar", "tracker") != "S"){
        redirect('principal/principalctrl/acessonegado');   
    }           
    $dados['titulo'] =  $this->lang->line('Tracker');
    $cliente = $this->session->userdata("par_cli_codigo");
    $dados['user_trackers_list'] = $this->TrackerModel->get_trackers($cliente);
    $dados['dinamico'] = '/tracker/TrackerView';
    $dados['filtro'] = $this->input->post('frota_list');
    $this->load->vars($dados);
    $this->load->view('/principal/PrincipalView');
    }
    function listar($dados=""){
    $dados['titulo'] =  $this->lang->line('Tracker');
    $cliente = $this->session->userdata("par_cli_codigo");
    $dados['filtro'] = $this->input->post('frota_list');
    $dados['user_trackers_list'] = $this->TrackerModel->get_trackers($cliente);
    $dados['dinamico'] = '/tracker/TrackerRelatorioView';
    //Paginacao
    $config['base_url'] =  base_url().'index.php/tracker/trackerctrl/listar/';
    $config['total_rows'] = $this->TrackerModel->conta_coordenadas($cliente);
    $config['per_page'] = 5;
    $config['uri_segment'] = 4;
    $config['first_link'] = $this->lang->line('Primeiro');
    $config['last_link'] = $this->lang->line('&Uacute;ltimo');
    $config['next_link'] = $this->lang->line('Pr&oacute;ximo');
    $config['prev_link'] = $this->lang->line('Anterior');           
    $dados['get_coordenadas'] = $this->TrackerModel->get_coordenadas($cliente, $this->input->post('frota_list'), $config['per_page'],$this->uri->segment(4));
    $this->pagination->initialize($config);
    $dados["paginacao"] =  $this->pagination->create_links();   
    $this->load->vars($dados);
    $this->load->view('/principal/PrincipalView');
    }
}

View:

<? 
if($this->session->flashdata('msg')){
?>
    <div style="background:#FF0000; text-align:center" align="center">
        <font color="#FFFFFF"><?=$this->session->flashdata('msg')?></font>
    </div>
<? 
}
?>
<div class="row-fluid sortable">        
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="icon-eject"></i><span class="break"></span><?=$titulo?></h2>
            <div class="box-icon">
                <a href="#" class="btn-minimize"><i class="icon-chevron-up"></i></a>
            </div>
        </div>
    <div class="box-content">
        <div class="control-group">
            <label class="control-label" for="typeahead"><font color="#FF0000"></font><?=$this->lang->line('Ve&iacute;culo')?>:</label>
                <div class="controls">
                    <form id="form" name="form" method="post" action="<?=site_url('tracker/trackerctrl/listar/')?>">
                        <select name="frota_list" id="frota_list" style="width:300px"  >
                            <? foreach($user_trackers_list as $opcao){?>
                                <option><?=$opcao->descricao?></option>
                            <? }?>
                        </select>
                        <br><br>
                        <button class="btn btn-small btn-primary" type="submit" id="btn_filtrar" name="btn_filtrar"><?=$this->lang->line('Filtrar')?></button>
                        <?=form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash());?> 
                    </form>
                </div>
        </div>
    </div><!--/span-->

</div><!--/row-->
    
asked by anonymous 17.03.2014 / 13:11

2 answers

1
Your form points to the listar method of the TrackerCtrl class where the frota_list field is not considered to be the variable user_trackers_list through the get_trackers method of the TrackerModel class.

Maybe you should change the get_trackers method of class TrackerModel to accept a second parameter (eg, frota_item ) and add a second where clause to filter by that field (eg% ).

You can do this conditionally, so as not to change the current operation. Ex.:

Model:

...
function get_trackers($cliente, $frota_item = false){
    ...
    $this->db->where('tracker_veiculo.cli_cod', $cliente);
    if($frota_item){
        $this->db->where('tracker_veiculo.frota_item', $frota_item);
    }
    $query = $this->db->get();
    ...
}
...

Controller:

...
function listar($dados=""){
    ...
    $dados['filtro'] = $this->input->post('frota_list');
    $dados['user_trackers_list'] = $this->TrackerModel->get_trackers($cliente, $this->input->post('frota_list'));
    $dados['dinamico'] = '/tracker/TrackerRelatorioView';
    ...
}
...
    
25.03.2014 / 15:25
0

From what I understand, you're passing values between views to keep the selected one and then doing the filtering, right? Why not use a bit of Jquery and use the Ajax function to dynamically fetch data without refresh? So every time the user selects something in the select dropbox it calls the page that filters the data. I'll give you the code.

Add Jquery to head delimiters

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

Now at the end of your page, before / body add:

<script>
    $( 'select' ).change( function () {

        var idItem  =  $( this ).val();

        pesquisar( idItem )

    });


    pesquisar = function ( idItem ) {

        $.ajax({

            type: 'POST',

            url: "sua_base_url/controler/metodo",

            data: 'idItem=' + idItem, 

            success: function (data) {

                $( 'tabela' ).html( data );

            },

            error: function(request, status, error){

                alert(request.responseText);

            }

        });

    }    

This data has reached your controller via POST, so do whatever you want with it and return it like this:

$ this-> parser-> parse ('ajax / your_page', $ data);

I think this optimizes your filtering system.

Anything is there.

    
31.07.2014 / 23:22