Update cell based on unique column value

Update cell based on unique column value

sbarnettsbarnett Posts: 23Questions: 5Answers: 0
edited October 2011 in DataTables 1.8
Hi,

I have a server side script which sends JSON to the JS on the client side. The data contains a unique ID (which maps to a hidden column in the table which has already been drawn). I need this JS to be able to take that unique ID and use the JSON data to update the row specified by the unique ID.

Does that make sense?

How would I go about getting hold of the row I need to update based on the unique column value?

Thanks,
Simon

Replies

  • OmnimikeOmnimike Posts: 22Questions: 0Answers: 0
    I don't know of any other way besides looping over the nodes in the table and looking for the row with the id you are looking for. Something like this:
    [code]

    var oTable = $('#gridId').dataTable();
    var nRows = oTable.fnGetNodes();
    for (var i in nRows) {
    if (oTable.fnGetData(nRows[i]).id == idYouAreLookingFor)
    oTable.fnUpdate(newData, nRows[i])
    }
    [/code]
  • sbarnettsbarnett Posts: 23Questions: 5Answers: 0
    Ugh, that's a bit cumbersome. Shame, I was hoping for something like getRowNum(id).

    Don't spose anyone else has any ideas that might be a bit more optimal?

    (Thanks by the way - not putting your suggestion down - I'd just like for there to be a more efficient way)
  • OmnimikeOmnimike Posts: 22Questions: 0Answers: 0
    Instead of (or as well as ) using a hidden column with the id in it you could always assign the id to tr element of each row. This would make it a lot easier to look up the row by it's id later on.
    [code]
    var table = $('#tableId').dataTable({
    fnRowCallback: function(tr, rowData) {
    $(tr).attr(rowData.id);
    return tr;
    }
    });

    table.fnUpdate(newData, $('#'+idYouAreLookingFor)[0]);
    [/code]
    In reality you would probably want to prefix the ids given to each of the rows with something to make sure they don't conflict with any other ids on the page, but that's the basic idea.
  • OmnimikeOmnimike Posts: 22Questions: 0Answers: 0
    Actually I just found this page http://www.datatables.net/plug-ins/api which has functions called fnFindCellRowIndexes and fnFindCellRowNodes which seem to do exactly what you want.
This discussion has been closed.