Denis
Denis

Reputation: 296

jQuery UI resizable - resize table columns size with overflow

I have a table with resizable columns implemented with jQuery UI Resizable. All working fine, except i can't change columns size narrowed than content. I can't use table-layout rule because after that rule table can't wider than window size. How to solve this?

$(function () {
  $("table th").resizable({
    minWidth: 100,
    resize: function (event, ui) {
      const sizerID = "#" + $(event.target).attr("id");
      const { width } = ui.size;
      $(sizerID).children("span").width(width);
    }
  });
});
.table-holder {
  overflow: auto;
}

.table thead span {
  display: block;
}
.table tbody tr td {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>



<div class="table-holder">
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th id="Company"><span>Company</span></th>
        <th id="Contact"><span>Contact</span></th>
        <th id="Country"><span>Country</span></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </tbody>
  </table>
</div>

Codepen demo https://codepen.io/DenisDov/pen/WNwodEe

Upvotes: 1

Views: 2938

Answers (1)

Twisty
Twisty

Reputation: 30893

As explained in Why does overflow:hidden not work in a <td>? the issue is not with Resizing, but with the nature of TD elements. They inherit Table-Cell properties and not Block properties so you can not re-flow the content in the same way.

As suggested be a few in the thread, you can wrap you cell content with a DIV element and overflow that. Here is an example with your code.

$(function() {
  $(".table th").resizable({
    minWidth: 100
  });
});
.table-holder {
  overflow: auto;
}

.table thead span {
  display: block;
  width: 100%;
}

.table tbody tr td div {
  display: inline-block;
  white-space: nowrap;
  position: relative;
  /* must be relative */
  width: 100%;
  /* fit to table cell width */
  margin-right: -1000px;
  /* technically this is a less than zero width object */
  overflow: hidden;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>



<div class="table-holder">
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th id="Company"><span>Company</span></th>
        <th id="Contact"><span>Contact</span></th>
        <th id="Country"><span>Country</span></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <div>Alfreds Futterkiste</div>
        </td>
        <td>
          <div>Maria Anders</div>
        </td>
        <td>
          <div>Germany</div>
        </td>
      </tr>
      <tr>
        <td>
          <div>Centro comercial Moctezuma</div>
        </td>
        <td>
          <div>Francisco Chang</div>
        </td>
        <td>
          <div>Mexico</div>
        </td>
      </tr>
      <tr>
        <td>
          <div>Ernst Handel</div>
        </td>
        <td>
          <div>Roland Mendel</div>
        </td>
        <td>
          <div>Austria</div>
        </td>
      </tr>
      <tr>
        <td>
          <div>Island Trading</div>
        </td>
        <td>
          <div>Helen Bennett</div>
        </td>
        <td>
          <div>UK</div>
        </td>
      </tr>
      <tr>
        <td>
          <div>Laughing Bacchus Winecellars</div>
        </td>
        <td>
          <div>Yoshi Tannamuri</div>
        </td>
        <td>
          <div>Canada</div>
        </td>
      </tr>
      <tr>
        <td>
          <div>Magazzini Alimentari Riuniti</div>
        </td>
        <td>
          <div>Giovanni Rovelli</div>
        </td>
        <td>
          <div>Italy</div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

Upvotes: 1

Related Questions