Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make html table columns resizable?

I have a fairly standard bootstrap-styled <table>. I want to make columns of this table resizable, e.g. by click-and-dragging right border of <th> element. I can't use any jQuery plugins as I'm using Angular and jQuery dependency is not acceptable.

I was thinking that if I could catch the click/mousemove event on the right border of <th> element, I could implement my own resizing logic (monitor horizontal mouse movement and adjust width accordingly), but I don't know how this can be done (there is no such thing as element border-related event as far as I know).

What's the best way to make columns resizable by user? Without jQuery (and preferably - in Angular 2 context).

like image 510
Titan Avatar asked Jan 16 '17 10:01

Titan


People also ask

How do you make resizable columns?

In order to allow user to resize col , we have to handle three events: mousedown on the resizer: Track the current position of mouse. mousemove on document : Calculate how far the mouse has been moved, and adjust the width of the column. mouseup on document : Remove the event handlers of document.

How do I resize a column in a table?

Select the rows or columns and then select Layout and choose your height and width. Select View > Ruler checkbox, select the cell you want, and then drag the markers on the ruler. Note: In Excel, select Home > Format, and then select Column Width.

How do you resize a table in HTML?

To manipulate the height or width of an entire table, place the size attribute (either "WIDTH=" or "HEIGHT=") within the <TABLE> code. To manipulate individual cells, place the size attribute within the code for that cell.


1 Answers

The resize property does not work with table. That's why you need to put a div inside the table th and td, then resize it.

Try the below snippet

table {    border-collapse: collapse;    border-spacing: 0px;  }  td {    border: 2px solid black;    padding: 0;    margin: 0px;    overflow: auto;  }    div {    resize: both;    overflow: auto;    width: 120px;    height: 120px;    margin: 0px;    padding: 0px;    border: 1px solid black;    display:block;    }    td div {    border: 0;    width: auto;    height: auto;    min-height: 20px;    min-width: 20px;  }
<table>    <tr>      <td><div>one</div></td>      <td><div>two</div></td>      <td><div>three</div></td>    </tr>    <tr>      <td><div>four</div></td>      <td><div>five</div></td>      <td><div>six</div></td>            </tr>    <tr>      <td><div>seven</div></td>      <td><div>eight</div></td>      <td><div>nine</div></td>           </tr>     </table>
like image 105
Jishnu V S Avatar answered Sep 19 '22 11:09

Jishnu V S