My datatable does not show filtering, pagination and search | Please help me!

My datatable does not show filtering, pagination and search | Please help me!

FacundoFFacundoF Posts: 2Questions: 0Answers: 0
edited January 21 in Free community support

I have this part of code where I show data in the table:

<?php 
    foreach ($pedidos as $dato) {
        $codigoPedido = $dato['Codigo_pedido'];
        $razonSocialCliente = $dato['Razon_social'];
        $tipoPedido = $dato['Tipo_pedido'];
        $nombreEmpleado = $dato['NombreEmpleado'];
        $estadoPedido = $dato['Estado_pedido'];
        $fechaPedido = $dato['Fecha'];
        $descripcionProductos = explode('<br>', $dato['DescripcionProductos']);
        $cantidadProductos = explode('<br>', $dato['CantidadProductos']);
        $precioTotal = $dato['PrecioTotal'];
        $numProductos = count($descripcionProductos);
  
        if (count($cantidadProductos) !== $numProductos) {
            continue;
        }
    ?>
    <tr>
        <td rowspan="<?php echo $numProductos ?>"><?php echo $codigoPedido ?></td>
        <td rowspan="<?php echo $numProductos ?>"><?php echo $razonSocialCliente ?></td>
        <td rowspan="<?php echo $numProductos ?>"><?php echo $tipoPedido ?></td>
        <td rowspan="<?php echo $numProductos ?>"><?php echo $nombreEmpleado ?></td>
        <td rowspan="<?php echo $numProductos ?>"><?php echo $estadoPedido ?></td>
        <td rowspan="<?php echo $numProductos ?>"><?php echo $fechaPedido ?></td>
        <td><?php echo $descripcionProductos[0] ?></td>
        <td><?php echo $cantidadProductos[0] ?></td>
        <?php
        $primerProductoPrecio = obtenerPrecioProductoPedido($codigoPedido, $descripcionProductos[0]);
        ?>
        <td><?php echo $primerProductoPrecio ?></td>
        <td rowspan="<?php echo $numProductos ?>">
        <a href="borrarPedido.php?id=<?php echo $codigoPedido ?>" onclick='return test_click();' class="btn btn-danger"><i class="fas fa-user-minus"></i></a>
            
            <a href="actualizarPedido.php?id=<?php echo $codigoPedido ?>" class="btn btn-info"><i class="fas fa-pen"></i></a>
            <a href="agregarremito.php?id=<?php echo $codigoPedido ?>" class="btn btn-info"><i class="fas fa-bars"></i></a>
        </td>
        </tr>
    <?php for ($i = 1; $i < $numProductos; $i++) { ?>
    <tr>
        <td><?php echo $descripcionProductos[$i] ?></td>
        <td><?php echo $cantidadProductos[$i] ?></td>
        <?php
        $productoPrecio = obtenerPrecioProductoPedido($codigoPedido, $descripcionProductos[$i]);
        ?>
        <td><?php echo $productoPrecio ?></td>
    </tr>
    <?php } ?>
    <?php } ?>
</tbody>

I need help because in the part where I add more than one data in a row, it makes the filter, pagination and search disappear

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

Replies

  • kthorngrenkthorngren Posts: 21,082Questions: 26Answers: 4,908

    Its unclear to me what the resulting HTML table is just by looking at your PHP code. For example I see a closing body in the last line but don't see the opening <tbody>. It looks like you might be building a table with varying numbers of td in each row which is not supported. See the HTMLL docs for what Datatables supports.

    it makes the filter, pagination and search disappear

    Typically this means there is a Javascript error stopping the script. See if there are errors in the browser's console.

    If you still need help then please post a link to your page or a test case replicating the issue so we can help debug. Maybe open the Page Source to see the HTML built by your PHP code. Copy it, maybe cut some rows if there are many, into a test case. You can update teh HTML tab of this link:
    https://live.datatables.net/

    Post the updated URL once you have completed the test case.

    Kevin

  • FacundoFFacundoF Posts: 2Questions: 0Answers: 0

    I'm going to check what you told me, but just in case I'll leave the link

    https://live.datatables.net/hedazavu/1/edit

  • kthorngrenkthorngren Posts: 21,082Questions: 26Answers: 4,908

    Your test case doesn't run and has many errors. Plus there is a lot of extra code we don't need to see to help. I also don't see any code where Datatables is initialized.

            <td rowspan="<?php echo $numProductos ?>"><?php echo $codigoPedido ?></td>
    

    Looks like you might be adding rowspan to the table cells. This is not supported. Datatables supports only one cell per column. See the HTML docs.

    You have 10 columns in the thead but it looks like you are adding some rows to the tbody that have only 3 columns. This is not supported by Datatables:

        <tr>
            <td><?php echo $descripcionProductos[$i] ?></td>
            <td><?php echo $cantidadProductos[$i] ?></td>
            <?php
            $productoPrecio = obtenerPrecioProductoPedido($codigoPedido, $descripcionProductos[$i]);
            ?>
            <td><?php echo $productoPrecio ?></td>
        </tr>
    

    If you need help debugging then please use the browser's developer tools to view the source generated. Copy just the HTML table and your Datatables initialization code into a test case that executes and shows the issue.

    Kevin

Sign In or Register to comment.