How to filter results and reload the/view page in Codeigniter?

Asked

Viewed 3,089 times

3

I have a code in Codeigniter that takes a value of a select dropbox in a view and leads to a second view showing the results in a table. In this second view, I kept the dropbox and the button of submit ("Filter") and would like the user to be able to select another value from dropbox and filter by reloading the page with other results to be shown 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 each refresh or something of the kind, but for the time being unsuccessful. 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-->

2 answers

1

Your form points to the method listar class TrackerCtrl where the countryside frota_list is not considered popular the variable user_trackers_list through the method get_trackers class TrackerModel.

Maybe you should change the method get_trackers class TrackerModel to accept a second parameter (eg.: frota_item) and add a second clause where to filter through this field (ex.: $this->db->where('tracker_veiculo.frota_item', $frota_item);).

You can do this conditionally so as not to change the current functioning. 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';
    ...
}
...

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 little Jquery and use the Ajax function to bring the data dynamically without giving the 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 the head delimiters

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

Now, at the end of your page, before the /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 came to your controller via POST, so do what you want with it and return it like this:

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

I think this optimizes your filtration system.

Anything we are there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.