1

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

1

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Now i understand what the problem, however i slightly change your answer to my needs, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.