0

Tenia montado un datatables con este código:

<?php if(count($autores)>0):?>
  <table class="table table-striped- table-hover datatable" id="libros">
    <thead class="thead-light">
        <tr>
            <th>Title</th>
            <th>ISBN</th>
            <th>Level</th>
            <th>Category</th>
            <th>Copies</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($autores as $autor):?>
        <?php if ($autor->fields["hidden"] != "1" && $autor->fields["type"] == "1"): ?>
        <tr>
            <td><a class='kt-link kt-font-bold' href='<?php setURL(); ?>./copies/<?php echo $autor->fields["id"]; ?>'><?php echo $autor->fields["titulo"]; if($autor->fields["subtitulo"] != "") { echo " | " . $autor->fields["subtitulo"] ;}; ?></a></td>
            <td><?php echo $autor->fields["isbn"]; ?></td>
            <td>
                <?php 

                $h = new SQLMan();
                $h->tablename = "nivelvslibro";
                $anyniv= $h->select("many","","libro_id=".$autor->fields["id"]);

                if (count($anyniv)>0) {
                    foreach($nivlib as $nl) {

                        if ($nl->fields["libro_id"] == $autor->fields["id"]) {
                            foreach($nivel as $niv) {
                                if ($niv->fields["id"] == $nl->fields["nivel_id"]) {
                                    echo "<span class='kt-badge kt-badge--success  kt-badge--inline kt-badge--pill'>".$niv->fields["nombre"]."</span> ";
                                    $h = "";
                                } else {

                                }
                            }
                          } else {
                            $h = "";
                          }
                    }; 
                } else {
                    echo "<span class='kt-badge kt-badge--success greysuccess kt-badge--inline kt-badge--pill'>Unlevel</span> ";
                    $h = "";
                }

                ?>
            </td>
            <td>
                <?php 

                $e = new SQLMan();
                $e->tablename = "categoriavslibro";
                $anycat= $e->select("many","","libro_id=".$autor->fields["id"]);

                if (count($anycat)>0) {
                    foreach($catlib as $cl) {

                        if ($cl->fields["libro_id"] == $autor->fields["id"]) {
                            foreach($categoria as $cat) {
                                if ($cat->fields["id"] == $cl->fields["categoria_id"]) {
                                    echo "<span class='kt-badge kt-badge--success  kt-badge--inline kt-badge--pill'>".$cat->fields["nombre"]."</span> ";
                                    $e = "";
                                } else {

                                }
                            }
                          } else {
                            $e = "";
                          }
                    }; 
                } else {
                    echo "<span class='kt-badge kt-badge--success greysuccess kt-badge--inline kt-badge--pill'>Uncategory</span> ";
                    $e = "";
                }

                ?>
            </td>
            <td>
                <?php foreach($ejemplares as $ejem) { 
                    if ($ejem->fields["libro_id"] == $autor->fields["id"] && $ejem->fields["status"] == "0" && $ejem->fields["hidden"] == "0") {
                        $count++;
                    } else {

                    } if ($ejem->fields["libro_id"] == $autor->fields["id"]  && $ejem->fields["hidden"] == "0") {
                        $countb++;
                    } else {

                    } 
                }; 
                if ($count>0) { 
                    $popcount = "kt-badge--success"; 
                } else { 
                    $popcount = "kt-badge--success greysuccessbadge";
                }; 
                echo "<span class='kt-badge ".$popcount." kt-badge--dot kt-badge--xl'></span>&nbsp;&nbsp; "; 
                echo $count . " / " . $countb; 
                $count = "0"; $countb = "0";  ?>
            </td>
            <td>
                <a href="<?php setURL(); ?>./copies/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="View Book">                          
                    <i class="fas fa-eye"></i>                       
                </a>
                <a href="<?php setURL(); ?>./copy-add/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Add Copy">                          
                    <i class="fas fa-plus"></i>                       
                </a>
                <a href="<?php setURL(); ?>./book-edit/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Edit Book">                          
                    <i class="fas fa-edit"></i>                       
                </a>
                <a href="./index.php?action=book-del&id=<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md confirmlink" title="Delete Book">                          
                    <i class="fas fa-trash-alt"></i>                       
                </a>
            </td>
        </tr>
        <?php else: ?>
        <?php endif; ?>
        <?php endforeach; ?>
    </tbody>
  </table>
  <?php else: ?>
  <?php getAlertMsg("1"); ?>
  <?php endif; ?>

introducir la descripción de la imagen aquí

Lo he cambiado a server-side con este código:

<table id="books" class="table table-striped- table-hover ">
  <thead class="thead-light">
      <tr>
          <th>Title</th>
          <th>ISBN</th>
          <th>Subject</th>
          <th>Level</th>
          <th>Category</th>
          <th>Copies</th>
          <th>Actions</th>
      </tr>
  </thead>
</table>

$('#books').DataTable( {
    dom: 'Bfrtlpi',
      buttons: [
            {
                extend:    'copy',
                text:      '<i class="far fa-copy"></i>',
                titleAttr: 'Copy'
            },
            {
                extend:    'excel',
                text:      '<i class="far fa-file-excel"></i>',
                titleAttr: 'Excel'
            },
            {
                extend:    'pdf',
                text:      '<i class="far fa-file-pdf"></i>',
                titleAttr: 'PDF'
            },
            {
                extend:    'print',
                text:      '<i class="fas fa-print"></i>',
                titleAttr: 'Print'
            }
      ],
      pageLength: 25,
      responsive: true,
    "processing": true,
    "serverSide": true,
    "ajax": "./view/libms/modules/library/action/ss.books.php"
} );

<?php

$table = 'libro';

$primaryKey = 'id';

$columns = array(
    array( 'db' => 'titulo', 'dt' => 0 ),
    array( 'db' => 'isbn',  'dt' => 1 ),
    array( 'db' => 'asignatura_id',   'dt' => 2 ),
    array( 'db' => 'autor_id',     'dt' => 3 ),
    array( 'db' => 'editorial_id',     'dt' => 4 ),
    array( 'db' => 'creationdate',     'dt' => 5 ),
    array( 'db' => 'type',     'dt' => 6 )
);

$sql_details = array(
    'user' => 'root',
    'pass' => '',
    'db'   => 'libms',
    'host' => 'localhost'
);

require($_SERVER['DOCUMENT_ROOT']."/libms/view/libms/modules/library/model/ssp.class.php");
 
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

?>

Pero ahora con el server-side, no soy capaz de agregar HTML para enlaces, o los para edit/delete y el resto de HTML que tenia en el otro código. Hay alguna forma de editar la parte de PHP que genera el JSON para añadir el HTML?

1 respuesta 1

0

Hola en la configuración de la DataTable en las columnas usa render

$('#books').DataTable({
    ...
    columns: [
        ...
        {
            data: id,
            class: 'clases_para_el_td',
            render: function(data, type, row){  // con row obtienes la información por fila
                return '<a href="/copies/'+row.id+'" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="View Book"><i class="fas fa-eye"></i></a>';
            }
        }
        ...
    ],
    ...
})
2
  • El data id no me lo reconoce. He probado con el data type que si esta añadido en el json pero tampoco lo reconoce. El json con los datos se ve así: gyazo.com/5404cc129ad6e4f77cb368f20168aa7a
    – Charlie
    el 27 jun. 2020 a las 19:35
  • Podrias intentar agregando los nombres de los campos en cada valor del json. [["text"=>"Why Is It So Loud", "id"=>"123456789",...], ["text"=>"Why Does Thunder Clap?", "id"=>"223456789",...], ...] el 1 jul. 2020 a las 23:49

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.