-2

how i can read specific data from JSON file without element name, i working with datatables, the follow is de code, for example, i need read in javascript file only row value '2021-06-21' and '$8.000', how would can to get these value.

I working with JSON file generated for PHP and datatables

This is my JSON file format example:

{
        "draw": 0,
        "recordsTotal": null,
        "recordsFiltered": null,
        "data": [
            [
                "hola",
                "1",
                "<a href=\"muestra.php?id=45\">Product</a>",
                "2021-06-21",
                "$ 1.091.000\n",
                "$ 8.000\n",
                "$ 16.000\n",
                "$ 1.115.000\n",
                "$ 1.949.000\n",
                "$ 834.000\n",
                "76%",
                "988980003697VO"
            ],

My JS code from datatable

    function format ( data ) {
            
            
            return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
                '<tr>'+
                    '<td>Full name:</td>'+
                    '<td>'+data.[6]+'</td>'+
                '</tr>'+
                '<tr>'+
                    '<td>Extension number:</td>'+
                    '<td>'+data.[11]+'</td>'+
                '</tr>'+
                '<tr>'+
                    '<td>Extra info:</td>'+
                    '<td>And any further details here (images etc)...</td>'+
                '</tr>'+
            '</table>';
        }
        $(document).ready(function(){   
                var employeeData = $('#escanmotorList').DataTable({
                    "lengthChange": true,
                    "processing":true,
                    "serverSide":true,
                    "autoWidth": false,
                    "order":[],
                    "ajax":{
                        url:"action.php",
                        type:"POST",
                        data:{action:'listEscanmotor'},
                        dataType:"json"
                    },
                    "language": {
                        "lengthMenu": "Mostrando _MENU_ productos por página",
                        "zeroRecords": "Nothing found - sorry",
                        "info": "Página _PAGE_ de _PAGES_",
                        "loadingRecords": "Cargando...",
                        "infoEmpty": "No records available",
                        "search": "Buscar",
                        "processing":     "Procesando...",
                        "paginate": {
                            "first":      "Primero",
                            "last":       "Último",
                            "next":       "Siguiente",
                            "previous":   "Anterior"
                        },
                        "infoFiltered": "(filtered from _MAX_ total records)"
                    },
                    "columns": [
                { "width": "15px", "targets": 1, "className":      'details-control' },     
                { "width": "15px", "targets": 1 },
                null,
                { "width": "55px", "targets": 1 },
                { "width": "90px", "targets": 1 },
                { "width": "40px", "targets": 1 },
                { "width": "40px", "targets": 1 },
                { "width": "90px", "targets": 1 },
                { "width": "90px", "targets": 1, "orderable": false },
                { "width": "70px", "targets": 1 },
                { "width": "30px", "targets": 1, "orderable": false },
                { "width": "120px", "targets": 1, "orderable": false },
                { "width": "70px", "targets": 1 },  
                { "width": "60px", "targets": 1, "orderable": false },
                { "width": "60px", "orderable": false },
                { "width": "60px", "orderable": false }
              ],
                    "pageLength": 10
                }); 

$('#escanmotorList tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = employeeData.row( tr );
 
        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.find('svg').attr('data-icon', 'plus-circle');    // FontAwesome 5
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
          tr.find('svg').attr('data-icon', 'minus-circle'); // FontAwesome 5
        }
    } );

1 Answers1

1

You access it not via "name", but via "key". And non-associative arrays has default keys from 0 to "length-1". In your case myJson.data[0][6]

var myJson = {
  "draw": 0,
  "recordsTotal": null,
  "recordsFiltered": null,
  "data": [
    [
      "hola",
      "1",
      "<a href=\"muestra.php?id=45\">Product</a>",
      "2021-06-21",
      "$ 1.091.000\n",
      "$ 8.000\n",
      "$ 16.000\n",
      "$ 1.115.000\n",
      "$ 1.949.000\n",
      "$ 834.000\n",
      "76%",
      "988980003697VO"
    ],
  ]
};

console.log(
  myJson.data[0][6],
  myJson.data[0][11],
  myJson.data[0][myJson.data[0].length - 1]
);
Justinas
  • 41,402
  • 5
  • 66
  • 96