row.add() and DT_RowID with dom table?

row.add() and DT_RowID with dom table?

mihomesmihomes Posts: 150Questions: 19Answers: 0
edited April 2014 in DataTables 1.10
When adding a row to a dom table using row.add() how can I specify the DT_RowID for it?

[code]
"columns": [
{
"searchable": false,
"sortable": false
},
/* 1 filename*/ null,
/* 2 filesize*/ null,
/* 3 timestamp*/ null
],

//Example row creation with php in the dom. I add the rowid manually here.
echo '


'.$file.'
'.formatSizeUnits($filesize).'
'.$filetime.'

';

//Now say I want to add a row to the table. Since this is dom I need to manually add it and draw the table afterwards.
dt.row.add( [
'td0',
'td1',
'td2',
'td3'
] ).draw();

[/code]

Replies

  • mihomesmihomes Posts: 150Questions: 19Answers: 0
    I ended up using the created row callback... is this the only way to go about this?

    [code]
    "createdRow": function( row, data, dataIndex ) {
    $(row).attr('id', data[1]);
    },

    [/code]
  • mihomesmihomes Posts: 150Questions: 19Answers: 0
    Another issue... adding the row is not working inside an ajax response... response returns correctly yet the row is not added.

    [code]
    // this works and adds the new row to the table
    $('#test').on('click', function () {
    dt.row.add( [
    'td0',
    'td1',
    'td2',
    'td3'
    ] ).draw();
    });

    //same code does not work within an ajax response...
    $('#dtCreate').on('click', function () {

    $.ajax({
    type: 'post',
    url: '/test/process/p_db_create.php'
    }).done(function (response) {

    if (response.success)
    {
    //add the row since this is not serverside
    dt.row.add( [
    'td0',
    'td1',
    'td2',
    'td3'
    ] ).draw();
    ...more code below...
    [/code]
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    > When adding a row to a dom table using row.add() how can I specify the DT_RowID for it?

    It might sound daft, but just specify `DT_RowID` as a property. Obviously you can't do this if you are using an array data source, but if you are using objects, it is that simple.

    > Another issue... adding the row is not working inside an ajax response... response returns correctly yet the row is not added.

    Looks like it should work. I'd need a test case showing the issue please.

    Allan
  • mihomesmihomes Posts: 150Questions: 19Answers: 0
    Based on your response I changed this to an ajax source and am returning DT_RowID, but it is not being added to the tr's.

    An example ajax return :

    [code]
    {
    "data":[
    {
    "DT_RowID":"1398142350",
    "checkbox":"",
    "filename":"1398142350.sql.gz",
    "filesize":"7.57 KB",
    "timestamp":"April 22nd, 2014 @ 12:52 am EDT"
    },
    {
    "DT_RowID":"1398143071",
    "checkbox":"",
    "filename":"1398143071.sql.gz",
    "filesize":"7.57 KB",
    "timestamp":"April 22nd, 2014 @ 1:04 am EDT"
    }
    ]
    }
    [/code]

    my columns in the initialization :

    [code]
    "columns": [
    {
    "data": 'checkbox',
    "searchable": false,
    "sortable": false,
    "class": 'center'
    },
    { "data": "filename" },
    { "data": "filesize" },
    { "data": "timestamp" }
    ],
    [/code]
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    It should be `DT_RowId` - sorry, I should have spotted that before. See list of options available is here: http://next.datatables.net/manual/server-side#Returned-data

    Allan
  • mihomesmihomes Posts: 150Questions: 19Answers: 0
    I should have caught that... sorry and thanks again. Everything working smoothly. Regarding the ajax response I had left out the 'dataType : 'json'' by accident which is why that was not working.
  • repziorepzio Posts: 2Questions: 1Answers: 0
    edited October 2014

    So I have to change the models that come back from my server to contain DT_RowID?

    Is there no other way to specify which column to use for DT_RowID?

  • snissnis Posts: 1Questions: 0Answers: 0

    please help me

    i have:

          $.ajax({
                        url: "tratarGestor.php?acao=inserirCat",
                        type: "POST",
                        data: form,
                        success: function(idInserido) {
                            tabelaCat.row.add
                                (
                                    [
                                        nomeCat.value
                                    ]
                                ).draw();
                        }
    
                    });
    

    when a draw the new tr with the nameCat.value, i want it have the id=idInserido..

    i test a lot of ways but i can't put this ON :(

    How is the way to the new "tr" have a id like "idInserido"?

    Tanks

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    To manipulate the DOM elements you would probably be best to use columns.createdCell or createdRow.

    Allan

This discussion has been closed.