Gaspa79
Gaspa79

Reputation: 5689

Remove a table row but keep the CSS alternate rows format

So I have a table that has an alternating format for rows (let's say grey and white). I'm deleting a row with the tr.remove() jquery function, and of course the css of the rows over/under it will not change, and they will keep their odd/even styles. Basically:

Before Deletion         After Deletion

grey                    grey
white                   white
grey                    white
white                   grey
grey

Is there any way to quickly force the table to "refresh" the styles? Or do I have to apply them manually using jquery?

Thanks a lot!

Upvotes: 2

Views: 526

Answers (2)

Penny Liu
Penny Liu

Reputation: 17438

Just utilize :nth-child(even of :not([hidden])). This selector ensures that striping is consistently updated across the entire table.

const hideBtns = document.querySelectorAll("[data-hide-btn]")

hideBtns.forEach(btn => {
  btn.addEventListener("click", () => {
    btn.closest("tr").classList.add("hidden")
  })
})
table {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 80%;
}

table td,
table th {
  border: 1px solid #ddd;
  padding: 8px;
}

table tr:nth-child(even of :not(.hidden)) {
  background-color: lightgray;
}

table th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #04AA6D;
  color: white;
}

tr.hidden {
  display: none;
}
<table>
  <thead>
    <tr>
      <th>Company</th>
      <th>Contact</th>
      <th></th>
    </tr>
  </thead>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
  <tr>
    <td>Königlich Essen</td>
    <td>Philip Cramer</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td><button data-hide-btn>
        Hide
      </button></td>
  </tr>
</table>

Upvotes: 0

epascarello
epascarello

Reputation: 207521

So do the styling with CSS instead of using odd/even classes/ hardcoding values.

document.querySelector("tbody").addEventListener("click", function(e) {
    var td = e.target,
        tr = td.parentNode;
        tr.parentNode.removeChild(tr);
    
});
    tr:nth-child(even) {
      background-color: #FFF;
    }
    
    tr:nth-child(odd) {
      background-color: #CCC;
    }
<table>
  <tbody>
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
    <tr><td>4</td></tr>
    <tr><td>5</td></tr>
    <tr><td>6</td></tr>
  </tbody>
</table>

Upvotes: 5

Related Questions