setting TR ID after using fnAddData (SOLVED)

setting TR ID after using fnAddData (SOLVED)

jrizzi1jrizzi1 Posts: 44Questions: 12Answers: 3
edited March 2011 in General
I hvae been attempting for some time now without success so i thought I would ask...I have been trying to modify this thread's code to set the TR id instead of the TD id http://datatables.net/forums/comments.php?DiscussionID=1378
[code]
var newRow = dataTable.fnAddData([//
response,
$( '#col1' ).val(),
$('#col2 option:selected').text(),
$( '#col3' ).val()
]);

var oSettings = dataTable.fnSettings();
var nTr = oSettings.aoData[ newRow[0] ].nTr;
$('tr', nTr)[0].setAttribute( 'id', response );
[/code]

This does not work to set the TR id, however if i was to switch the attribute setting to td, then it successfully sets the td id
[code]
$('td', nTr)[0].setAttribute( 'id', response );
[/code]

please forgive me if I am asking novice questions, I am just trying to understand

Replies

  • jrizzi1jrizzi1 Posts: 44Questions: 12Answers: 3
    edited March 2011
    I knew as soon as I posted something i would figure it out, always happens that way


    To set a TR ID, you do it in the setup options, not after the fnAddData

    so for my version, here is my datatable setup code which sets bjqueryui, then the row callback
    [code]
    $( '#records' ).dataTable({
    "bJQueryUI": true,
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    $(nRow).attr('id', aData[0]);
    return nRow;
    }
    });[/code]

    My aData is the HTML of my jquery template, the first value is the ID from the database, if i wanted to use an id based off of actual row position i would sub out aData[0] for iDisplayIndexFull, and for paginated display row position iDisplayIndex

    Maybe i'll help someone else
  • mullermuller Posts: 1Questions: 0Answers: 0
    edited April 2011
    You method works when the first column of the table is also the id of the row.

    In my case the first column has some other value, so this is what I had to do to set the id from some other place:
    [code]
    //usual stuff
    oTable = $('#table_id').dataTable();
    var a = oTable.fnAddData([
    $('#field_1').val(),
    $('#field_2').val()' ] );
    //save the table settings in a variable
    var oSettings = oTable.fnSettings();
    //count how many rows has the table, minus three (don't ask me why)
    var rows=$('#table_id tr').length-3;
    //get the position of the newly inserted item on the table (plus one)
    var position=oSettings.aoData[rows].nTr.rowIndex+1;
    //set the tr id to some other value that is not on the aData
    $($('#issues_table tr')[position]).attr('id', $('#id_field').val());

    [/code]
This discussion has been closed.