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).
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With