使用javascript显示表格

时间:2017-10-12 07:59:59

标签: javascript jquery html

我必须显示一些信息,例如:

  

姓名:myname

但我得到的是这样的:

  

姓名:
  MYNAME

我认为当我在javascript中使用insertRow时,我会创建一个新行!但我喜欢在现有表中插入新列。 有人可以帮我看一下吗?

function Donnee() {
  $.ajax({
    url: "/MyUrl",
    type: "GET",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('occurrences').each(function() {
        var row = document.getElementById("row-nom");
        var cell1 = document.getElementById("cell-nom");
        if (row === null) {
          var row = table.insertRow(3);
          row.id = "row-nom";
        }

        if (cell1 === null) {
          var cell1 = row.insertCell(0);
          cell1.id = "cell-nom";
        }

        $(this).find("occurrence").each(function() {
          datasection = $(this).attr('datasection');
          if (datas == "mytable") {
            $(this).find("data").each(function() {
              var item = $(this).find('item').text();
              var value = $(this).find('value').text();
              if (item == "NMPRES") {
                NMPRES = $(this).find('value').text();
              }
            });
            cell1.innerHTML = NMPRES;
          }
        });
      });
    }
  });
}
<html>
<table id="Table">
  <tr>
    <td>
      <div> personal info </div>
    </td>
  </tr>
  <div>
    <tr>
      <td> Name </td>
      <tr>
  </div>

</html>

1 个答案:

答案 0 :(得分:1)

不要创建新列,创建新行并转置表格。填充完所有行后,添加以下代码。

$("#Table").each(function() {
    var $this = $(this);
    var newrows = [];
    $this.find("tr").each(function(){
        var i = 0;
        $(this).find("td").each(function(){
            i++;
            if(newrows[i] === undefined) { newrows[i] = $("<tr></tr>"); }
            newrows[i].append($(this));
        });
    });
    $this.find("tr").remove();
    $.each(newrows, function(){
        $this.append(this);
    });
});