48

This is the only jQuery plugin I could find to resize table column widths, but it does not integrate well with my table, and has unnecessary bloat (saves in cookies). Are there any other plugins for just resizing columns? (NOT datagrid plugins, please don't suggest those).

If not, I'll write my own, it shouldn't be too hard, I just don't know how to detect when a user clicks on the td border (to resize). Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • If you look at the plugin you're linking to, it uses an additional sizing handler element to trigger resize mode. It's quite a good strategy, because it also allows to style that element with an appropriate cursor. – Frédéric Hamidi May 27 '11 at 18:27
  • Interesting. Where are you seeing this? –  May 27 '11 at 18:37
  • in the `Demo - Code examples` tab, you can see a dark vertical line to the right of the column headers. That element is added by the plugin and serves as a sizing handler. You can use a tool like [Firebug](http://getfirebug.com/) to learn more about it. – Frédéric Hamidi May 27 '11 at 18:41

9 Answers9

117

:-) Once I found myself in the same situation, there was no jQuery plugin matching my requeriments, so I spend some time developing my own one: colResizable
 

About colResizable

colResizable is a free jQuery plugin to resize table columns dragging them manually. It is compatible with both mouse and touch devices and has some nice features such as layout persistence after page refresh or postback. It works with both percentage and pixel-based table layouts.

It is tiny in size (colResizable 1.0 is only 2kb) and it is fully compatible with all major browsers (IE7+, Firefox, Chrome and Opera).

enter image description here

Features

colResizable was developed since no other similar plugin with the below listed features was found:

  • Compatible with mouse and touch devices (PC, tablets, and mobile phones)
  • Compatibility with both percentage and pixel-based table layouts
  • Column resizing not altering total table width (optional)
  • No external resources needed (such as images or stylesheets)
  • Optional layout persistence after page refresh or postback
  • Customization of column anchors
  • Small footprint
  • Cross-browser compatibility (IE7+, Chrome, Safari, Firefox)
  • Events

Comparison with other plugins

colResizable is the most polished plugin to resize table columns out there. It has plenty of customization possibilities and it is also compatible with touch devices. But probably the most interesting feature which is making colResizable the greatest choice is that it is compatible with both pixel-based and fluid percentage table layouts. But, what does it mean?

If the width of a table is set to, lets say 90%, and the column widths are modified using colResizable, when the browser is resized columns widths are resized proportionally. While other plugins does behave odd, colResizable does its job just as expected.

colResizable is also compatible with table max-width attribute: if the sum of all columns exceed the table's max-width, they are automatically fixed and updated.

Another great advantage compared with other plugins is that it is compatible with page refresh, postbacks and even partial postbacks if the table is located inside of an updatePanel. It is compatible with all major browsers (IE7+, Firefox, Chrome and Opera), while other plugins fail with old IE versions.

See samples and JSFiddle.

Code sample

$("#sample").colResizable({
        liveDrag:true
});
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Alvaro Prieto
  • 2,107
  • 2
  • 13
  • 12
  • Great job, but is there a way when we apply to table to prevent plugin from changing table's th/td/tr default padding and width. – jasin_89 Jan 15 '12 at 21:07
  • there was an issue with that so horizontal padding in cells is not allowed. There is no limitation with column's default width as far as I know :-) – Alvaro Prieto Jan 18 '12 at 17:54
  • yep, there is no limitation with width, and I discover that there is only need to be setted padding for sides, if I delete them from plugin where they are marked with !Important and manually set it, it will work. – jasin_89 Jan 20 '12 at 07:22
  • yes but side padding is removed for an important reason (there was a strange behaviour when you resize to 0px a cell with padding). So if you remove that !important rule, unexected results will appear in some browsers. However, it is up to you :-) – Alvaro Prieto Jan 23 '12 at 18:11
  • Hi dude. Great work, can you tell me what do I need to do to make the cell content reflowable, that is, as the column shrinks or expands, the content will reflow, thus changing that row's height accordingly and simultaneously.. – SexyBeast Jan 26 '13 at 11:06
  • 7
    I tried this, but it doesn't currently work with jquery 1.9.1 (or latest). 'TypeError: a.browser is undefined' – iPadDeveloper2011 Mar 21 '13 at 01:09
  • Brilliant. Works as advertised thus far. – R2-Bacca Apr 05 '13 at 14:43
  • In order to make it work with jquery 1.9.1, use the jquery migrate : https://github.com/jquery/jquery-migrate – ZooZ Aug 26 '13 at 05:01
  • it seems this fork has this fixed (along with a few other fixes): https://github.com/futska/colResizable – tutuDajuju Oct 31 '13 at 08:36
  • This plugin does not work in the JavaFX WebKit implementation (JQuery itself is loaded and working properly). – erdal.karaca Jan 17 '14 at 09:48
  • quick fix: replace "t=a.browser.msie," with "t=false," in min-version, or var "ie = $.browser.msie;" with "var ie =false;" – FtLie May 23 '14 at 14:02
  • 1
    Hi @AlvaroPrieto could you please tell me how an i going to use this plugin if I have predefined width for each column and then set the table according when I load it at first. – Hmahwish Feb 02 '15 at 04:47
  • Unfortunately, this plugin doesn't work if table-layout isn't fixed. [Reported issue](https://github.com/alvaro-prieto/colResizable/issues/38) – Matt Sep 30 '15 at 12:43
  • @AlvaroPrieto I am using this plugin in angular project.. I am facing issue-- when page loads this works but after any click or if I switch page. Again if i will check its not working.. – Javascript Coder Jan 21 '16 at 05:11
  • 1
    this is a great plugin – techie_28 Jun 20 '16 at 10:31
  • Is there a way to "destroy and rebuild" or to "reset" the object? In a situation with sortable/draggable table columns this would be great. – insertusernamehere Nov 12 '16 at 13:36
  • This is really a great plugin. easily configure with my JQuery Datatable plugin. Thanks – Shrikant Dandile Jul 23 '20 at 05:48
  • Is there anyway to make this plugin work if the content overflows the width? – Ricardo Figueiredo Oct 14 '20 at 13:46
  • colResizable doesn't seem to support Right-to-Left (dir="rtl") – Cesar Dec 15 '20 at 11:55
  • I tried with Bootstrap-table but initial resize not working but after second time only its working. [jsfiddle](https://jsfiddle.net/jsjqfinder/y3bjx0u5/40/) – WpFinder Aug 12 '22 at 04:20
59

So I started writing my own, just bare bones functionality for now, will be working on it next week... http://jsfiddle.net/ydTCZ/

9

Here's a short complete html example. See demo http://jsfiddle.net/CU585/

<!DOCTYPE html><html><head><title>resizable columns</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>
<style>
th {border: 1px solid black;}
table{border-collapse: collapse;}
.ui-icon, .ui-widget-content .ui-icon {background-image: none;}
</style>
<body>
<table>
<tr><th>head 1</th><th>head 2</th></tr><tr><td>a1</td><td>b1</td></tr></table><script>
$( "th" ).resizable();
</script></body></html>
mikeyreilly
  • 6,523
  • 1
  • 26
  • 21
  • Note that I'm assuming that the purpose of the question is to figure out how to easily make table columns resizable. It is not necessary to write a plugin to accomplish this task. You can do so in jquery simply by $( "selector" ).resizable() where "selector" is your selector for finding the of the column you wish to make resizable. By default, jquery adds little handles to elements you make resizable: I have set the style so that these are removed. – mikeyreilly Jul 17 '14 at 13:40
  • I just installed the latest version of Firefox and it worked for me. I suspect that either you have an older version of Firefox or you have a Firefox add-on which is preventing it from working. – mikeyreilly Jul 17 '15 at 08:36
  • It is too buggy when applied to tables. – happy_marmoset Sep 01 '16 at 11:04
5

try flexigrid, here is One more Reference

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • 1
    I appreciate the help, but the table I am using is very specific and a datagrid plugin like flexigrid would be extremely difficult to use. –  May 27 '11 at 18:35
  • 1
    I've had the same problem with flexigrid. For what it does, it's great, but there's no obvious way to strip it back to just the fucntionality you want. – daveyfaherty Aug 15 '11 at 10:09
  • flexigrid is a broken link – Flimm Jul 16 '15 at 17:01
3

Or try my solution: http://robau.wordpress.com/2011/08/16/unobtrusive-table-column-resize-with-jquery-as-plugin/ :)

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
  • 1
    Can you quote the relevant part, please, so that the answer is usabla even if / when the linked page no longer works? – Suma Feb 29 '20 at 19:20
2

Although very late, hope it still helps someone:

Many of comments here and in other posts are concerned about setting initial size.

I used jqueryUi.Resizable. Initial widths shall be defined within each "< td >" tag at first line (< TR >). This is unlike what colResizable recommends; colResizable prohibits defining widths at first line, there I had to define widths in "< col>" tag which wasn't consikstent with jqueryresizable.

the following snippet is very neat and easier to read than usual samples:

$("#Content td").resizable({
    handles: "e, s",
    resize: function (event, ui) {
        var sizerID = "#" + $(event.target).attr("id");
        $(sizerID).width(ui.size.width);
    }
});

Content is id of my table. Handles (e, s) define in which directions the plugin can change the size. You must have a link to css of jquery-ui, otherwise it won't work.

  • 1
    It is too buggy when applied to tables. – happy_marmoset Sep 01 '16 at 11:04
  • All the `td`s must have an id? Unusable – edc65 Mar 30 '17 at 09:04
  • My code had id for each td by default. You may be able to remove that resize function. Now after this while I see that I had used that function for a special purpose. Only defining Handles may be enough. – Alireza Ahmadi Rad Apr 01 '17 at 12:12
  • Could you give a reference for the fact that colResizable prohibits defining widths at first line? – user3245268 Feb 26 '19 at 16:44
  • I'm afraid not, sorry, I had shifted to the other plugin at the same time. for a while I was comparing them, but no more documentation or evidence about colResizable. – Alireza Ahmadi Rad Feb 27 '19 at 18:12
  • Unfortunately `jQuery.resizable` does not work well when the table width is fixed or controlled from outside, like when the table has `width=100%`. In such situation resizing kind of works, but does not match mouse movement. – Suma Mar 01 '20 at 17:19
1

I tried to add to @user686605's work:

1) changed the cursor to col-resize at the th border

2) fixed the highlight text issue when resizing

