无法通过循环遍历Javascript数组将新列附加到现有表

时间:2016-05-29 08:43:18

标签: javascript jquery html

我有两个表,我将行从一个表复制到另一个表。复制时我想从列中的一个列中的下拉列表中只复制选定的值以及行。我能够做到这一点但是第一个值在追加时从数组中丢失。

这是我的代码



$("#addrecords").click(function () {
  alert("clicked");
  var found = false;
  var jsonObj = []; //creating array to push all values which are selected from each drop down.
  var count=0; //initializing count to iterate later to create dynamic column and append at last

  $('#dataTable').find('tr').each(function () {
    var row = $(this);
    if (row.find('input[type="checkbox"]').is(':checked')) {
      found = true;

      var row = $(this).closest('tr').html(); //get checked row
      var dropdownVal= $(this).find("td:eq(5)").find('option:selected').val();   //get the selected val from dropdown

      jsonObj.push(dropdownVal);   //pushing into array

      $('#dynamictable tbody').append('<tr>' + row + '</tr>'); //appending copied row from first table to second table
    }
    else {
      //alert "NOthing found"
      //found=false;
    };
  });
  if (!found) {
    alert("Nothing Found");
  }
  else {
    //alert("Found");// Or you can omit the else part
    //alert("val---" + values);
  }


  $('#dynamictable tr').find('td:last-child').remove(); // removing last column td in second table 

  $('#dynamictable tbody').each(function(count){

    $(this).find('td').eq(4).after(jsonObj[count]); //trying to append new column to table by iterating through array jsonObj loop

  });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" style="width:50%" class="table table-striped" id="dataTable">
  <thead>
    <tr>
      <th>Select</th>
      <th>FR_ID</th>
      <th>Tag Name</th>
      <th>FR_Source</th>
      <th>FR_Destination</th>
      <th>Reusability</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" /></td>
      <td>1</td>
      <td>Molex</td>
      <td>ITSM</td>
      <td>Service Now</td>
      <td><Select>
        <option value="Completely">Completely</option>
        <option value="Partially">Partially</option>
        </Select>
      </td>
    </tr>
    <tr>
      <td><input type="checkbox" /></td>
      <td>14</td>
      <td>bpm,xml,snow,srms</td>
      <td>itsm</td>
      <td>b2b</td>
      <td><Select>
        <option value="Completely">Completely</option>
        <option value="Partially">Partially</option>
        </Select>
      </td>
    </tr>
  </tbody>
</table>
<div class="small-12 columns">
  <div>
    <input type="submit" value="Next" id="addrecords" style="font-size: 10px" />
  </div>
</div>
<br />
<table border="1" style="width:50%" class="table table-striped" id="dynamictable">
  <thead>
    <tr>
      <th>Select</th>
      <th>FR_ID</th>
      <th>FR_Client</th>
      <th>FR_Source</th>
      <th>FR_Destination</th>
      <th>Reusability</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>
&#13;
&#13;
&#13;

下图显示了我能够获得的结果。请建议我这里缺少的更改,以获得正确的值。感谢您在此处提出的任何修改/建议

enter image description here

1 个答案:

答案 0 :(得分:1)

使用$('#dynamictable tbody tr')代替$('#dynamictable tbody')。 您只需添加html(),而不是删除最后一列。 同时将jsonObj初始化为全局变量。

var jsonObj = []; //creating array to push all values which are selected from each drop down.
$("#addrecords").click(function() {
  alert("clicked");
  var found = false;
  var count = 0; //initializing count to iterate later to create dynamic column and append at last

  $('#dataTable').find('tr').each(function() {
    var row = $(this);
    if (row.find('input[type="checkbox"]').is(':checked')) {
      found = true;
      var row = $(this).closest('tr').html(); //get checked row
      var dropdownVal = $(this).find("td:eq(5)").find('option:selected').val(); //get the selected val from dropdown
      jsonObj.push(dropdownVal); //pushing into array
      $('#dynamictable tbody').append('<tr>' + row + '</tr>'); //appending copied row from first table to second table                  
    } else {
      //alert "NOthing found"
      //found=false;
    };
  });
  if (!found) {
    alert("Nothing Found");
  } else {
    //alert("Found");// Or you can omit the else part
    //alert("val---" + values);
  }


  $('#dynamictable tr').find('td:last-child').empty(); // removing last column td in second table 
  $('#dynamictable tbody  tr').each(function(count) {
    console.log(jsonObj)
    $(this).find('td:last-child').text(jsonObj[count]); //trying to append new column to table by iterating through array jsonObj loop

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" style="width:50%" class="table table-striped" id="dataTable">
  <thead>
    <tr>
      <th>Select</th>
      <th>FR_ID</th>
      <th>Tag Name</th>
      <th>FR_Source</th>
      <th>FR_Destination</th>
      <th>Reusability</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="checkbox" />
      </td>
      <td>1</td>
      <td>Molex</td>
      <td>ITSM</td>
      <td>Service Now</td>
      <td>
        <Select>
          <option value="Completely">Completely</option>
          <option value="Partially">Partially</option>
        </Select>
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" />
      </td>
      <td>14</td>
      <td>bpm,xml,snow,srms</td>
      <td>itsm</td>
      <td>b2b</td>
      <td>
        <Select>
          <option value="Completely">Completely</option>
          <option value="Partially">Partially</option>
        </Select>
      </td>
    </tr>
  </tbody>
</table>
<div class="small-12 columns">
  <div>
    <input type="submit" value="Next" id="addrecords" style="font-size: 10px" />
  </div>
</div>
<br />
<table border="1" style="width:50%" class="table table-striped" id="dynamictable">
  <thead>
    <tr>
      <th>Select</th>
      <th>FR_ID</th>
      <th>FR_Client</th>
      <th>FR_Source</th>
      <th>FR_Destination</th>
      <th>Reusability</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>