我试图在模态中创建动态表进行摊销

Javascript: I am trying to create dynamic table inside modal for amortization

本文关键字:动态 创建 模态      更新时间:2023-09-26

我试图为摊销表创建一个动态表,但我不能追加列。它每次插入一行,所以我的每个值都在新行中,我如何创建循环,使下一列应该是+1。因为这是第(0)行,所以它每次都是向上的,而整个表是上下颠倒的

i write so far

function writeln(str) {
  var output = document.getElementById("output");
  var txt = document.createTextNode(str);
  var row = output.insertRow(0);
  var cell1 = row.insertCell(0);
  cell1.innerHTML = str;
}
function main() {
  for (var i = 1; i <= numpayments; ++i) {
    sumpay += payments;
    propertypriceapp = 0.0090 + propertypriceapp;
    var thisint = balance * intrate;
    sumint += thisint;
    var thisprin = payments - thisint;
    sumprin += thisprin;
    balance -= thisprin;
    var curvalequity = sumprin * (1 + propertypriceapp);
    writeln(flushright(i, 5));
      // flushright(fmtmoney(payments), 9) + "  " + 
      // flushright(fmtmoney(thisint), 9) + "  " + 
      // flushright(fmtmoney(thisprin), 8) + "  " + 
      // flushright(fmtmoney(sumpay), 9) + "  " + 
      // flushright(fmtmoney(sumint), 9) + "  " + 
      // flushright(fmtmoney1(sumprin), 9) + "  " + 
      // flushright(fmtmoney(balance), 12) + "   " +
      // flushright(fmtmoney1(curvalequity), 9) + "  " +
      writeln(flushright(fmtmoney1(propertypriceapp), 9));
    // write("----------------------------------------------------------------------------------------------------------------------")
    // writeln("")
  }
}
我的HTML代码是
<table class="modal-body" id="output">
        </table>

您的writeln函数每次调用时创建1行和1列。

它有1个变量,那是不可用的- var txt = document.createTextNode(str);,每次调用DOM - var output = document.getElementById("output");这对性能不利,而且代码很难理解。

如果您想在一行中创建更多列,您的代码必须像这样:

var output = document.getElementById("output");
function writeln(str) {
    // str -> must be array, that have lenght = num of columns, you want to create
    var row = output.insertRow(0),
        len = str.lenght; // count of rows
    // creates rows 
    for (var i = 0; i < len; i++) {
        // create new row
        var cell = row.insertCell(i);
        // insert HTML code in it
        cell.innerHTML = str[i];    
    }
}

如果你想创建更多的行与列,你必须使它在循环中,并且每次改变var row = output.insertRow(j),行的j参数。

这就是上述问题的解决方案,STR被转换成数组

temp = str.split(" "); // this will split string into ","

之后是用于创建单元格的数组

var table=document.createElement('TABLE');
table.setAttribute('class','table table-striped table-bordered')//this is bootstraps classes
var tbdy=document.createElement('TBODY');
table.appendChild(tbdy);
function writeln(str) {
  var output = document.getElementById("output");
    var temp = new Array();
    temp = str.split("  ");
     var tr=document.createElement('TR');
     tbdy.appendChild(tr);
     for (var zxc2=0;zxc2<temp.length;zxc2++){
      var td=document.createElement('TD');
      td.appendChild(document.createTextNode(temp[zxc2]));
      tr.appendChild(td);
    }
  output.appendChild(table);
}

我就这样得到了答案