簡體   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