Datatables Jquery

Asked

Viewed 80 times

0

I can list the data, but I can’t filter the data, my query is right, returns the json, but I get a processing message.

  • I’m using the slim for the routes.

    inserir a descrição da imagem aqui

my call:

<script>
$(document).ready(function() {
    $('#example').DataTable({
        "language": {
            "processing": "Processando",
            "search": "Pesquisar",
            "info": "Mostrando página _PAGE_ de _PAGES_",
            "paginate": {
                "first": "Primeira",
                "previous": "Anterior",
                "next": "Próxima",
                "last": "Última"
            },
        },

        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "/vehicles/out/test",
            "type": 'POST',

        },
    });

});

my route

$app->post('/vehicles/out/test', function () {

$charge = new Charge();

$chargesToPay = $charge->chargeToPay($_REQUEST);

$data = array();
foreach ($chargesToPay as $chargeToPay) {

    $data[] = [
        $chargeToPay['idcharge'],
        $chargeToPay['cha_tag'],
        $chargeToPay['cha_date_entry'],
        $chargeToPay['cha_obs'],
        $chargeToPay['mod_name'],
        $chargeToPay['bra_name'],
        $chargeToPay['typ_vehicle_name'],
        $chargeToPay['monthly'],
        $chargeToPay['edit'] = '<button type="button" id="getEdit" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal" data-id='.$chargeToPay['idcharge'].'><i class="glyphicon glyphicon-pencil">&nbsp;</i>Edit</button>
            <a href="index.php?delete="' .$chargeToPay['idcharge']. 'onclick="return confirm("Are You Sure") class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-trash">&nbsp;</i>Delete</a>',

    ];
}


$json_data=array(
    "draw"              =>  intval(1),
    "recordsTotal"      =>  intval(1),
    "recordsFiltered"   =>  intval(1),
    "data"              =>  $data
);

echo json_encode($json_data); exit;

});

 public  function chargeToPay($request)
{
    $sql = new Sql();

    $query = "SELECT    a.idcharge,
                            a.cha_tag,
                            a.cha_date_entry,
                            a.cha_obs,
                            b.mod_name,
                            c.bra_name,
                            d.typ_vehicle_name,
                            IFNULL(e.mon_name, '') as monthly

                            FROM izpg_tbl_charge AS a
                            JOIN izpg_tbl_model b ON b.idmodel = a.id_model
                            JOIN izpg_tbl_brand c ON c.idbrand = a.id_brand
                            JOIN izpg_tbl_type_vehicle d ON d.idtypevehicle = a.id_type_vehicle 
                            LEFT JOIN izpg_tbl_monthly e on e.idmonthly = a.id_monthly
                            WHERE '1' = '1'";


    if(!empty($request['search']['value'])){

        $query.=" AND (idcharge Like '".$request['search']['value']."%')";

        return $sql->select($query);

    }

    return $sql->select($query);

}

the json that returns when I search

{"draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[["1","ens-7758","2019-02-11 20:49:37","obs","INTEGRA","CHEVROLET","CARRO","","<button type=\"button\" id=\"getEdit\" class=\"btn btn-primary btn-xs\" data-toggle=\"modal\" data-target=\"#myModal\" data-id=1><i class=\"glyphicon glyphicon-pencil\">&nbsp;<\/i>Edit<\/button>\r\n                <a href=\"index.php?delete=\"1onclick=\"return confirm(\"Are You Sure\") class=\"btn btn-danger btn-xs\"><i class=\"glyphicon glyphicon-trash\">&nbsp;<\/i>Delete<\/a>"]]}

table

            <table id="example" class="cell-border" cellspacing="0" width="100%">
                <thead>
                <tr>
                    <th id="id">id</th>
                    <th>Placa</th>
                    <th>Entrada</th>
                    <th>obs</th>
                    <th>Modelo</th>
                    <th>Marca</th>
                    <th>Tipo</th>
                    <th>Mensalista</th>
                    <th>Ação</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th>id</th>
                    <th>Placa</th>
                    <th>Entrada</th>
                    <th>obs</th>
                    <th>Modelo</th>
                    <th>Marca</th>
                    <th>Tipo</th>
                    <th>Mensalista</th>
                    <th>Ação</th>
                </tr>
                </tfoot>
            </table>

1 answer

0

I managed to solve, by way of curiosity I’ll leave to help someone else who goes through the same mistake.

$json_data=array(
"draw"              =>  intval(1),
"recordsTotal"      =>  intval(1),
"recordsFiltered"   =>  intval(1),
"data"              =>  $data

);

As I was mounting, I was passing some values like 1 to mount Json, but the draw is a parameter that informs what is being done... as I was passing fixed, it did not change when doing a search.

the same should be passed this way:

"draw"              =>  $_REQUEST['draw'],

Browser other questions tagged

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