Hidden columns and cell().data( set )

Hidden columns and cell().data( set )

Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

With cell().data() it is possible to update the contents of cells. But there is one "but". This only will do what you expect if the columns before the cell and the column of the cell itself are visible. If not then you will update a cell in another column. I experienced this today and resolved it by calculating a column offset based on eventual columns before the target column that were invisible .

Let's say we have a table with 8 rows.

Columns 1 and 3 are made invisible by:

table.column(1).visible(false);
table.column(3).visible(false);

Now there are updates for some cells in column(6). I can find those columns
very easy because they are classified by classes.

I could do an update with

table.cell(rowNumber, 6).data(upDatedData);

But because there are 2 columns before my target column invisible, the
update will appear in column 8.

I resolved this by:

  1. First of all, checking of the target column (6) is visible,
  2. And if so, count the number of eventual invisible columns before
    column 6. This will give me an offset.
  3. Now I can do the update by:
table.cell(rowNumber, 6 - offset).data(upDatedData);
function updateCells(classToFind, data) {

    /*  Get all the cells we have to update. */
    targetCells = table.cells(classToFind);


    /*
        Check if our target column is visible, because if not, we will not
        be able to update the cells.
    */


    if (table.column(targetCells[0][0].column).visible()) {


        /*  Initialize nummber visible columns. (1 because our target col is visible) */
        columnsVisible = 1;


        /*  Let's count how many columns before our target column are visible. */
        for (var i = targetCells[0][0].column - 2; i >= 0; i--) {
            if (table.column(i).visible()) columnsVisible++;
        }

        /*  Now we can calculate the column offset. */
        columnsOffset = targetCells[0][0].column - columnsVisible;


        /*  Update our target cells. */
        for (var i = targetCells[0].length - 1; i >= 0; i--) {


            /*  If one our more columns before our target column not are
                visible it will be corrected by the offset.
            */

            table.cell( targetCells[0][i].row,
                        targetCells[0][i].column-columnsOffset).data(data);
        }
    }

    else {
        // Problem, because our target is not visible, how to update the data now?
    }
}

Answers

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    Can you give me a link to a test case showing the issue please? I've just put together this little example showing it working.

    Allan

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    @allan, at the and of the day I will place things on a test server and then I will PM you the credentials to do the login stuff etc.

This discussion has been closed.