Pass additional meta data to javascript array

Pass additional meta data to javascript array

alzamboalzambo Posts: 38Questions: 17Answers: 1
edited November 2016 in DataTables 1.10

Hi,
I need to pass some additional data to datatable through a javascript array.
This would be my data array to be passed to dt:

datiTabella = array(
    'dati' => array(
        0 => array(
            'id' => '1'
        ),
        1 => array(
            'id' => '2'
        )
    ),
    'schema' => array(
        0 => array(
            'nomeCampo' => 'id',
            'relazione' => 'incarico.idAssicurato'
        )
    )
);

'dati' contains database data
'schema' contains database fields metadata, to be used in a function

In every column i need to access both 'dati' (to be displayed) and 'schema' to be used in a function, but how to access that keys?

"data": datiTabella,
"columns": [
    {
        // THIS IS THE 'ACTION' COLUMN: it contains links related to the data row and that links should depend on data in 'schema' key
        "data":           null,
        "render": function(data, type, row, meta) {
             // HERE I NEED TO ACCESS 'schema' key to output functional buttons
        }
    },
    {
        "data": // HERE I NEED TO ACCESS 'dati' key
    }
]

Could someone help?
Thank you

Alex

Answers

  • alzamboalzambo Posts: 38Questions: 17Answers: 1

    ...up...

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    Try adding console.log(row); into your rendering function to see what data is available in the row.

    I don't know what the demo SSP class will do with the input configuration above - perhaps you've modified it to allow for that and return the required JSON?

    Allan

  • alzamboalzambo Posts: 38Questions: 17Answers: 1
    edited November 2016

    Hi Allan,
    it doesn't display anything (not even errors) on render function.

    I need to precise that my table is client side, with data array populated in php and converted to js array with

    datiTabella = <?= json_encode($datiTabella);?>;
    

    This is my js

            var table = $('#tabellaDati').DataTable( {
                "scrollX": true,
                "autoWidth": false,
                "stateSave": true,
                },
                "data": datiTabella,
                "columns": [
                    {
                        "sWidth": "2%",
                        "className":      'details-control',
                        "orderable":      false,
                        "data":           null,
                        "render": function(data, type, row, meta) {
                            return  '<a class="btn btn-xs" href="modello_form_modal.php?tabella=<?= $obj->getTableName(); ?>&action=edit&id='+data.id+'" title="Modifica"><span class="glyphicon glyphicon-edit"></span></a>'+
                                '<a class="btn btn-xs delete-object" delete-id="'+data.id+'" title="Elimina"><span class="glyphicon glyphicon-trash"></span></a>'
                        }
                    },
                    {
                        "data": 'dati.id',
                        "render": function (data, type, row, meta) {
                            console.log(row);
                        }
                    }
                ]
            });
    

    and this is html

                                        <table id="tabellaDati" class="table table-bordered table-hover table-striped nowrap">
                                            <thead>
                                                <tr>
                                                    <th>Azioni</th>
                                                    <th>id</th>
                                                </tr>
                                            </thead>
                                        </table>
    

    Thank you for helping me...
    Alex

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    Can you show me what datiTabella contains then please?

    Thanks,
    Allan

  • alzamboalzambo Posts: 38Questions: 17Answers: 1

    Hi Allan,
    here it is:

    {
        "dati": [
            {
                "id": "1"
            },
            {
                "id": "2"
            }
        ],
        "schema": [
            {
                "nomeCampo": "id",
                "relazione": "relazione"
            }
        ]
    }
    

    Thanks
    Alex

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    Hi Alex,

    Which array is the one you want to show in the DataTable? You could use data: datiTabella.schema if you want to show the schema array for example.

    Each array element is a row in the table.

    Allan

  • alzamboalzambo Posts: 38Questions: 17Answers: 1
    edited November 2016

    Hi Allan,

    datiTabella.schema would contain just one 'trigger' (basically the related field name). I need it in a function for output an additional link (to perform a db query) in every row. So it's logically related to the column and not to every row.

    So I need to use both arrays: datiTabella.dati on id column and datiTabella.schema on Azioni column.

    Would it be possible?

This discussion has been closed.