row.add and render function

row.add and render function

trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

Hello,
I'm initializing a datatable using AJAX, createdRow and some render functions on some columns :smile:

var tableDONNEES = $("#tableDONNEES").DataTable( {
        "ajax": {
            url: "myscript.php",
            type: "GET",
            data:{
                'param' : "list"
            }
          },
...
"createdRow": function( row, data, dataIndex ) {
            if (data['eds']=="NO") $('td:first-child', row).addClass('some_class');
          },
...
"columns": [{name: 'id', data:"id", type: "num" },
{name: 'nom', data:"nom"},
                    {name: 'ville', data:"ville", render:function ( data, type, row ) { 
                        if (data['link']=="") return data['CP']+" " + data['nom'];
                        return data['link'];
                    }},
                    {name: 'direction-nomprenom', data:"direction", 
                        render:function ( data, type, row ) { return data['nomprenom']; }
                    },
                    {name: 'direction-tel', data:"direction", 
                        render:function ( data, type, row ) { return data['tel'];}
                    }
],
...

It works perfectly but now, I'm trying to add a row using an AJAX call with success function here :

success: function(response) {
var rowNode = tableDONNEES.row.add({
                                "id":           response.id,
                                "nom":          nom,
                                "ville":        $('select[name="ville"] option:selected', '#form').text(),
                                "direction-nomprenom":      response.direction,
                                "direction-tel" : response.direction
                            } )
                            .draw();
}

The script which returns the "response" variable is like this :

$dataArray["id"] = $somenumber;
$dataArray["nom"] = $somenumber;
$dataArray["ville"] = $something;
$dataArray["direction"] = array(); 
     $data =array();
     $data["data"] = array("nomprenom"=>"something1", "tel"=>"something2");
$dataArray["direction"] = $data;
$dataArray["eds"]=$something; // "YES" or "NO"
echo json_encode($dataArray);

The add function does not work. Of course, I expect that the array returned by my script to the success function is not correctly formed. But I don't know how to correct it.

Could you help me please ?

Many thanks in advance !

T.

Answers

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    In what way does it not work? Any error messages or anything shown on the console?

    Thanks,
    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Oups, sorry... There is the following message in the console :
    TypeError: undefined is not an object (evaluating 'data['nomprenom']')
    and the line where this error occurs is
    render:function ( data, type, row ) { return data['nomprenom']; }

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    In the PHP code above you have:

    $data["data"] = array("nomprenom"=>"something1", "tel"=>"something2");

    But $data isn't actually used anywhere else - it isn't being output.

    I also don't see a nomprenom property in the data that you pass to row.add().

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    $data is being used on line 7 : $dataArray["direction"] = $data;

    The column named "direction-nomprenom" is supposed to get data named "direction" (because of 'data:"direction"' in the column declaration) and it's the data['nomprenom'] element which is used... So I thought that I had to output a json array with an item named "direction" who has a sub-array named "nomprenom"...

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Doh - I missed that - sorry.

    In that case, yes I agree it looks like it should work!

    Can you give me a link to a page so I can debug the issue?

    Thanks,
    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2
    edited April 2017

    Hello Allan,
    Thank you very much for your help.
    I sent you a private message with login & password credentials.
    Best regards,
    T.

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Hello Allan,
    Did you find my private email with login informations ?
    Thank you very much for your valuable help !
    T.

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Hi,

    Yes, sorry I haven't got back to you about this yet. I've not had a chance to look into it yet, but will do so as soon as possible (probably Monday now) and let you know.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Thank you... :-)
    T.

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Thanks for the link.

    If I put a break point into the rendering function where the error is happening I can see that the row's data structure after I've clicked the button is:

    {
        "id": 961,
        "nom": "allan",
        "ville": "4000 Liège",
        "direction-nomprenom": {
            "data": {
                "nomprenom": " ",
                "tel": ""
            }
        },
        "direction-tel": {
            "data": {
                "nomprenom": " ",
                "tel": ""
            }
        },
        "contact-nomprenom": {
            "data": {
                "nomprenom": " ",
                "tel": ""
            }
        },
        "contact-tel": {
            "data": {
                "nomprenom": " ",
                "tel": ""
            }
        },
        "ecole_de_stage": "OUI"
    }
    

    That gives an error because the column is configured as data: "direction", and as you can see above there is no direction object in that object.

    So the data being used for row.add() is not in the same format as the data that is being used to initially populate the table.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Wow... I got it ! Thank you very, very much !!

This discussion has been closed.