How can I update a column after other row columns are changed?

How can I update a column after other row columns are changed?

rprobinsonrprobinson Posts: 14Questions: 3Answers: 0

I've a table that has multiple row columns that are initially updated based on other values in the row (and for some columns, values of a cell in the previous row).

After the table is initially populated with updated row data, I need to finally create a Total Column by walking through the rows applying an algorithm to various columns of data that span multiple rows. The algorithm basically states: take the sum of next 6 rows of column A, B, and C; divide by a constant, and place in the Total column of row 1. The move on to row 2, Total Column by applying the same algorithm. Repeat until the entire table is processed.

I've no clue on how to accomplish this; I'm assuming there's a way to pull in the columns of interest, walk through the columns, apply the algorithm, and then update the Total Column. Ideally I just refresh the Total Column being displayed and not all column.

Keep in mind the the initial creation for the rows already has calculated column values that are needed in the above algorithm. I have to let the entire table first be created, then apply the algorithm.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,889Questions: 1Answers: 10,530 Site admin

    Typically with calculated values you would use columns.render, but that won't help in this case since it is really only designed to give you the data for the current row.

    What you would need to do is use drawCallback and get the rows in the table (table.rows( { order: 'applied' } )) and iterate over them. You could get the data for the cells in a given column (table.cells( null, 2, {order: 'applied'} ).data() - where 2 is the column index) and then loop over it like you would with an array. Then write the values into the cells directly.

    Does that make sense? I can put together a little example if it would be useful.

    Allan

  • rprobinsonrprobinson Posts: 14Questions: 3Answers: 0

    Example would be helpful.

    I've the drawCallback working and I'm able to get arrays for the columns of interest. I've the algorithm working as required.

    The "write values into cells directly" is unclear. Not sure how I get a handle on the underlying data, change that cell, then use html td reference for that cell.

    I'll also need to change the cell background column for the Total Column of each row (<0 red, 0 yellow, > 0 green).

    I did try cells().invalidate to attempt to get the values present on the web page, but I believe I'm missing the fundamentals of updating the underlying data.

  • allanallan Posts: 63,889Questions: 1Answers: 10,530 Site admin
    Answer ✓

    Here we go: http://live.datatables.net/regufaso/1/edit .

    The numbers are obviously meaningless here, and I've assumes that the "next six rows" includes the current row, and that when you get to the point where there aren't six or more rows that it just ignores them.

    Does it make sense with the example, or is there anything that I can explain?

    Thanks,
    Allan

  • rprobinsonrprobinson Posts: 14Questions: 3Answers: 0

    This makes sense.

    I also found if I updated the underlyling data and when done do:

       api.cells().invalidate('auto');
    

    I'd get the behavior I wanted.

    Thanks for your help.

This discussion has been closed.