JSFiddle - React, Tailwind, and code Playground

by Mike McAngus

HTML

<script src="http://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>
<a class="addRow" href="#">add row</a>

<table id="example">
    <thead>
        <tr>
            <th>Invoice ID</th>
            <th>Vendor ID</th>
            <th>Product ID</th>
            <th>Seq Num</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        </tr>
    </tbody>
</table>

JavaScript

function addRow() {
    var nextSeqNum = (maxIntValue(exampleTable, ".seqNum") + 1);
    var col0 = 'Invoice 1';
    var col1 = 'Vendor 3';
    var col2 = 'Product 3.' + nextSeqNum;
    var col3 = nextSeqNum;
    exampleTable.row.add([col0, col1, col2, col3]).draw();
}

function maxIntValue(table, colSelector) {
    var valArray = exampleTable.column(colSelector)
                               .data()
                               .sort()
                               .reverse();
    for (var i=0;i<valArray.length;i++) {
        if (!isNaN(valArray[i])) {
             return parseInt(valArray[i]);
        }
    }
    return 0;
}