I partially succeeded at both. Maybe someone who is better at CSS can help move this forward?

http://jsfiddle.net/telefonica/L2f7F/4/

HTML

<!--Click on th and drag...-->
<table>
    <thead>
        <tr>
            <th><div class="noCrsr">th 1</div></th>
            <th><div class="noCrsr">th 2</div></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>td 1</td>
            <td>td 2</td>
        </tr>
    </tbody>
</table>

JS

$(function() {
    var pressed = false;
    var start = undefined;
    var startX, startWidth;

    $("table th").mousedown(function(e) {
        start = $(this);
        pressed = true;
        startX = e.pageX;
        startWidth = $(this).width();
        $(start).addClass("resizing");
        $(start).addClass("noSelect");
    });

    $(document).mousemove(function(e) {
        if(pressed) {
            $(start).width(startWidth+(e.pageX-startX));
        }
    });

    $(document).mouseup(function() {
        if(pressed) {
            $(start).removeClass("resizing");
            $(start).removeClass("noSelect");
            pressed = false;
        }
    });
});

CSS

table {
    border-width: 1px;
    border-style: solid;
    border-color: black;
    border-collapse: collapse;
}

table td {
    border-width: 1px;
    border-style: solid;
    border-color: black;
}

table th {
    border: 1px;
    border-style: solid;
    border-color: black;
    background-color: green;
    cursor: col-resize;
}

table th.resizing {
    cursor: col-resize;
}

.noCrsr {
    cursor: default;
    margin-right: +5px;
}

.noSelect {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
Adam
  • 3,142
  • 4
  • 29
  • 48
1

I have tried several solutions today, the one working best for me is Jo-Geek/jQuery-ResizableColumns. Is is very simple, yet it handles tables placed in flex containers, which many of the other ones fail with.

<table class="resizable">
</table>
$('table.resizable').resizableColumns();

$(function() {
  $('table.resizable').resizableColumns();
})
body {
  margin: 0px;
}

table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://jo-geek.github.io/jQuery-ResizableColumns/demo/resizableColumns.min.js"></script>
<table class="resizable" border="true">
  <thead>
  <tr>
    <th>col 1</th><th>col 2</th><th>col 3</th><th>col 4</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td>
    </tr>
    <tr>
      <td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td>
    </tr>
  </tbody>
</table>
Suma
  • 33,181
  • 16
  • 123
  • 191
0

I've done my own jquery ui widget, just thinking if it's good enough.

https://github.com/OnekO/colresizable

OnekO
  • 41
  • 1
  • 8