简体   繁体   English

无法在我的 javascript 生成的 materializecss 表中加载复选框

[英]Can't get a checkbox to load in my javascript generated materializecss table

I am building a dynamic table for my Google Apps Script web app.我正在为我的 Google Apps Script Web 应用程序构建一个动态表。 I am using the materializecss table for my styling.我正在使用 materializecss 表进行造型。 The table generates but I can't seem to get a checkbox to load in the last column.该表生成,但我似乎无法在最后一列中加载复选框。

Here is my HTML code:这是我的 HTML 代码:

                <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
                <table id="myTable" class="highlight">
                <tr class="header">
                
                <th>First Name</th>
                <th>Surname</th>
                <th>Current Housepoints</th>
                <th>Award Housepoints</th>
                <th>Behaviour</th>
                </tr>
                <tbody id = "table-body">
                
                
                
                </tbody>
                </table>

and my javascript code to generate the table:和我的 javascript 代码来生成表:

function displayPupils(dataArray) {

    var tbody = document.getElementById("table-body");

    dataArray.forEach(function(r) {
    var pupilID = r[0];
    pupilIds.push(pupilID);
    var row = document.createElement("tr");
     
    var col2 = document.createElement("td");
    col2.textContent = r[1];
    var col3 = document.createElement("td");
    col3.textContent = r[2];
    var col4 = document.createElement("td");
    col4.textContent = r[3];
    var col5 = document.createElement("td");
    var y = document.createElement("div");
    y.setAttribute("class", "input-field col s2");
    var x = document.createElement("INPUT");
    x.setAttribute("type", "number");
    
    x.setAttribute("class", "validate");
    x.setAttribute("id", pupilID);
    x.setAttribute("value", "Assign");
    


    var col6 = document.createElement("td");
    
    var checkBox1 = document.createElement("INPUT");
    checkBox1.setAttribute("id", pupilID+"Op1");
    checkBox1.type = "checkbox";
    checkBox1.setAttribute("class","browser-default");
    checkBox1.setAttribute("checked", "checked");
    
    col6.appendChild(checkBox1);
    col5.appendChild(y);
    y.appendChild(x);
   
    row.appendChild(col2);
    row.appendChild(col3);
    row.appendChild(col4);
    row.appendChild(col5);
    row.appendChild(col6);
    tbody.appendChild(row);
    
})

}

According to MaterializeCSS docs https://materializecss.com/checkboxes.html you're missing a span tag beside the checkbox and a label tag to wrap them all, see the snippet:根据 MaterializeCSS 文档https://materializecss.com/checkboxes.html你在复选框旁边缺少一个span标签和一个label标签来包装它们,请参阅片段:

 function displayPupils(dataArray) { var tbody = document.getElementById("table-body"); dataArray.forEach(function(r) { var pupilID = r[0]; pupilIds.push(pupilID); var row = document.createElement("tr"); var col2 = document.createElement("td"); col2.textContent = r[1]; var col3 = document.createElement("td"); col3.textContent = r[2]; var col4 = document.createElement("td"); col4.textContent = r[3]; var col5 = document.createElement("td"); var y = document.createElement("div"); y.setAttribute("class", "input-field col s2"); var x = document.createElement("INPUT"); x.setAttribute("type", "number"); x.setAttribute("class", "validate"); x.setAttribute("id", pupilID); x.setAttribute("value", "Assign"); var col6 = document.createElement("td"); var checkBox1 = document.createElement("INPUT"); checkBox1.setAttribute("id", pupilID+"Op1"); checkBox1.type = "checkbox"; checkBox1.setAttribute("class","browser-default"); checkBox1.setAttribute("checked", "checked"); // Add a label and a span var label = document.createElement('label'); label.append(checkBox1); label.append(document.createElement('span')); col6.appendChild(label); col5.appendChild(y); y.appendChild(x); row.appendChild(col2); row.appendChild(col3); row.appendChild(col4); row.appendChild(col5); row.appendChild(col6); tbody.appendChild(row); }) } const pupilIds = [] displayPupils([ [1, 'John', 'Doe', '8'] ])
 <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet"/> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."> <table id="myTable" class="highlight"> <tr class="header"> <th>First Name</th> <th>Surname</th> <th>Current Housepoints</th> <th>Award Housepoints</th> <th>Behaviour</th> </tr> <tbody id = "table-body"> </tbody> </table>

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

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