Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Table Row Show/Hide based on row

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?

like image 779
Daniel Smith Avatar asked May 31 '26 08:05

Daniel Smith


1 Answers

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>
like image 185
DaniP Avatar answered Jun 02 '26 22:06

DaniP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!