How to make a datatables data be clicked by calling another view?

Asked

Viewed 195 times

0

Good afternoon guys. Follow the code of my view, which is working fine. It turns out that I would like to do the number of the process (2º <td>), be clickable by calling another view. It is possible?

View code:

<head>   
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.css"/>
    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.js"></script> 
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>                
</head>
<div class="col-md-12">  
    <div class="row">
        <br>
        <div class="col-md-12">
            <p><font  size="3" face="helvetica"><strong>LISTA DE PROCESSOS</strong></font></p>                                                                           
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 ">   
                <button type="button"  title="Imprimir" class="btn btn-primary  hidden-print" onclick="myFunction()">
                    <span class="glyphicon glyphicon-print" aria-hidden="true"></span></button>            
                <button  type="button" title="Cadastrar Processo" onclick="return window.location.href = '<?= base_url('processo/cadastro/') ?>'" class="btn btn-primary btn-sm hidden-print"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>  PROCESSO</button>                                                                                                
        </div> 
    </div> 
    <br>
    <div class="row">        
        <div class="col-md-12">
            <table id="book-table" >
                <thead>
                    <tr>
                        <td><b>Cliente</b></td>
                        <td><b>Nº do Processo</b></td>                
                        <td><b>Parte</b></td>
                        <td><b>Pasta</b></td>
                        <td><b>Valor da causa</b></td>
                        <td><b>Situação</b></td>
                    </tr>
                </thead>
                <tbody>               
                </tbody>
            </table> 
        </div> 
    </div> 
</div>  
<script type="text/javascript">
    $(document).ready(function () {
        $('#book-table').DataTable({
            "autowidth": false,
            "ajax": {
                url: "<?php echo site_url("processo/processos_page") ?>",
                type: 'GET'
            }
        });
    });
</script>
<script>
    function myFunction() {
        window.print();
    }
</script>

Controller Function Code (Process):

public function processos_page() {

        // Datatables Variables
        $draw = intval($this->input->get("draw"));
        $start = intval($this->input->get("start"));
        $length = intval($this->input->get("length"));


        $processo = $this->Processo_model->getAll();

        $data = array();

        foreach ($processo->result() as $proc) {

            $data[] = array(
                $proc->nomecliente,
                $proc->nprocesso,
                $proc->nomeparte,                
                $proc->pastaescritorio,
                $proc->valorcausa,
                $proc->situacao
            );
        }

        $output = array(
            "draw" => $draw,
            "recordsTotal" => $processo->num_rows(),
            "recordsFiltered" => $processo->num_rows(),
            "data" => $data
        );
        echo json_encode($output);
        exit();
    }

1 answer

1

Yes, does so

public function processos_page() {
$processo = $this->Processo_model->getAll();
$data = array();
$no = $_POST['start'];
foreach ($processo as $proc) {
    $no++;
    $row = array();

    $row[] =  $proc->nomecliente;
    $row[] =  " <div align='center'> <a  href='" . base_url() . "processor/ver/" . $ $proc->nprocesso . "'class='btn btn-success' ><i class='fa fa-search'></i></a> </div>";
    $row[] =   $proc->nomeparte;                
    $row[] =   $proc->pastaescritorio;
    $row[] =   $proc->valorcausa;
    $row[] =   $proc->situacao;


      $data[] = $row;
}

$output = array(
    "draw" => $_POST['draw'],
    "recordsTotal" => $this->processo->count_all(),
    "recordsFiltered" => $this->processo->count_filtered(),
    "data" => $data,
);

echo json_encode($output);

}

model, change your model if it is different for you to use Datatables in the correct way

//Datatables

var $table = "nome da sua tabela";
var $column = array('nome', 'cpf', 'data', 'rg', 'etc');
var $order = array('id' => 'asc', 'nome' => 'asc', 'cpf' => 'asc', 'data' => 'asc', 'rg' => 'asc');

private function _get_datatables_query() {
    $this->db->from($this->table);

    $i = 0;
    foreach ($this->column as $item) {
        if ($_POST['search']['value'])
            ($i === 0) ? $this->db->like($item, $_POST['search']['value']) : $this->db->or_like($item, $_POST['search']['value']);
        $column[$i] = $item;
        $i++;
    }

    if (isset($_POST['order'])) {
        $this->db->order_by($column[$_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();
}

Script to call Datatables

<script type="text/javascript">

        var save_method; //for save method string
        var table;
        $(document).ready(function () {
            table = $('#book-table').DataTable({

                "processing": true, //Feature control the processing indicator.
                "serverSide": true, //Feature control DataTables' server-side processing mode.


                // Load data for the table's content from an Ajax source
                "ajax": {
                    "url": "<?php echo site_url('processo/processos_page') ?>",
                    "type": "POST"
                },
                "language": {
                    "sEmptyTable": "Nenhum registro encontrado",
                    "sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
                    "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
                    "sInfoFiltered": "(Filtrados de _MAX_ registros)",
                    "sInfoPostFix": "",
                    "sInfoThousands": ".",
                    "sLengthMenu": "_MENU_ resultados por página",
                    "sLoadingRecords": "Carregando...",
                    "sProcessing": "Processando...",
                    "sZeroRecords": "Nenhum registro encontrado",
                    "sSearch": "Pesquisar",
                    "oPaginate": {
                        "sNext": "Próximo",
                        "sPrevious": "Anterior",
                        "sFirst": "Primeiro",
                        "sLast": "Último"
                    },
                    "oAria": {
                        "sSortAscending": ": Ordenar colunas de forma ascendente",
                        "sSortDescending": ": Ordenar colunas de forma descendente"
                    }
                },

                //Set column definition initialisation properties.
                "columnDefs": [
                    {
                        "targets": [-1], //last column
                        "orderable": false, //set not orderable
                    },
                ],

            });
        });


        function reload_table()
        {
            table.ajax.reload(null, false); //reload datatable ajax
        }

    </script>
  • that way it didn’t work either, I don’t use the Model class for that View. Maybe the problem is on this line: $Row[] = " <div align='center'> <a href='" . base_url() . "Processor/ver/" . $ $proc->nprocesso . " 'class='btn btn-Success' ><i class='fa fa fa-search'></i></a> </div>";What would be Processor/ver/

  • Netbeans gave this message regarding this line: $no = $_POST['start']; "Do Not Access Superglobal Array $_Post Directly.

  • And your controller to open the view to view the process,

  • Create and model class and add the codes I’ve put that will work, this is the way that the guys from the datatables teach in their forum.

  • I added the script for you to put in your view file to call the datatables

  • It’s tense, I’m not getting it :(

Show 1 more comment

Browser other questions tagged

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