Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

341
Vistas
jQuery Datatable - Error message from ajax call
 "ajax": {
            "url": url,
            "type": "GET",
            "datatype": "json",
            "data": { id: id}
            //,
            //"success": function (data) {
            //    if (!data.data)
            //        ShowErrorMessage(data.message);
            //}
        },
        "columns": [
                { "data": "ID" },
                { "data": "Name" },
                { "data": "Time" }
        ],
        "columnDefs": [{
            "targets": 2,
            "data": "Time",
            "render": function (data) {
                return (ToJavaScriptDate(data));
            }
        }]
    });

Above the sample code, which will populate a datatable using ajax GET call. The issue here is, sometimes I will return a valid error message instead of table data from ajax response.

So when ever there is data in data.data, I need to populate the table and if data is not there, I need to populate the data.message as custom error message. Here both the calls are success calls but whether to display the table content or error message depends.

I tried "success" attribute but I am not sure how to manually bind the columns when there is data in data.data

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The best way would be to use language.emptyTable option.

However since you want to use message from the Ajax response, there is a hacky way to do it.

var table = $('#example').DataTable({
    ajax: {
       url: 'https://api.myjson.com/bins/if7vf',
       dataSrc: function(d){
          if(d.data.length === 0){
             var settings = $('#example').DataTable().settings()[0];
             settings.oLanguage.sEmptyTable = d.message;
          }

          return d.data;
       }
    }
});

See this example for code and demonstration.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda