Updating individual cells in a row

Updating individual cells in a row

ChrisDennisChrisDennis Posts: 12Questions: 5Answers: 0
edited March 2017 in Free community support

I can't work how to update just the changed cells in a row.

I can get the relevant row like this:

let row = this.dt1.row('#row-' + data.id);

and then update the whole row successfully with

row.data(data).draw(true);

But since a change to the backend, data now only has changed values, so I want to iterate through all the cells in the row, and update the cells for which there is new data.

I tried this

row.cells().every(function () { ... }

but that function is fired for every visible cell in table, not just the one row.

What should I do?

`

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    Answer ✓

    The cell-selector can be used to select a specific cell. If you know the specific row and column you could do something like this:

    table.cell({row:2, column:1}).data('New value for row 2 column 1');
    

    Kevin

  • ChrisDennisChrisDennis Posts: 12Questions: 5Answers: 0
    edited March 2017

    Thanks Kevin -- that partly solves the problem, and got me thinking on the right path.

    I didn't emphasise in my question that I want to iterate across the columns and check which have data to update.

    With a bit of help from Stackoverflow I eventually ended up with this, which relies on the properties of my msg having the same names as the data entries in the table's column definitions. (The columns array also has the name of each column, so it could use those for matching if required.)

    let msg = { id: 1, customer_name: 'Fred' };  // source of updates (from backend)
    let row = table.row('#row-' + msg.id);
    let rowindex = row.index();                                                            
    let columns = table.settings().init().columns;                                      
    table.columns().every(function (colindex) {     
         let coldata = columns[colindex].data;  // 'data' as in the DT column definition
         if (coldata != 'id' && msg.hasOwnProperty(coldata)) {  // (don't update the id!)
             table.cell({row: rowindex, column: colindex}).data(msg[coldata])  
         }                                                                              
    });
    
This discussion has been closed.