3

I have a table that has some row with same value in rows like -

<table>
    <thead>
        <tr>
            <th>Players</th>
            <th>Country</th>
            <th>ZIP Code</th>
        </tr>
    </thead>>
    <tbody>
        <tr>
            <td>Jhon Doe</td>
            <td>USA</td>
            <td>53255343</td>
        </tr>
        <tr>
            <td>Jhon Doe</td>
            <td>USA</td>
            <td>53255343</td>
        </tr>
        <tr>
            <td>Etinee Gomes</td>
            <td>Ghana</td>
            <td>566682</td>
        </tr>
    </tbody>
</table>

I need to show first row of those rows which having same value. In this case first and second row having same data, need to show only first row. If table data (<td>) not same then leave as it. Is it possible using Jquery?

4
  • Yeah it's possible. You'd want to loop through each <tr>, perhaps with a $.each() statement in jQuery, and search all the other rows for the value of each <td> to see if they match. Try writing some code yourself and come back here if you run into an issue. Commented Nov 21, 2016 at 18:04
  • This is definitely possible with jquery! You just need to iterate through each of your rows, building up an index - I suggest hashing every value together to one key - and check if this key is already in you index. If so, hide your row. Commented Nov 21, 2016 at 18:05
  • Thanks Michael. I have update my post. Can you please comment with jdfidde example.. Commented Nov 21, 2016 at 18:07
  • Thanks @ CupawnTae Commented Nov 21, 2016 at 18:17

1 Answer 1

2

You can loop through the items of your tbody and look for an if/else condition like this:

var data = []
$('tbody tr').each(function() {
  var trdat = $(this).text().trim();
  if ($.inArray(trdat, data) !== -1) {
    $(this).hide()
  } else {
    data.push(trdat);
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>Players</th>
      <th>Country</th>
      <th>ZIP Code</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Jhon Doe</td>
      <td>USA</td>
      <td>53255343</td>
    </tr>
    <tr>
      <td>Jhon Doe</td>
      <td>USA</td>
      <td>53255343</td>
    </tr>
    <tr>
      <td>Etinee Gomes</td>
      <td>Ghana</td>
      <td>566682</td>
    </tr>
  </tbody>
</table>

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

2 Comments

Hello DaniP thanx for your answer. If I want also consider some more text to be consider as hide like - here hide <td>Jhon Doe</td> and consider <td>Jhon Doe A</td> also hide, so how to this?
@JargoViet I don't get the idea can you clarify with an example on jsfiddle or codepen

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.