如何在 table header 中使用可调整大小的 jquery 以及如何在调整 table header 大小时扩展 parent div?

how to use jquery resizable in table headers and how to extend parent div when I resize the table header?

这里有一个示例代码,请使用 jquery resizable() 扩展 table header,而不改变 div 的高度和宽度。提前致谢。

<!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>
<div style="overflow: scroll; height: 288px; width : 288px;">
<table>
<tr><th>head 1</th><th>head 2</th></tr><tr><td>a1ysgefjdsgjfhghfgggfhdgghfdhfghsadhg</td><td>b1dsfghjkhgcjg</td></tr></table><script>
$( "th" ).resizable();
</script></div></body></html>

答案如下:

<!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>
<div id="draggableDiv" style="overflow: scroll; height: 288px; width: 288px;">
<div>
<table>
<tr><th>head 1</th><th>head 2</th></tr><tr><td>hi all this is one</td><td>hi all this is two</td></tr></table></div><script>
var table_width = $('table').width();
var start_width,end_width;
var diff = 0;
$( "th" ).resizable({
    start: function(e, ui) {
        start_width = ui.size.width;
        console.log("start:"+ui.size.width);
    },
    resize:function(event,ui){ 
      diff = -(start_width - ui.size.width);
      $("table").css('width',(table_width+diff)+'px');
     // console.log(diff);
      //console.log(ui.size.width);
    },
    stop: function(e, ui) {
        end_width = ui.size.width;
        diff = end_width-start_width;
        table_width += diff;
        //$("table").css('width',table_width+'px');
        console.log("end  :"+ui.size.width);
        console.log("diff :"+diff);
    }
});
</script></div></body></html>