1

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..

user229044
  • 232,980
  • 40
  • 330
  • 338
MrProgram
  • 5,044
  • 13
  • 58
  • 98

1 Answers1

1

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
});
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • This works perfect. Is it possible to calculate the second row depending on values in the first row? – MrProgram Mar 12 '14 at 14:27
  • Yes, but it depends upon what data the second row contains. You will probably need to use JavaScript [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) objects. You can probably use `parse` to load the `Date` object. Then see [here](http://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript) and [here](http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date) for how you might perform arithmetic on the dates. – Justin Ethier Mar 12 '14 at 14:32
  • I fixed it with the date, but the other values on row 1 is the source for row 2 values. For example the last cell of row 1 "value: 100000" should depend on another cell and then get lower – MrProgram Mar 12 '14 at 16:01