2

I'm trying to change the 5th column's table data in my Jquery DataTable, by going to the specific table row ID. By default, all of my table rows have an id of row<id number>. For example:

<tr id="row147">
  <td>147</td>
  ...

To attempt to change a specific table data cell, I tried something like this

table.row("#row" + data.job_id).data(variable).draw()

But this writes data across the entire row in each table data cell. How do I specify the table data cell within the row and just have it write in that with the DataTables API?

2
  • I tried table.row(row).column(5).data(variable).draw(), and while it isn't throwing any error, it is not updating the cell in that column. Commented Feb 2, 2017 at 23:41
  • disregard comment... Commented Feb 3, 2017 at 1:30

6 Answers 6

2

Your code should look something like this. Add the initComplete:

$(document).ready(function () {
    $("#uiDataTable").DataTable({
        "order": [[1, "asc"]],
        "pagingType": "full",
        "deferRender": true,
        "initComplete": function (settings, json) {
            $('tbody tr').each(function () {
                var nTds = $('td', this);
                var nTrs = $('tr', this);
                //row #  
                if (nTrs.context.id == "4") {
                    $(nTds[0]).text("Changing row 3 column 1");
                    //alert(sBrowser);
                }
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Is there no way to accomplish what I am asking via the API? initComplete is an option..
2

I ended up finding a solution by following this post here. Basically have to give every <td> an ID. Then I can use table.cell('#td_5_' + data.job_id).data(variable) to write in the new data to a specific cell.

Comments

2

This is possible if you read through the docs on the row-selector you will notice it says:

also optionally cells() and cell()) methods provide the ability to select rows from the table

So if you go to the cell docs:

You will see the overloaded cell method:

cell( rowSelector, columnSelector, [ modifier ] )

For your case:

table.cell("#row" + data.job_id,'').data(variable).draw()

table.cell("#row" + data.job_id, column_index).data(variable).draw()

Comments

1

's method wont work when you change the order, because the rowIndex, is the order in which the row was loaded, not the order it is currently displayed like here . If you want it based on the current position in the table, it's probably easiest to use jQuery using row index -

$('#yourTableId tbody tr:eq(rowIndex) td:eq(columnIndex)').text('yourvalue');

Comments

-1
var rowId = "row" + data.job_id;
var element = document.getElementById(rowId).cells;
element[index_of_column].innerHTML = "New content";`

1 Comment

I should have rephrased my question on how it is done via the DataTables API.
-1
$('#'+table).find('tr#'+rowId).find('td:eq(colNum)').html(newValue);

1 Comment

Your answer was flagged as low quality because it lacks an explanation. Expand upon your answer, so that it is not deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.