Modify other cell in row based on current cell value

Modify other cell in row based on current cell value

dknifedknife Posts: 14Questions: 4Answers: 1
edited June 2018 in Free community support

Hi, new here and loving this product. Perfect for what I need and the Editor is fantastic.

I'm trying to modify a cell in a row based on logic from the current createdCell.

columnDefs: [{
            targets: 'desig-next-th',
            createdCell:function(td, cellData, rowData, row, col) {
                var desigNext = true;
                var desigNextClass;
                if (cellData >= 95) {
                    $(td).addClass('desig-next-95 desig-next');
                    desigNextClass = 'desig-next-95';
                } else if (cellData >= 90) {
                    $(td).addClass('desig-next-90 desig-next');
                    desigNextClass = 'desig-next-90';
                } else if (cellData >= 85) {
                    $(td).addClass('desig-next-85 desig-next');
                    desigNextClass = 'desig-next-85';
                } else if (cellData >= 80) {
                    $(td).addClass('desig-next-80 desig-next');
                    desigNextClass = 'desig-next-80';
                } else if (cellData >= 75) {
                    $(td).addClass('desig-next-75 desig-next');
                    desigNextClass = 'desig-next-75';
                } else {
                    $(td).addClass('desig-next');
                    desigNext = false;
                }

                if (desigNext === true) {
                    if (cellData > 75 && rowData.nextDesignationType === 'gci') {
                        table.cell(row,'gci-net:name').node().addClass(desigNextClass);
                    } else if (cellData > 75 && rowData.nextDesignationType === 'units') {
                        table.cell(row,'units-net:name').node().addClass(desigNextClass);
                    }
                }

            }
        }]

I just can't get the selector right for modifying the other cell. I get the current row index from createdCell but that is only an integer index, not a jquery selector. So I'm trying to reference the cell by specifying the row index as the first parameter of cell() then modify the td DOM element with the class however no matter how I try different ways to select the cell (name, class, it always states addClass is not a function.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    Hi @dknife ,

    It would be better to use createdRow in that instance, then you have access to all the row immediately, as shown in this example here.

    Cheers,

    Colin

  • dknifedknife Posts: 14Questions: 4Answers: 1
    edited June 2018

    Thank you very much, that allowed me to reference the cell to determine the outcome and modify the specific cells with the required classes.

    One more question though. Since the row parameter for createdRow is a DOM jquery object, I guess there is no way to reference the cell by its data identifier or name? Specifying the cell reference by internal pointer # is terrible for when you add new cells, which is why I was trying to originally reference the row object then the node.

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Hi @dknife,

    Yep, the second and third parameters on the createdRow are data and dataIndex - data contains the data (whether that's an array or an object depending upon how the table is defined), and dataIndex is the row, which you can use in the API calls such as row(),

    Cheers,

    Colin

  • dknifedknife Posts: 14Questions: 4Answers: 1

    I'm sorry but I don't know where to go from there. Could you please provide an example of how to modify the class of a specific cell by its data name in the createdRow function?

    How do I go from this:
    $('td',row).eq(29).addClass('desig-next-95 desig-next');

    To the direct cell reference being defined as such:

    {data:'nextDesignation',searchable:false,render:$.fn.dataTable.render.number(',','.',0,'','%')}

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    I guess there is no way to reference the cell by its data identifier or name?

    You are correct - unfortunately there isn't currently an option for that. You need to do it on a per index basis - or better might be to have a map lookup which you could modify if the table strcuture changes in future. Also, I'd suggest using the fourth parameter given to the createdRow method rather than $('td', row).eq(29) - e.g. that would become $(cells[29). The reason for this is that the former would not take into account if any of the cells have been made hidden by colun visibility methods.

    Allan

This discussion has been closed.