How to put an id to a tr from array?

1

How can I put the ids that come from the database to the tr from the controller since it is printed from there?

//CONTROLADOR 

public function ajax_list()
    {
        $list = $this->Prorrateo_model->get_datatables();
        $data = array();
        $no   = $_POST['start'];
        foreach ($list as $traba) {

            $originalDate1 = $traba->fechaInTraba;
            $newDate1      = date("d-m-Y", strtotime($originalDate1));
            $originalDate2 = $traba->fechaCese;
            $newDate2      = date("d-m-Y", strtotime($originalDate2));

            $no++;
            $row   = array();
            $row[] = $no;
            $row[] = $traba->empleado;
            $row[] = $traba->dniTraba;
            $row[] = $traba->nCargo;
            $row[] = $traba->nArea;
            $row[] = $traba->emailTraba;
            $row[] = $traba->celTraba;
            $row[] = $traba->direcTraba;
            $row[] = $newDate1;
            $row[] = $newDate2;
            $row[] = $traba->nContrata;
            $row[] = $traba->nSispension;
            $row[] = $traba->horaIng;
            $row[] = $traba->horaSali;
            $row[] = $traba->JorLaboral;
            $row[] = $traba->remuneracion;
            $row[] = $traba->numlabor;
            $row[] = $traba->asigfamilia;
            $row[] = $traba->thextra;
            $row[] = $traba->boniordi;
            $row[] = $traba->tconcepremu;
            $row[] = $traba->remuvacapro;
            $row[] = $traba->gratiprorra;
            $row[] = $traba->subsidios;
            $row[] = $traba->conditraba;
            $row[] = $traba->vales;
            $row[] = $traba->movilidad;
            $row[] = $traba->comisiones;
            $row[] = $traba->boniextra;
            $row[] = $traba->utilidades;
            $row[] = $traba->remutotal;
            $row[] = $traba->renta5tac;
            $row[] = $traba->pensionesp;
            $row[] = $traba->renta4tac;
            $row[] = $traba->rejudicial;
            $row[] = $traba->tardanza;
            $row[] = $traba->otros;
            $row[] = $traba->totaldsctos;
            $row[] = $traba->essalud;
            $row[] = $traba->tnetopagar;
            $row[] = $traba->ctsprorrateo;
            $row[] = $traba->smeprivado;
            $row[] = $traba->taporteEmp;
            $row[] = $traba->despagarterceros;
            $row[] = $traba->tgremuneracion;
            $row[] = $traba->adremuneracion;
            $row[] = $traba->observaciones;

            $row[]  = '<center><button  type="button" class="btn btn-xs bg-teal waves-effect editar" href="javascript:void(0)" data=' . $traba->idProrrateo . ' onclick="edit_person(' . "'" . $traba->idProrrateo . "'" . ')"><i class="fa fa-pencil"></i>&nbsp;Editar</button>   <button  type="button" class="btn btn-xs bg-red waves-effect" href="javascript:void(0)" onclick="delete_person(' . "'" . $traba->idProrrateo . "'" . ')"><i class="fa fa-trash"></i>&nbsp;Eliminar</button></center>';
            $data[] = $row;
        }

        $output = array(
            "draw"            => $_POST['draw'],
            "recordsTotal"    => $this->Prorrateo_model->count_all(),
            "recordsFiltered" => $this->Prorrateo_model->count_filtered(),
            "data"            => $data,
        );
        //output to json format
        echo json_encode($output);
    }

//FUNCION PARA LISTAR 
$(document).ready(function() {
  table = $('#table').DataTable({
   "scrollX": true,
   "scrollY":        "245px",
   "lengthMenu": [[5, 15, 25, 50,100, -1], [5, 15, 25, 50,100, "Todo"]],
   "scrollCollapse": true,
        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "order": [], //Initial no order.
        "language": {
          "lengthMenu": "Mostrar _MENU_ registros por pagina",
          "zeroRecords": "No se encontraron resultados en su busqueda",
          "searchPlaceholder": "Buscar registros",
          "info": "Mostrando  de _START_ al _END_  de  _TOTAL_ registros",
          "infoEmpty": "No existen registros",
          "infoFiltered": "",
          "search": "Buscar:",
          "processing": "Procesando...:",
          "paginate": {
            "first": "Primero",
            "last": "Último",
            "next": "Siguiente",
            "previous": "Anterior"
          },
        },
        // Load data for the table's content from an Ajax source
        "ajax": {
          "url": "<?php echo base_url(); ?>prorrateo/ajax_list",
          "type": "POST"
        },
        //Set column definition initialisation properties.
        "columnDefs": [
        {
            "targets": [ -1 ], //last column
            "orderable": false, //set not orderable
          },
          ],


        });

});
<!--VISTA DE LA TABLA -->


