Search code examples
jqueryjquery-uijquery-ui-resizable

Table column resize going wrong


I tried to impelement the jQuery-UI plugin to resize a table column via Drag and Drop.

I got this:

$("#table,#table table tr th,#table table tr td").resizable({
     handles: 'e'
});

The dragging is working and it is changing the size, but in the beginning then I click between the 2 table columns and move the mouse a bit, the column width gets much bigger

I dont know how to solve this problem ... Any ideas? Thanks in advance


Solution

  • Based on your example code, consider the following example.

    $(function() {
      $("table").resizable();
      $("table thead th").resizable({
        handles: 'e'
      });
    });
    table {
      border-collapse: collapse;
    }
    
    table,
    th,
    td {
      border: 1px solid black;
    }
    
    table th {
      background: #CCC;
      width: 120px;
      min-width: 2em;
    }
    
    table thead tr .ui-resizable-e {
      border: 1px solid black;
      width: 2px;
      height: 17px;
      background: #666;
      top: 1px;
      right: -2px;
    }
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    <table>
      <thead>
        <tr>
          <th>H1</th>
          <th>H2</th>
          <th>H3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>A1</td>
          <td>B1</td>
          <td>C1</td>
        </tr>
        <tr>
          <td>A2</td>
          <td>B2</td>
          <td>C2</td>
        </tr>
        <tr>
          <td>A3</td>
          <td>B3</td>
          <td>C3</td>
        </tr>
      </tbody>
    </table>

    This allows the user to resize the header, and by proxy of the table structure, the columns are sized.

    Hope this helps.