0

Quiero filtrar los datos que me retorna la petición AJAX para obtener solo los que empiezen con las letras vmi.

$('#datatable').DataTable({
  'dom': 'Bfrtip',
  'buttons': [{
      extend: 'excelHtml5',
      className: 'btn btn-primary',
      exportOptions: {
        columns: ':visible'
      }
    },
    {
      extend: 'pdfHtml5',
      className: 'btn btn-primary',
      exportOptions: {
        columns: ':visible'
      }
    },
    {
      extend: 'colvis',
      className: 'btn btn-primary',
      text: 'Columnas Visibles'
    }
    //'colvis'
  ],
  "columnDefs": [{
    "targets": -1,
    "visible": false
  }],
  "autoWidth": true,
  "ajax": {
    "url": "http://rev.zom.com.mx/extranete/WSs/Servicfios.svc/buscaRefer",

    "type": "POST",
    "contentType": "application/json; charset=utf-8",
    "data": function(d) {
      return data2;
    },
    "dataSrc": function(json) {
      console.log(json.data.length);
      for (var i = 0, ien = json.data.length; i < ien; i++) {



      }
      return json.data;

    }

  },

  "error": function(jqXmlHttpRequest, textStatus, errorThrown) {
    //alert("Datos Incorrectos"); //#coment
  }

});

La estructura del JSON que se retorna es la siguiente:

{
   "data":[
      [
         "-",
         "ALME18-00007",
         "5",
         "16/03/2018",
         "3843",
         "810",
         "8000045",
         "03/01/2018",
         "03/01/2018",
         "04/01/2018"
      ],
      [
         "-",
         "LCE18-00044",
         "6",
         "10/01/2018",
         "3843",
         "510",
         "8000077",
         "20/01/2018",
         "20/01/2018",
         "18/01/2018"
      ],
      [
         "-",
         "MAN18-00002",
         "4",
         "15/03/2018",
         "3235",
         "160",
         "8000040",
         "07/01/2018",
         "12/01/2018",
         "18/01/2018"
      ],
      [
         "-",
         "VMI18-00001",
         "1",
         "15/03/2018",
         "3843",
         "430",
         "8000068",
         "15/01/2018",
         "16/01/2018",
         "18/01/2018"
      ],
      [
         "-",
         "VMI18-00002",
         "3",
         "16/03/2018",
         "3843",
         "430",
         "8000070",
         "15/01/2018",
         "16/01/2018",
         "18/01/2018"
      ]
   ]
}
3
  • Cual es la estructura de tu json retornado?
    – alanfcm
    el 7 may. 2018 a las 17:19
  • ASi esta {"data":[["-","ALME18-00007","5","16/03/2018","3843","810","8000045","03/01/2018","03/01/2018","04/01/2018"],["-","LCE18-00044","6","10/01/2018","3843","510","8000077","20/01/2018","20/01/2018","18/01/2018"],["-","MAN18-00002","4","15/03/2018","3235","160","8000040","07/01/2018","12/01/2018","18/01/2018"],["-","VMI18-00001","1","15/03/2018","3843","430","8000068","15/01/2018","16/01/2018","18/01/2018"],["-","VMI18-00002","3","16/03/2018","3843","430","8000070","15/01/2018","16/01/2018","18/01/2018"]]}
    – emanuelle
    el 7 may. 2018 a las 17:23
  • Por favor, en tus próximas preguntas sé un poco más claro y explícito :)
    – gugadev
    el 7 may. 2018 a las 17:31

1 respuesta 1

2

Usa el método Array#filter para filtrar por el predicado deseado:

...
"dataSrc": json => json.data.filter(arr => arr[1].startsWith('VMI')),
...
4
  • funciona pero no hace el filtro y la salida sigue pasando todos los datos ¿como guardo el resultado del filtro en json.data?
    – emanuelle
    el 7 may. 2018 a las 17:34
  • @emanuelle He actualizado la respuesta.
    – gugadev
    el 7 may. 2018 a las 17:43
  • Eres un mastrazo !! te agradesco mucho brother
    – emanuelle
    el 7 may. 2018 a las 17:47
  • De nada. Si te sirvió la respuesta puedes indicarlo marcándola como correcta (check debajo de la votación).
    – gugadev
    el 7 may. 2018 a las 17:52

Tu Respuesta

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

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