fill a datatable with ajax and php mvc

0

Hi, I'm trying to fill a datatable in php but I can not do it, this is what I have:

driver

public function VentasXvendedor(){

        $IdVendedor= strtoupper(utf8_encode($_REQUEST['IdVendedor']));
         $arrayCli=array();
        $data=$this->model->BuscarVentasXvendedor($IdVendedor);
        for ($i=0; $i < count($data); $i++) {               
            $resultadodetallado_json = array(               
                'c_numped'      => utf8_encode($data[$i]->c_numped),//
                'c_nomcli'      => utf8_encode($data[$i]->c_nomcli),//
                'c_asunto'      => utf8_encode($data[$i]->c_asunto),
                'c_login'       => utf8_encode($data[$i]->c_login),
                'estado'        => utf8_encode($data[$i]->estado),
                'moneda'        => utf8_encode($data[$i]->moneda),
                'd_fecped'      => utf8_encode($data[$i]->d_fecped),
                'n_tcamb'       => utf8_encode($data[$i]->n_tcamb),
                'total'         => utf8_encode($data[$i]->total)
             );     

         array_push($arrayCli, $resultadodetallado_json);        
        } 
        echo(json_encode($arrayCli));  

    }

Model

public function BuscarVentasXvendedor($ivendedor)
    {
        try
        {
            $variable=trim($ivendedor);
            $stm = $this->pdo->prepare("SELECT sum_detalle.c_numped, sum_detalle.c_tipped, sum_detalle.d_fecped, sum_detalle.c_codcli, sum_detalle.c_nomcli, sum_detalle.c_asunto, Sum(sum_detalle.n_preprd) AS total, sum_detalle.n_tcamb, sum_detalle.moneda, sum_detalle.c_estado, sum_detalle.n_swtapr, sum_detalle.n_preapb, sum_detalle.b_IncIgv, sum_detalle.n_apbpre, sum_detalle.c_login, IIf(pedicab.c_estado='0' And pedicab.n_swtapr=1,'Aprobado',IIf(pedicab.c_estado='0' And pedicab.n_swtapr=0 ,'Generado',IIf((pedicab.c_estado='2' or pedicab.c_estado='1' ) And pedicab.n_swtapr=1,'Facturado',IIf(pedicab.c_estado='4' And pedicab.n_swtapr=0,'Eliminado',IIf(pedicab.c_estado='5' And pedicab.n_swtapr=1,'Fusionado','nada'))))) AS estado
                FROM sum_detalle
                where sum_detalle.c_login='$variable'
                GROUP BY sum_detalle.c_numped, sum_detalle.c_tipped, sum_detalle.d_fecped, sum_detalle.c_codcli, sum_detalle.c_nomcli, sum_detalle.c_asunto, sum_detalle.n_tcamb, sum_detalle.moneda, sum_detalle.c_estado, sum_detalle.n_swtapr, sum_detalle.n_preapb, sum_detalle.b_IncIgv, sum_detalle.n_apbpre, sum_detalle.c_login;
                ");
            $stm->execute();
            return $stm->fetchAll(PDO::FETCH_OBJ);
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }

Vista

<div class="form-group">
                <label>Vendedor</label>
                <select id="vendedores" name="vendedores" class="form-control select2">
                  <option selected="selected">Seleccione</option>
                    <?php foreach($this->model->ListarVendedores() as $vendedores):
                    ?>
                        <option value="<?php echo $vendedores->c_login; ?>"
                        > 
                        <?php echo $vendedores->c_nombre; ?> </option>
                    <?php endforeach; ?>
                    <option selected="TODOS">TODOS</option>
                </select>
              </div>
<table id="example" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Cotizacion</th>
            <th>Cliente</th>
            <th>Asunto</th>
            <th>Vendedor</th>
            <th>Estado</th>
            <th>Moneda</th>
            <th>Fecha</th>
            <th>T. Cambio</th>
            <th>Total</th>
        </tr>
    </thead>

    <tbody>

    </tbody>
</table> 

jquery (is in sight) This is where I need to know how to call the function to fill my datatable

$(document).ready(function(){  
    $('#vendedores').on('change', function() {  

$('#example').dataTable( {
  "ajax": {
    "url": "?c=dashboard&a=VentasXvendedor",
    "data": function ( d ) {
        d.IdVendedor = $('#vendedores').val();
    }
  }
} );

 })
}) 

this error marks me:

  

TypeError: f is undefined [Learn more]

    
asked by Vanesa Claros 30.05.2018 в 00:06
source

1 answer

1

Good morning I hope this has been your help ....

Driver

public  function VentasXvendedor() {
$data = array();
$IdVendedor = strtoupper(utf8_encode($_REQUEST['IdVendedor']));
$arrayCli = array();
$data = $this->model->BuscarVentasXvendedor($IdVendedor);
for ($i = 0; $i < count($data); $i++) {
    $resultadodetallado = array();
    array_push($resultadodetallado, utf8_encode($data[$i]->c_numped));
    array_push($resultadodetallado, utf8_encode($data[$i]->c_nomcli));
    array_push($resultadodetallado, utf8_encode($data[$i]->c_asunto));
    array_push($resultadodetallado, utf8_encode($data[$i]->c_login));
    array_push($resultadodetallado, utf8_encode($data[$i]->estado));
    array_push($resultadodetallado, utf8_encode($data[$i]->moneda));
    array_push($resultadodetallado, utf8_encode($data[$i]->d_fecped));
    array_push($resultadodetallado, utf8_encode($data[$i]->n_tcamb));
    array_push($resultadodetallado, utf8_encode($data[$i]->total));
    $arrayCli['data'][] = $resultadodetallado;
}
echo(json_encode($arrayCli));

}

Jquery view

    var url="";
    $('#example').dataTable({
    ajax:url,
    buttons: [
        'copy', 'csv', 'excel', 'pdf', 'print'
    ],
    "bDestroy": true,
    "autoWidth": false,
    dom: "Bfrtip",
    destroy: true
});
    
answered by 30.05.2018 / 00:50
source