SO-67687623

by David Thomas

HTML

<div class="wrapper herotable">

  <div id="test">

    <div class="table">
      <div class="row header">
        <div class="cell sorting sort" data-sort="name">Name</div>
        <div class="cell sorting sort" data-sort="age">Age</div>
        <div class="cell sorting sort" data-sort="ocupat">Occupation</div>
        <div class="cell sorting sort" data-sort="location">Location</div>
      </div>

      <div class="table list">

        <div class="row">
          <div class="cell name" data-title="Name">Luke Peters</div>
          <div class="cell age" data-title="Age">25</div>
          <div class="cell ocupat" data-title="Occupation">Freelance Web Developer</div>
          <div class="cell location" data-title="Location">Brookline, MA</div>
        </div>

        <div class="orders">
          <div class="order">This should be an independent div-element. I would like to inject it with JavaScript when clicking on line above.<br> It should not expand the name column.</div>
        </div>

        <div class="row">
          <div class="cell name" data-title="Name">Joseph Smith</div>
          <div class="cell age" data-title="Age">27</div>
          <div class="cell ocupat" data-title="Occupation">Project Manager</div>
          <div class="cell location" data-title="Location">Somerville, MA</div>
        </div>

        <div class="orders" hidden>
          <div class="order">This should be an independent div-element. I would like to inject it with JavaScript when clicking on line above.<br> It should not expand the name column.</div>
        </div>

        <div class="row">
          <div class="cell name" data-title="Name">Maxwell Johnson</div>
          <div class="cell age" data-title="Age">26</div>
          <div class="cell ocupat" data-title="Occupation">UX Architect &amp; Designer</div>
          <div class="cell location" data-title="Location">Arlington, MA</div>
        </div>

        <div class="orders" hidden>
          <div class="order">This...

CSS

:root {
  --column1Width: 25%;
  --column2Width: 25%;
  --column3Width: 25%;
  --column4Width: 25%;
  --horizontalGap: 0.25rem;
  --verticalGap: 0.5rem;
  --fullWidth: 90vw;
}
*,
::before,
::after {
  box-sizing: border-box;
  font-size: 1rem;
  line-height: 1.5;
  margin: 0;
  padding: 0;
}

.herotable {
  margin: auto 1em;
  width: var(--fullWidth);
}

.header {
  font-weight: bold;
  border-bottom: 2px solid currentColor;
}

.table.list {
  margin-top: var(--verticalGap);
  margin-bottom: var(--verticalGap);
}

.row {
  display: flex;
  justify-content: space-between;
  margin-top: var(--verticalGap);
  margin-bottom: var(--verticalGap);
  gap: var(--verticalGap) var(--horizontalGap);
}

.row:not(.header) {
  cursor: pointer;
}


.row .cell:nth-child(1) {
  flex: 1 0 var(--column1Width);
}

.row .cell:nth-child(2) {
  flex: 1 0 var(--column2Width);
}

.row .cell:nth-child(3) {
  flex: 1 0 var(--column3Width);
}

.row .cell:nth-child(4) {
  flex: 1 0 var(--column4Width);
}

.order {
  border: 1px solid currentColor;
  border-left-color: transparent;
  border-right-color: transparent;
}

JavaScript

const rows = document.querySelectorAll('.row:not(.header)'),
  isVisible = (el) => {
    if (el) {
      return el.hidden === false && window.getComputedStyle(el, null).display !== 'none';
    }
    return false;
  },
  orderToggle = (e) => {
    const activated = e.currentTarget,
      potentialOrders = activated.nextElementSibling,
      currentVisibility = isVisible(potentialOrders);

    if (potentialOrders && potentialOrders.matches('.orders')) {
      potentialOrders.hidden = currentVisibility;
    }
  };

rows.forEach(
  (el) => {
    el.addEventListener('click', function(e) {
      orderToggle(e)
    });
  });