表格欄寬固定死的時候,內容一多就很難看。這裡把自己封裝的一個外掛分享出來,掛上去就能用滑鼠拖曳表頭邊界調整欄寬。外掛命名為 jquery.tableresize.js。
/*
Writen by mlcactus, 2014-11-24
這是一個 jQuery 外掛,能讓 table 的每一欄可以左右拖曳,使寬度變小或變大
用法:
單一 table:$("#table_id").tableresize();
頁面所有 table:$("table").tableresize();
*/
(function ($) {
$.fn.tableresize = function () {
var _document = $("body");
$(this).each(function () {
if (!$.tableresize) {
$.tableresize = {};
}
var _table = $(this);
// 設定 ID
var id = _table.attr("id") || "tableresize_" + (Math.random() * 100000).toFixed(0).toString();
var tr = _table.find("tr").first(), ths = tr.children(), _firstth = ths.first();
// 設定臨時變數存放物件
var cobjs = $.tableresize[id] = {};
cobjs._currentObj = null, cobjs._currentLeft = null;
ths.mousemove(function (e) {
var _this = $(this);
var left = _this.offset().left, top = _this.offset().top,
width = _this.width(), height = _this.height(),
right = left + width, bottom = top + height,
clientX = e.clientX, clientY = e.clientY;
var leftside = !_firstth.is(_this) && Math.abs(left - clientX) <= 5,
rightside = Math.abs(right - clientX) <= 5;
if (cobjs._currentLeft || clientY > top && clientY < bottom && (leftside || rightside)) {
_document.css("cursor", "e-resize");
if (!cobjs._currentLeft) {
if (leftside) {
cobjs._currentObj = _this.prev(); // 拖左邊界,調整前一欄
} else {
cobjs._currentObj = _this; // 拖右邊界,調整自己
}
}
} else {
cobjs._currentObj = null;
}
});
ths.mouseout(function (e) {
if (!cobjs._currentLeft) {
cobjs._currentObj = null;
_document.css("cursor", "auto");
}
});
_document.mousedown(function (e) {
if (cobjs._currentObj) {
cobjs._currentLeft = e.clientX;
} else {
cobjs._currentLeft = null;
}
});
_document.mouseup(function (e) {
if (cobjs._currentLeft) {
cobjs._currentObj.width(cobjs._currentObj.width() + (e.clientX - cobjs._currentLeft));
}
cobjs._currentObj = null;
cobjs._currentLeft = null;
_document.css("cursor", "auto");
});
});
};
})(jQuery);
設計上的兩個要點:滑鼠移到欄位邊界 5px 內時才把游標改成 e-resize,給人明確的可拖曳暗示;拖曳過程掛在 body 的 mousedown/mouseup 上,而不是表頭上,這樣滑鼠移出表頭後仍能順利完成拖動。另外用 $.tableresize[id] 為每個表格獨立保存狀態,讓同一頁有多張表格時互不干擾。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>表格欄寬拖曳</title>
<style type="text/css">
td{ text-align:center;}
</style>
<script type="text/javascript" src="script/jquery-1.10.2.js"></script>
<script type="text/javascript" src="script/jquery.tableresize.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// 使兩張 table 同時支援左右拖曳
$("table").tableresize();
});
</script>
</head>
<body>
表格1<br/>
<table cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
<tbody><tr>
<td style="width:200px;">ID</td><td style="width:200px;">名字</td><td style="width:200px;">年紀</td><td style="width:200px;">地址</td><td style="width:200px;">電話</td>
</tr><tr>
<td>22</td><td>Name:44</td><td>Age:23</td><td>Address:47</td><td>Phone:15</td>
</tr><tr>
<td>28</td><td>Name:42</td><td>Age:68</td><td>Address:30</td><td>Phone:50</td>
</tr><tr>
<td>29</td><td>Name:63</td><td>Age:48</td><td>Address:90</td><td>Phone:76</td>
</tr></tbody>
</table>
<br/>表格2<br/>
<table cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
<tbody><tr>
<td style="width:200px;">ID</td><td style="width:200px;">名字</td><td style="width:200px;">年紀</td><td style="width:200px;">地址</td><td style="width:200px;">電話</td>
</tr><tr>
<td>22</td><td>Name:44</td><td>Age:23</td><td>Address:47</td><td>Phone:15</td>
</tr><tr>
<td>28</td><td>Name:42</td><td>Age:68</td><td>Address:30</td><td>Phone:50</td>
</tr></tbody>
</table>
</body>
</html>
呼叫方式就兩種:$("#table_id").tableresize() 針對單一表格,$("table").tableresize() 一次處理全頁表格。若搭配 colgroup 使用,或想加上雙擊自動還原寬度,都可以在這個基礎上繼續擴充。