Search code examples
javascriptjqueryfor-loophandsontable

Fill my handsontable with loop


Having trouble filling my handsontable.

function fillTable() {
var PV = $("#PresentValue").val();
var FV = $("#FutureValue").val();
var IntRate = $("#InterestRate").val();
var PmtPeriods = $("#PaymentPeriods").val();
var StartDate = $("#anStartDate").val();
for(var i = 0; i < PmtPeriods; i++) {
    var data = [
                    ["", "Date", "Invoice amount", "Interest rate", "Interest amount", "Amortization", "Capital balance"],
                    [1, StartDate, 2000, IntRate, PV*IntRate/360*30, 1500, 100000,]
              ];
    $("#dataTable").handsontable({
        data: data,
        startRows: 10,
        startCols: 7
    });
}

}

I just want to table to be filled as long as the loop are going. I know this isn't working but that's all I have for now.

If possible, I would like that certain cells on row 2 are calculated depending on some cells on row 1. Is this possible? But first I would really like some help with my loop..


Solution

  • You need to build the array in your loop, and then call handsontable afterwards. Here is a first-cut of restructuring the code. You will probably still need to tweak it to get what you want:

    var data = [["", "Date", "Invoice amount", "Interest rate", 
                 "Interest amount", "Amortization", "Capital balance"]];
    
    for(var i = 0; i < PmtPeriods; i++) {
        data.push(
            [i + 1, StartDate, 2000, IntRate, PV*IntRate/360*30, 1500, 100000]);
    }
    
    $("#dataTable").handsontable({
        data: data,
        startRows: 10,
        startCols: 7
    });