RowGroup aggregation data-attributes

RowGroup aggregation data-attributes

chrisjshieldschrisjshields Posts: 4Questions: 1Answers: 0

I am extending the custom row example in https://datatables.net/extensions/rowgroup/examples/initialisation/customRow.html to provide an average aggregation if the cell values contain a % symbol, otherwise a sum aggregation.

I would like to be able to read the data attributes of either the column header or the cell to be able to retrieve a " data-aggregation-disabled" attribute. Is this possible? How can I get an instance of the HTML object?

I have tried $(v.cell().node()), but no luck.

Many thanks for any help. rowGroup endRender Code follows:

    function endRender(rows, group) {
        const tr = document.createElement('tr');
        this.addCell(tr, group);
        const columns = rows.data()[0].length;
        for (let i = 1; i < columns; i++) {
            const v = rows.data().pluck(i);
            if (v[0].endsWith('%')) { // Average
                this.addCell(tr, v.reduce((a, b) => parseFloat(a) + parseFloat(b) * 1, 0).toFixed(1) + '%');
            } else { // Sum
                if (typeof v[0] === 'string') {
                    this.addCell(tr, v.reduce((a, b) => this.extractNumber(a) + this.extractNumber(b), 0));
                } else if (typeof v[0] === 'number') {
                    this.addCell(tr, v.reduce((a, b) => a + b, 0));
                } else {
                    this.addCell(tr, '');
                }
            }
        }
        return tr;
    }

Answers

  • kthorngrenkthorngren Posts: 21,946Questions: 26Answers: 5,068
    edited May 8

    Possibly you can use column().header() in the for loop to get the th cell then get the data attribute from there.

    It would be more difficult to get the cell node. Probably would require refactoring the code so you can get the row index to pass in the row and column indexes into cell().

    Kevin

  • chrisjshieldschrisjshields Posts: 4Questions: 1Answers: 0

    rows.column(i).header() works perfectly, thank you!

Sign In or Register to comment.