桌面軟體式的表格都有這個功能:滑鼠移到表頭的邊框線上時游標變成左右箭頭,按住拖曳會出現一條跟手的垂直輔助線,放開後欄寬就跟着變了。用 jQuery 也能做出來,以下是自己動手實作的過程。
需要一個 .cur 檔當作拖曳狀態的游標,在自己的硬碟裡搜尋 *.cur 通常就能找到現成的。
為了能在任何頁面直接套用、不必改動既有的 HTML,所有程式碼都包在 $(document).ready() 裡,寫進一支獨立的 JS 檔。
第一步:用一個 1px 寬的 div 模擬輔助線,頁面載入後附加到 body:
$(document).ready(function () {
$("body").append("<div id=\"line\" style=\"width:1px;height:200px;border-left:1px solid #00000000; position:absolute;display:none\"></div>");
});
第二步:處理滑鼠移到邊框上時的游標變形。原本想說在表頭裡塞一個很窄的區塊來觸發 mousemove 與 mouseout,但為了簡單起見,還是直接為整個 th 綁事件,再靠座標判斷是否靠近邊緣。
$("th").bind("mousemove", function (event) {
var th = $(this);
// 不給第一欄與最後一欄加效果
if (th.prevAll().length <= 1 || th.nextAll().length < 1) { return; }
var left = th.offset().left;
// 距離邊框線左右 4 像素內才觸發
if (event.clientX - left < 4 || (th.width() - (event.clientX - left)) < 4) {
th.css({ 'cursor': '/web/Page/frameset/images/splith.cur' }); // 換成你的游標路徑
} else {
th.css({ 'cursor': 'default' });
}
});
第三步:按下滑鼠時顯示輔助線,設定它的高度與位置,同時記下目前要調整的是哪個 th。由於一條邊框由左右兩個 th 共享,這裡一律取左邊那個。
$("th").bind("mousedown", function (event) {
var th = $(this);
if (th.prevAll().length < 1 || th.nextAll().length < 1) { return; }
var pos = th.offset();
if (event.clientX - pos.left < 4 || (th.width() - (event.clientX - pos.left)) < 4) {
var height = th.parent().parent().height();
$("#line").css({
"height": height,
"top": pos.top,
"left": event.clientX,
"display": ""
});
lineMove = true; // 全域旗標:目前處於調整欄寬狀態
// 一律取左邊的 th
currTh = (event.clientX - pos.left < th.width() / 2) ? th.prev() : th;
}
});
第四步:拖曳時讓輔助線跟手。因為滑鼠游標可能移出 th 範圍,這個效果要綁在 body 上:
$("body").bind("mousemove", function (event) {
if (lineMove == true) {
$("#line").css({ "left": event.clientX }).show();
}
});
第五步:放開滑鼠,套用新的欄寬。這裡同時給 body 與 th 都綁了 mouseup——原本以為只綁 body 就夠,但實際測試發現滑鼠停在 th 上時事件沒有觸發,只好兩處都掛。這段重複的程式碼怎麼抽成共用函式,當時還沒想通。
$("body").bind("mouseup", function (event) {
if (lineMove == true) {
$("#line").hide();
lineMove = false;
var pos = currTh.offset();
var index = currTh.prevAll().length;
currTh.width(event.clientX - pos.left);
currTh.parent().parent().find("tr").each(function () {
$(this).children().eq(index).width(event.clientX - pos.left);
});
}
});
$("th").bind("mouseup", function (event) {
if (lineMove == true) {
$("#line").hide();
lineMove = false;
var pos = currTh.offset();
var index = currTh.prevAll().length;
currTh.width(event.clientX - pos.left);
currTh.parent().parent().find("tr").each(function () {
$(this).children().eq(index).width(event.clientX - pos.left);
});
}
});
完成後,只要在需要的頁面引入這支 JS 檔,表格就自動具備拖曳調整欄寬的能力。
mouseup 的重複程式碼。為什麼滑鼠在 th 上時 body 的 mouseup 沒觸發,值得再查——通常與事件傳遞被中途攔截有關。抽成一個命名函式再分別 bind,是最基本的整理;更根本的做法是改用 $(document).on('mouseup', ...) 統一掛在文件上。
改用事件委托取代逐一綁定。示範中對每個 th 個別 bind,表格一多效能就會下滑。改成 $('table').on('mousemove', 'th', handler) 只需綁一次。
colspan 與 rowspan 沒有處理。程式用 children().eq(index) 依索引對齊各列的儲存格,一旦表中有合併儲存格,索引就會錯位。這是最容易在實務上出問題的一環。
欄寬最好寫回 localStorage。使用者調完重新整理就復原,體驗會打折。把每張表各欄的寬度存起來,下次載入時套用,實用度會高很多。
游標圖檔在 Firefox 下沒生效。不同瀏覽器對 .cur 的支援與路徑寫法有差異,Firefox 需要提供多重尺寸或使用 data URI,這點要另外處理。
另外,示範使用的 jQuery 版本為 1.2.6,若要搬到新專案,建議改用 on() 並移除已廢棄的寫法。