简体   繁体   English

如何将 2 列添加到使用 JS 动态创建的 HTML 表中?

[英]How to add 2 columns to an HTML table dynamically created using JS?

The function below does check if the row of data coming in is valid and sets the 06th column to an input field, where the price will be informed.下面的函数会检查输入的数据行是否有效,并将第 06 列设置为输入字段,在此通知价格。 The data, however, has 07 "columns", but I'd need to add two more and I can't wrap my head around getting it done:然而,数据有 07 个“列”,但我需要再添加两个,我无法完成它:

1st additional column, or td would be a total field, which will get refreshed as the user informs the price.第一个附加列,或td将是一个总计字段,当用户通知价格时,它将被刷新。 I'll create a function for that later;我稍后会为此创建一个函数;

02nd additional column would be a check box. 02nd 附加列将是一个复选框。

This is what I have so far:这是我到目前为止所拥有的:

Sample Data样本数据

[
 ["Arms","Black-830011","","100 cot",1.63,273,"","178 m"],
 ["Arms2","Black-830011","","97 cot/3 span",1,267,"","176 m"]
]

Code代码

function loadItems() {
    const orderPo = document.getElementById('selectOrderPo');
    const selectedOrderPo = orderPo.options[orderPo.selectedIndex].text;

    const supplier = document.getElementById('selectSupplier');
    const selectedSupplier = supplier.options[supplier.selectedIndex].text;

    google.script.run.withSuccessHandler(function(ar) {
        if (ar == 'No items were found') {
            var div = document.getElementById('po-items');
            div.innerHTML = "There are no items for this Order PO Number and Supplier chosen above!";
            return;
        }
        if (ar && ar !== undefined && ar.length != 0) {
            var result = "<br><div class='card' id='card'><table class='table table-borderless table-hover table-vcenter' id='dtable'>" +
                "<thead style='white-space: nowrap'>" +
                "<tr>" + //Change table headings to match witht he Google Sheet
                "<th style='width: 4%' class='text-center'>Tela</th>" +
                "<th style='width: 25%' class='text-center'>Color</th>" +
                "<th style='width: 4%' class='text-center'>Pantone</th>" +
                "<th style='width: 3%' class='text-center'>Contenido</th>" +
                "<th style='width: 17%' class='text-center'>Acho(m)</th>" +
                "<th style='width: 12%' class='text-center'>Peso(gsm)</th>" +
                "<th style='width: 24%' class='text-center'>Precio/m (COP)</th>" +
                "<th style='width: 24%' class='text-center'>Metros</th>" +
                "<th style='width: 24%' class='text-center'>Precio Total sin IVA</th>" +
                "<th style='width: 24%' class='text-center'>Sel.</th>" +
                "</tr>" +
                "</thead>" +
                "<tbody>";
            console.log('Dados para a tabela: ' + JSON.stringify(ar))
            for (var i = 0; i < ar.length; i++) {
                result += "<tr>";
                for (var j = 0; j < ar[i].length; j++) {
                    (j === 6 && isNan(ar[i][4]) === false) ? "<td><input type='number' name='priceField'></td>" : "<td class='align-middle' style='word-wrap: break-word;max-width: 160px;text-align:center'>" + ar[i][j] + "</td>";
                }
                result += "</tr>";
            }
            result += "</tbody></table></div><br>";
            div = document.getElementById('po-items');
            div.innerHTML = result;
        } else {
            div = document.getElementById('po-items');
            div.innerHTML = "No items found.";
            return;
        }
    }).loadItems(selectedOrderPo, selectedSupplier);
}

Expected Result预期结果在此处输入图像描述

Thanks for any help!谢谢你的帮助!

From your added information, how about modifying your showing script as follows?根据您添加的信息,如何修改您的显示脚本如下?

From:从:

for (var i = 0; i < ar.length; i++) {
    result += "<tr>";
    for (var j = 0; j < ar[i].length; j++) {
        (j === 6 && isNan(ar[i][4]) === false) ? "<td><input type='number' name='priceField'></td>" : "<td class='align-middle' style='word-wrap: break-word;max-width: 160px;text-align:center'>" + ar[i][j] + "</td>";
    }
    result += "</tr>";
}

To:至:

for (var i = 0; i < ar.length; i++) {
  result += "<tr>";
  for (var j = 0; j < ar[i].length; j++) {
      result += (j === 6 && isNaN(ar[i][4]) === false) ? "<td><input type='number' name='priceField'></td>" : "<td class='align-middle' style='word-wrap: break-word;max-width: 160px;text-align:center'>" + ar[i][j] + "</td>";
  }
  result += `<td>0.00</td>`;
  result += `<td><input type="checkbox"></td>`;
  result += "</tr>";
}
  • isNan is isNaN . isNanisNaN
  • In your showing script, result += "<tr>" is not updated.在您的显示脚本中, result += "<tr>"未更新。 Because the values are not added in the loop.因为这些值没有添加到循环中。
  • In order to add 2 columns, I added result += 0.00 and result += <td><input type="checkbox"></td> .为了添加 2 列,我添加了result += 0.00 and result += <td><input type="checkbox"></td>
    • About 0.00 , from I'll create a function for that later;大约0.00I'll create a function for that later; , I added it as a sample value. ,我将其添加为样本值。
    • Please add the style, id and so on for your actual situation.请根据您的实际情况添加样式、id 等。

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

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