繁体   English   中英

Javascript:我正在尝试在模式内部创建动态表格以进行摊销

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

我正在尝试为摊销表创建动态表,但是我很想追加列。 它每次都插入行,所以我的每个值都在新行中,以及如何创建循环,以便我的下一列应为+1。 因为这是row(0),所以每次它都向上,而我的整个表都倒过来显示。

我到目前为止写

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,无法使用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), line中更改j参数。

这是上述问题的解决方案,str转换为数组

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

之后,用于创建单元格的数组<td>

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);
}

这样我得到了解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM