Cloning a row, editing it then adding it.

Cloning a row, editing it then adding it.

CGRemakesCGRemakes Posts: 32Questions: 1Answers: 0
edited September 2012 in General
I'm trying to clone an existing row, change a few of the column values, then append the cloned row back to the table with a redraw of the table to apply sorting and formatting and things. I've got it working using the fnAddTr plugin, but I'm doing it by converting the row to .html. Is there a way to add it without having to convert it to HTML? Here is a snippet of the code I'm working with:

[code]

var row_clone = $('#claimTable tbody tr:eq(1)').clone();

row_clone.find('input[name="claim_id[]"]').each(function(){

$(this).val(v);
});

row_clone.find('input[name="claim_id_row[]"]').each(function(){

$(this).val(v);
});

row_clone.find('input[name="serial_num[]"]').each(function(){

$(this).attr('value', k);
});

row_clone.find('input[name="ship_date[]"]').each(function(){

$(this).attr('id', 'dp' + v);
});

var row_copy = '' + row_clone.html() + '';

oTable.fnAddTr($(row_copy)[0]);

[/code]

I tried just passing row_clone, instead of $(row_copy)[0], but it didn't seem to work.

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    You'd need to modify the fnAddTr plug-in to accept strings as well as the DOM nodes that it currently expects. A simple check for `typeof nTr === 'string'` and then convert like you've done should be enough.

    Allan
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Why do you need to use row_clone.html() at all btw? Can't you just pass row_clone[0] into fnAddTr? It should already be in a TR element.

    Allan
  • CGRemakesCGRemakes Posts: 32Questions: 1Answers: 0
    edited September 2012
    Ah, that's better. That was the question, I couldn't figure out how to do it without converting it into a string. It seemed like a pointless step, so I tried passing just row_clone (which didn't work), I didn't know I needed to add the [0]. Now that I think about it, that was pretty dumb of me. ;)
This discussion has been closed.