sort date fields, using moment?
sort date fields, using moment?
I am using server side to fetch my data.
I'm rendering my date data to show it in the desired am / pm format.
but when sorted by this field it doesn't sort as it should, sort out of order.
add this line of code in this format which is the same as I am rendering, but my ordering still does not work correctly
$.fn.dataTable.moment('YYYY-MM-DD hh:mm A', 'es');
this is an example of that field and my data table setup, using server side. I want to sort by data 10
let tablep1=$('#piku-produccion').DataTable({
dom: 'Blf<"#searchCodPpro">rtip', //<"#SearchProduccion">
"language": {
"sProcessing": "<img src='vistas/img/plantilla/200.gif' />",
"sLengthMenu": "MOSTRAR _MENU_ REGISTROS",
"sZeroRecords": "NO SE ENCONTRARON RESULTADOS",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "MOSTRANDO REGISTROS DEL _START_ AL _END_ <br> DE UN TOTAL DE _TOTAL_",
"sInfoEmpty": "MOSTRANDO REGISTROS DEL 0 AL 0 DE UN TOTAL DE 0 <br>",
"sInfoFiltered": "(FILTRADO DE UN TOTAL DE _MAX_ REGISTROS)",
"sInfoPostFix": "",
"sSearch": "<i class='fa fa-search text-info'></i>",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "CARGANDO...",
"oPaginate": {
"sFirst": "PRIMERO",
"sLast": "ULTIMO",
"sNext": "SIGUIENTE",
"sPrevious": "ANTERIOR"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
},
"buttons": {
"copyTitle": 'agregado al porta papeles',
"copyKeys": 'Seguir adelante <i>ctrl</i> o <i>\u2318</i> + <i>C</i> para copiar los datos de la tabla a su portapapeles. <br><br>Para cancelar, haga clic en este mensaje o presione Esc.',
"copySuccess": {
_: '%d líneas copiadas',
1: '1 línea copiada copiada'
}
}
},
"processing": true,
"serverSide": true,
"searchDelay":2000,
"sAjaxSource":'serverside/show.tabla.php',
search: {search: respuesta["lote_piku"]},
"ordering": true,
"order": [[10, "desc"]],
"columns": [
{ data: 1 },
{ data: 2 },
{ data: null,
"render": function ( data, type, row ) {
//let dt1=row[3];
let dt1 =`
<span class="badge bg-yellow pull-right" data-toggle="tooltip" data-placement="right" data-html="true" title="Esta campo <br> no tiene lote reproceso">
LOTE RE-PROCESO ?
</span>
`
;
//console.log(row[3]);
if (row[3]!==null) {
dt1=row[3];
}else{
}
return dt1;
}
},
{ data: 4 },
{ data: 5 },
{ data: 6 },
{ data: 7 },
{ data: 8 },
{ data: 9 },
{//this is the field that I am formatting with moment and the field that I want to sort
"render": function ( data, type, row ) {
//2021-08-09 11:27 AM or 2021-08-09 02:27 PM
let dt1 = moment(row[10], ["YYYY-MM-DD HH:mm:ss"]).format("YYYY-MM-DD hh:mm A");
return dt1;
}
}
],
"lengthMenu": [[500, 250, -1], [500, 250, "TODO"]],
/*activo la el scroll dentro de la tabla para poder depararme y obtener vista de todas las columnas-filas */
"scrollY": 300,
"drawCallback": function(row, data, index) {
$('[data-toggle="tooltip"]').tooltip();
},
"initComplete": function(){
//para evitar que el servidor y el renderizado colapse, haciendo llamadas ajax
$("#piku-produccion_filter input").unbind().bind("input", function(e) { // Bind our desired behavior
// If the length is 6 or more characters,search
if(this.value.length == 6) {
// Call the API search function
tablep1.search(this.value).draw();
}
});
}
});
note: for some unknown reason it correctly sorts the first data load in the table, but when I apply the order by clicking the field, everything gets messed up.
Replies
When using server side processing the sorting, searching and paging is controlled by the server script. Using
$.fn.dataTable.moment('YYYY-MM-DD hh:mm A', 'es');
will only work with client side processing. Are you using a Datatables supplied server side processing script? Do you need server side processing?Kevin
if i am using server side processing.
file. serverside/show.tabla.php
file serverside.php