rodrigobertin
7/15/2019 - 5:31 PM

Responsive Tables

$('table').each(function () {
      let This = $(this);
      let lista = [];

      This.find('th').each(function (index) {
        console.log(index);
        let Th = $(this);
        let text = Th.text();
        lista.push(text);
      });

      This.find('tr').each(function () {
        $(this).find('td').each(function (index) {
          $(this).attr('data-label', lista[index]);
        });
    });
});
@media screen and (max-width: 600px) {

  table caption {
    font-size: 1.5em;
    margin: .5em 0 .75em;
  }

  table tr {
    background-color: #f8f8f8;
    border: 1px solid #ddd;
    padding: .39em;
  }

  table th,
  table td {
    padding: .625em;
    text-align: center;
  }

  table th {
    font-size: .85em;
    letter-spacing: .1em;
    text-transform: uppercase;
  }


  table {
    border: 0;

    caption {
      font-size: 1.3em;
    }

    thead {
      border: none;
      clip: rect(0 0 0 0);
      height: 1px;
      margin: -1px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1px;
    }

    tr {
      border-bottom: 3px solid #ddd;
      display: block;
      margin-bottom: .625em;
    }

    td {
      border-bottom: 1px solid #ddd;
      display: block;
      font-size: .8em;
      text-align: right;
    }

    td::before {

      /*
      * aria-label has no advantage, it won't be read inside a table
      content: attr(aria-label);
      */
      content: attr(data-label);
      float: left;
      font-weight: bold;
      text-transform: uppercase;
    }
  }

  table td:last-child {
    border-bottom: 0;
  }
}