0

I have a table and i'm trying to delete rows if any values in the first column are identical. how could i achieve this using javascript or jquery?

         
  $("table tr").each(function () {
  var tdText = $(this).text();

  $("table tr")
      .filter(function () {
          return tdText == $(this).text();
      })
      .not(":first")
      .remove();

  });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
      <tr>
      <th>Product Code</th>
      <th>Item Name</th>
      <th>Long Description</th>
      <th>Material</th>
      <th>Style</th>
      </tr>
  </thead>
  <tbody>
        <tr>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          </tr>
        <tr>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          <td><input></td>
        </tr>
            <tr>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          <td><input></td>
          <td><input></td>
        </tr>
      </tbody>
    </table>

3
  • 3
    What have you tried so far? Commented Jun 22, 2018 at 19:56
  • i've added a function i've tried to use for this purpose Commented Jun 22, 2018 at 20:12
  • I'm assuming you are trying to use input boxes and check the entered text? If so you want to call .val() on the input not .text() on the row. See my answer and let me know if I misinterpreted Commented Jun 22, 2018 at 20:15

2 Answers 2

3

function removeDuplicates()  {
  var tableRows = $('#myTable').find("tbody tr");
  var seens = [];
  for(var i = 0; i < tableRows.length; i++) {
    var tRow = $(tableRows[i]);
    var cell1Content = tRow.find("td:first-child input").val();
    if(seens.indexOf(cell1Content) != -1) {
      tRow.remove();
    } else {
      seens.push(cell1Content);
    }
  }
}

function addRow()  {
  $('#myTable').find("tbody").append(['<tr>',
                                      '    <td><input></td>',
                                      '    <td><input></td>',
                                      '    <td><input></td>',
                                      '    <td><input></td>',
                                      '    <td><input></td>',
                                      '  </tr>'].join(""));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <thead>
      <tr>
      <th>Product Code</th>
      <th>Item Name</th>
      <th>Long Description</th>
      <th>Material</th>
      <th>Style</th>
      </tr>
  </thead>
  <tbody>
    <tr>
      <td><input value="1"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      </tr>
    <tr>
      <td><input value="3"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
    </tr>
     <tr>
      <td><input value="1"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
    </tr>
     <tr>
      <td><input value="2"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
    </tr>
     <tr>
      <td><input value="3"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
    </tr>
     <tr>
      <td><input value="4"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
    </tr>
     <tr>
      <td><input value="5"></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
      <td><input></td>
    </tr>
  </tbody>
</table>
<div>
  <button onclick="addRow()">Add Row</button>
  <button onclick="removeDuplicates()">Remove Duplicates</button>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You'll have to iterate through the rows and then remove the duplicates for every row you go through.

$('table').find('tr').each(function(){
  const $row = $(this);
  $row.nextAll('tr').each(function(){
    const $next = $(this);
    if($row.find('td:first').text() == $next.find('td:first').text()) {
      $next.remove();
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>Product Code</th>
      <th>Item Name</th>
      <th>Long Description</th>
      <th>Material</th>
      <th>Style</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
      <td>D</td>
      <td>E</td>
      <td>F</td>
    </tr>
    <tr>
      <td>A</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
    <tr>
      <td>B</td>
      <td>B</td>
      <td>B</td>
      <td>B</td>
      <td>B</td>
      <td>B</td>
    </tr>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
      <td>D</td>
      <td>E</td>
      <td>F</td>
    </tr>
    <tr>
      <td>B</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
    <tr>
      <td>C</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.