Change table layout, Datatable initialization

Change table layout, Datatable initialization

cris19ncris19n Posts: 55Questions: 18Answers: 0

I have my table in a modal, this is the configuration before starting Datatables:

<div class="modal fade" id="modal-dts-viaje" >
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title ">REGISTRADOR DATOS DEL VIAJE</h4>
                <div class="bg-danger col-4 h3">Fecha Proceso: <span id="span_dts_viaje_fe_pro" class="h5"></span></div>
                <div class="bg-danger col-4 h3">Viaje: <span id="span_dts_viaje_vj" class="h5"></span></div>

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>

            <div class="modal-body">
                <div class="card">
                    <div class="card-header">
                        <form class="form registrarDtsViaje" role="form" id="form-dts-viaje" enctype="multipart/form-data" autocomplete="off">
                             <!-- code form -->
                    <div class="card-body">

                    <table id="tb-dts-viaje-modal" class="table table-striped table-bordered table-hover">
                                <th>#</th><!-- hidden -->
                                <th>FECHA DE REGISTRO</th>
                                <th>PERSONA RESPONSABE</th>
                                <th>FK. VIAJE</th><!-- hidden -->
                                <th>No HUACAL</th>
                                <th>AVES X HUACAL</th>
                                <th>TOTAL AVES</th>
                                <th>PESO BRUTO</th>

                                <th colspan="4"></th>
                                <th id="tb-dts-vj-mdl-ft-tlAvhc">TL Huacal:</th>
                                <th id="tb-dts-vj-mdl-ft-tlhc">TL Aves Huacal:</th>
                                <th id="tb-dts-vj-mdl-ft-tlav">TL Aves: </th>
                                <th id="tb-dts-vj-mdl-fttlpb-">Peso Bruto:</th>
                                <th id="tb-dts-vj-mdl-ft-tld">Destare:</th>
                                <th id="tb-dts-vj-mdl-ft-pn"></th>
                                <th id="tb-dts-vj-mdl-ft-pr"></th>
            <div class="modal-footer justify-content-between">
                <button type="button" class="btn btn-default" data-dismiss="modal">CERRAR</button>
        <!-- /.modal-content -->
    <!-- /.modal-dialog -->

This is my Datatable js code:

 function cargarDataTable(btn) {
    let idviaje =Number($(btn).attr("TBidviaje"));
    /* let datosViajeTable =  */$('#tb-dts-viaje-modal').DataTable({
      destroy: true,
      processing: true,
      serverSide: true,
      ordering: true,
      'ajax' : {
        'url' : 'serverside/tb_dts.viaje.php',
        'data' : { 'id_viaje' : idviaje }
      order: [[0, "desc"]],
      columns: [
        { data: 0, visible: false },
        { data: 1 },
        { data: 2 },
        { data: 3, visible: false },
        { data: 4 },
        { data: 5 },
        { data: 6 },
        { data: 7 },
        { data: 8 },
        { data: 9 },
          data: null,
          orderable: false,
          /* visible:
              ? true
              : false, */
          render: function (data, type, row) {
            /*  return `<div class="btn-group" role="group" aria-label="Third group">
                      <button class="btn btn-warning btnEditarDesc manito-clic" editDS='${row["0"]}' loteP='${row["3"]}' idUsuario='${ID_USER}' User='${USUARIO_LOGUEO}' data-toggle="modal" data-target="#modalEditarDescartes">
                          <i class="fa fa-pencil"></i>
                  <div class="btn-group" role="group" aria-label="Third group">
                      <button class="btn btn-danger btnEliminarDesc manito-clic" deleteloteDS='${row["3"]}' deleteDS='${row["0"]}'>
                          <i class="fa fa-times"></i>
                  </div>`; */


                  return `
                    <div class="btn-group">
                      <button type="button" class="btn btn-warning sweeEditDtsVj" title="Editar Viaje" idDtsVj="${row["0"]}" data-toggle="modal" data-target="#modal-edit-dts-viaje" ><i class="fas fa-edit"></i></button>
                      <button type="button" class="btn btn-danger sweeDeleteDtsVj" title="Eliminar Viaje" idDtsVj="${row["0"]}"><i class="fas fa-trash-alt"></i></button>
                      <!-- <button type="button" class="btn btn-info">Bottom</button> -->
      scrollY: 250,
      //scrollX: true,
      footerCallback: function (row, data, start, end, display) {
        let api = this.api();

        //obtener datos desde el servidor
        let json = api.ajax.json();

        //TOAL HUACAL
        $("#tb-dts-vj-mdl-ft-tlAvhc").attr("title", "TL HUACAL");
        $("#tb-dts-vj-mdl-ft-tlhc").attr("title", "TL AVES X HUACAL");
        //TOTAL AVES
        $("#tb-dts-vj-mdl-ft-tlav").attr("title", "TL AVES");
        $("#tb-dts-vj-mdl-fttlpb-").attr("title", "TL PESO BRUTO");
        $("#tb-dts-vj-mdl-ft-tld").attr("title", "TL DESTARE");
        //PESO NETO
        $("#tb-dts-vj-mdl-ft-pn").attr("title", "Peso Neto");
        //PESO PRO
        $("#tb-dts-vj-mdl-ft-pr").attr("title", "Promedio");



this is causing the footer columns to somehow lose their colspan setting.

when you start Datatable.

when a header is clicked, the footer aligns correctly.

I am loading Datatable in modal, these data are different according to the id_viajeparameter that I sent, each time the click is detected on the button that activates the modal, main table.

Is there any way to initialize Datatable only once, without Data, and change the parameters every time the modal is activated, that is, I am using the destroy:true option, without it I cause the error Datatable cannot be initialized.

It is worth mentioning that I only need to update the data, according to the value of the id_viajeparameter.

This question has an accepted answers - jump to answer


Sign In or Register to comment.