通过jquery实现拖拽table表头改变列宽

效果:
在这里插入图片描述

直接上代码,有注释:

<!DOCTYPE html>
<html>
<head>
    <style>
        table, td, th {
            border: 1px solid #ddd;
            text-align: left;
        }

        table {
            border-collapse: collapse;
            width: 100%;
            table-layout: fixed;
        }

        th, td {
            padding: 5px;
            position: relative;
            user-select: none;
            /*text-overflow: ellipsis;*/
            word-break: break-all;
        }

        .th-sisehandler {
            position: absolute;
            right: -0.5px;
            top: 0;
            z-index: 1;
            width: 5px;
            height: 100%;
            padding-left: 4px;
            cursor: col-resize;
        }

        .th-sisehandler::after {
            content: '';
            display: block;
            width: 10px;
            background-color: #4CAF50; /*演示为了看效果加上的,可以去掉*/
            height: 100%;
        }

        .siselayer {
            position: fixed;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            z-index: 9999;
            background-color: #4445a049; /*演示为了看效果加上的,可以去掉*/
            cursor: col-resize;
        }

    </style>
    <meta charset="UTF-8">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<div style="width: 600px;overflow: auto">
    <table>
        <tr>
            <th width="150">Firstname</th>
            <th width="150">Lastname</th>
            <th width="150">Savings</th>
        </tr>
        <tr>
            <td>Bill</td>
            <td>Gates</td>
            <td>$100</td>
        </tr>
        <tr>
            <td>Steve</td>
            <td>Jobs</td>
            <td>$150</td>
        </tr>
        <tr>
            <td>Elon</td>
            <td>Musk</td>
            <td>$300</td>
        </tr>
        <tr>
            <td>Mark</td>
            <td>Zuckerberg</td>
            <td>$250</td>
        </tr>
    </table>
</div>
<script>

    $("th").mouseover(function (e) {
        if (($(this).find("div").length <= 0)) {
            //1.鼠标移动到表头上时,在th内部添加一个div 元素,用于处理后续拖动事件
            $(this).append("<div class='th-sisehandler'></div>")

            //2.处理上面添加的元素的鼠标按下事件
            $(".th-sisehandler").mousedown(function (evt) {
                //3.在添加的元素上按下时,记录下当前的th表头
                let dragTh = $(this).parent()


                //4.记录按下时的鼠标位置
                let oldClientX = evt.clientX;
                //5.获取当前鼠标按下时的表头的宽度
                let oldWidth = dragTh.width();
                /*6.添加一个全局layer层,用于处理鼠标按下时鼠标移动事件,因为不能在第一步添加的元素上处理鼠标移动事件,添加的元素太小,
                    鼠标容易跑出范围,就捕获不到后续事件
                    所以添加一个全局的遮罩层,捕获鼠标移动事件。
                 */

                let changeSizeLayer = $('<div class="siselayer"></div>');
                $("body").append(changeSizeLayer);

                changeSizeLayer.on('mousemove', function (evt) {
                    //7.处理遮罩层的鼠标移动事件,计算新的表头宽度
                    var newWidth =evt.clientX - oldClientX + oldWidth;
                    //设置新的宽度
                    dragTh.attr('width',Math.max(newWidth,1));

                });

                changeSizeLayer.on('mouseup', function (evt) {
                    //8.鼠标按键复位时,要清楚遮罩层
                    changeSizeLayer.remove();
                    dragTh.find('.th-sisehandler').remove();
                });
            })
        }

        $(this).mouseleave(function () {
            //9.鼠标离开表头时,要移除第一步添加的div
            $(this).find("div").remove()
        })
    })


</script>
</body>
</html>
Bootstrap Table 是一个功能强大的 jQuery 表格插件,可以实现表格的排序、分页、搜索、编辑、导出等功能。要实现表头固定,以及列宽可拖动改变度大小,可以通过以下步骤实现: 1. 引入 Bootstrap Table 插件和相关样式文件。 ```html <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.3/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap-table/1.12.1/bootstrap-table.min.css"> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-table/1.12.1/bootstrap-table.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-table/1.12.1/extensions/editable/bootstrap-table-editable.min.js"></script> ``` 2. 在表格标签中加入 data-height 属性,设置表格高度,并加入 data-fixed-columns 属性,设置固定列数。 ```html <table id="table" data-height="400" data-fixed-columns="3"> <thead> <tr> <th data-field="id" data-sortable="true">ID</th> <th data-field="name" data-sortable="true">Name</th> <th data-field="price" data-sortable="true">Price</th> <th data-field="operate">Operate</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Item 1</td> <td>$1</td> <td><button class="btn btn-primary">Edit</button></td> </tr> <tr> <td>2</td> <td>Item 2</td> <td>$2</td> <td><button class="btn btn-primary">Edit</button></td> </tr> <tr> <td>3</td> <td>Item 3</td> <td>$3</td> <td><button class="btn btn-primary">Edit</button></td> </tr> </tbody> </table> ``` 3. 在 JavaScript 文件中,使用 bootstrapTable 函数初始化表格,并加入 fixedColumns 和 resizable 属性,分别设置固定列和可改变列宽。 ```javascript $(function() { $('#table').bootstrapTable({ fixedColumns: true, resizable: true }); }); ``` 这样就可以实现表头固定,以及列宽可拖动改变度大小的功能了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值