<div class="body">
  <table id="table" class="table table-striped table-bordered table-condensed table-hover table-responsive" cellspacing="0" style="min-width: 100%; max-width: 100%; width: 1000px;">
    <thead>
      <tr>
        <th style="width: 10px;">ITEM</th>
        <th style="width: 250px;">EMPLEADO</th>
        <th style="width: 100px;">DNI</th>
        <th style="width: 200px;">CARGO</th>
        <th style="width: 200px;">ÁREA</th>
        <th style="width: 300px;">CORREO</th>
        <th style="width: 150px;">CELULAR</th>
        <th style="width: 300px;">DIRECCIÓN</th>
        <th style="width: 150px;">FECHA INGRESO</th>
        <th style="width: 150px;">FECHA CESE</th>
        <th style="width: 200px;">TIPO CONTRATACION</th>
        <th style="width: 150px;">SISTEMA PENSION</th>
        <th style="width: 150px;">HORA INGRESO</th>
        <th style="width: 150px;">HORA SALIDA</th>
        <th style="width: 150px;">JORNADA LABORAL</th>
        <th style="width: 300px;">REMUNERACION BASICA</th>
        <th style="width: 300px;"># DE DIAS LABORADOS</th>
        <th style="width: 300px;">ASIGNACION FAMILIAR</th>
        <th style="width: 200px;">COMISIONES</th>
        <th style="width: 300px;">TOTAL HORAS EXTRAS</th>
        <th style="width: 300px;">BONIFICACION ORDINARIA</th>
        <th style="width: 300px;">TOTAL CONCEPTOS REMUNERATIVOS</th>
        <th style="width: 300px;">REMUNERACION VACACIONAL PRORRATEADA</th>
        <th style="width: 300px;">GRATIFICACIONES PRORRATEADAS</th>
        <th style="width: 150px;">SUBSIDIOS</th>
        <th style="width: 300px;">CONDICION TRABAJO</th>
        <th style="width: 100px;">VALES</th>
        <th style="width: 150px;">MOVILIDAD</th>
        <th style="width: 250px;">BONIFICAION EXTRAORDINARIA</th>
        <th style="width: 300px;">UTILIDADES</th>
        <th style="width: 250px;">REMUNERACION TOTAL</th>
        <th style="width: 300px;">RENTA DE 5TA. CATEGORIA</th>
        <th style="width: 300px;">PRENSIONES PREVISIONALES</th>
        <th style="width: 300px;">RENTA DE 4TA. CATEGORIA</th>
        <th style="width: 250px;">RETENCION JUDICIAL</th>
        <th style="width: 150px;">TARDANZAS</th>
        <th style="width: 150px;">OTROS</th>
        <th style="width: 300px;">ADELANTOS DE REMUNERACION</th>
        <th style="width: 300px;">TOTAL DSCTOS.</th>
        <th style="width: 300px;">TOTAL NETO A PAGAR</th>
        <th style="width: 150px;">ESSALUD</th>
        <th style="width: 300px;">CTS PRORRATEADOS</th>
        <th style="width: 300px;">SEGURO MEDICO PRIVADO</th>
        <th style="width: 300px;">TOTAL APORTES EMPLEADOR</th>
        <th style="width: 400px;">TOTAL DESCUENTOS REALIZADOS A PAGAR A TERCEROS</th>
        <th style="width: 300px;">TOTAL GASTO REMUNERACION</th>
        <th style="width: 300px;">OBSERVACIONES</th>
        <th style="width:200px; " align="center">OPCION</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
</div>
    
asked by Luis 29.05.2018 в 17:13
source

1 answer

0

DataTable has an automatic way to add the id attribute to the tr that are generated with the data that feeds the table, all you have to do is send the property DT_RowId within each element that Shape the object, leaving said object as follows:

{
  "draw": 1,
  "recordsTotal": 1,
  "recordsFiltered": 1,
  "data": [{
    "DT_RowId": "1",
    "item_1": "Valor 1",
    "item_2": "Valor 2"
  }, {
    "DT_RowId": "2",
    "item_1": "Valor 1.1",
    "item_2": "Valor 2.1"
  }]
}

Although this is the default property, it can also be changed by some special specific property, as follows:

{
  "draw": 1,
  "recordsTotal": 1,
  "recordsFiltered": 1,
  "data": [{
    "id": "1",
    "item_1": "Valor 1",
    "item_2": "Valor 2"
  }, {
    "id": "2",
    "item_1": "Valor 1.1",
    "item_2": "Valor 2.1"
  }]
}

$('#myTable').DataTable({
    "ajax": '/api/prueba',
    "rowId": 'id'
});

The property rowId also receives as a value the index of the value to be used in case the data is not a array of objects (which in fact is how you have it):

{
  "draw": 1,
  "recordsTotal": 1,
  "recordsFiltered": 1,
  "data": [[
    "1",
    "Valor 1",
    "Valor 2"
  ], [
    "2",
    "Valor 1.1",
    "Valor 2.1"
  ]]
}

$('#myTable').DataTable({
    "ajax": '/api/prueba',
    "rowId": '0',
    "columnDefs": [{
        "targets": [0], //Ocultamos la primera columna, donde se muestra el id
        "visible": false
    }],
});

There are two important aspects to take into account when generating the id :

  

The value must be unique among all the IDs in the home subtree of the item and must contain at least one character.

     

The value must not contain any character space.

For more information you can consult DataTables - rowId

    
answered by 29.05.2018 в 18:30