简体   繁体   English

动态创建表时如何创建ID的元素

[英]How to create element of ID's when create a table dynamically

Hi guy's i have a problem with creating an element of id's while create a table dynamically. 嗨,大家好,我在动态创建表格时创建id元素存在问题。 i want make the id's different from one column to the another. 我想使ID从一列到另一列有所不同。 i realize that i must use an array for doing that. 我意识到我必须使用一个数组来做到这一点。 but i don't know where i must to place that array for the id's. 但我不知道我必须在哪里放置ID的数组。 i want to create the id's of <input type='text' id='input[J]' <<< just for example 我想创建<input type='text' id='input[J]' <<< <input type='text' id='input[J]' ,例如

this is my code for create the table 这是我创建表的代码

$(document).ready(function() {
  $('#submit-file').on("click", function(e) {
    if ($('#files').val() == "") {
      alert("Anda Harus Memasukkan File Terlebih Dahulu");
    } else {
      e.preventDefault();

      $('#files').parse({
        config: {
          delimiter: "",
          skipEmptyLines: false,
          complete: displayHTMLTable,
        },
        before: function(file, inputElem) {
          //console.log("Parsing file...", file);
        },
        error: function(err, file) {
          //console.log("ERROR:", err, file);
        },
        complete: function() {
          //console.log("Done with all files");
        }
      });
    }
  });

  function displayHTMLTable(results) {
    var table = "<table id='table_data' class='table table-bordered' width='90%'>";
    var data = results.data;
    var size = -1;
    var check1 = 1;
    var check2 = 100;
    table += "<tbody>";
    for (i = 0; i < data.length; i++) {
      table += "<tr>";
      var row = data[i];
      var cells = row.join(";").split(";");
      if (cells.length < size) continue;
      else if (cells.length > size) size = cells.length;
      if (cells.length < check1 || cells.length > check2) {
        alert('Data Yang Anda Masukkan Salah');
        return false;
      } else {
        for (j = 0; j < cells.length; j++) {

          table += "<td>";
          table += cells[j];
          table += "</td>";
        }

        table += "<td><input type='text' name='input1' id='input1' size='2'></td>";
        table += "<td><input type='text' name='input2' id='input2' size='2'></td>";
        table += "<td><input type='text' name='input3' id='input3' size='7'></td>";
        table += "</tr>";

      }
    }
    table += "</tbody>";
    table += "</table>";
    $("#parsed_csv_list").html(table);

  }
});

If you use the backtick ``(above the tab) then you can use the ${} syntax to gain access to a variable within a string. 如果使用反引号``(在选项卡上方),则可以使用$ {}语法来访问字符串中的变量。 Below is your code with a count variable that is initialized to 1 and then incremented after each use. 以下是您的代码,其中的count变量初始化为1,然后在每次使用后递增。 It can be accessed in any string like this: `here is a string ${count}` 可以使用任何这样的字符串来访问它:`这里是一个字符串$ {count}`

  for (i = 0; i < data.length; i++) {
    table += "<tr>";
    var row = data[i];
    var cells = row.join(";").split(";");
    var count = 1;
    if (cells.length < size) continue;
    else if (cells.length > size) size = cells.length;
    if (cells.length < check1 || cells.length > check2) {
      alert('Data Yang Anda Masukkan Salah');
      return false;
    } else {
    for (j = 0; j < cells.length; j++) {
      table += "<td>";
      table += cells[j];
      table += "</td>";
    }

    table += `<td><input type='text' name='input${count}' id='input${count}' size='2'></td>`;
    count++;
    table += `<td><input type='text' name='input${count}' id='input${count}' size='2'></td>`;
    count++;
    table += `<td><input type='text' name='input${count}' id='input${count}' size='7'></td>`;
    table += "</tr>";

  }
}

If you want to do this in a loop then it's even easier. 如果您想循环执行此操作,则更加简单。 Inside the loop that you're using to create the table make another loop and create each td in that loop: 在用于创建表的循环内,进行另一个循环并在该循环中创建每个td

for (var j = 1; j <= 3; j++) {
  table += `<td><input type='text' name='input${j}' id='input${j}' size='2'></td>`;
}

So your table code could use a loop and end up looking like this (I'm not dealing with your data. Just showing you how to make your ids work): 因此,您的表代码可能会使用一个循环,最终看起来像这样(我不处理您的数据。只是向您展示如何使ids工作):

for (i = 0; i < data.length; i++) {
    table += "<tr>"; 
    for (var j = 1; j <= 3; j++) {
        table += `<td><input type='text' name='input${j}' id='input${j}' size='2'></td>`;
      }   
   }
   table += "</tr>";
}

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

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