表格自由拉伸修改版

该博客主要介绍使用JavaScript实现表格的相关功能,包括自由拉伸表格,在缩放当前列时保证其他列宽度不变且表格x轴超出可滑动,同时在拉伸过程中控制文字的显示与溢出省略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要实现一下几点

1.自由拉伸表格

2.缩放当前列其他列宽度不变,表格x轴超出滑动

3.拉伸的同时控制文字的显示与溢出省略

​
<!DOCTYPE html>

<html lang="en">



<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

<style>

.box {

width: 100%;

height: 100%;

}



table {

width: 100%;

height: 300px;



}



tr {}



tr:nth-of-type(odd) {

background-color: #fff;

}



tr:nth-of-type(even) {

background-color: #EDF6FA;

}



th {



height: 40px;

background-color: #fff;

text-align: center;

padding: 0;

line-height: 40px;

color: #708BB3;

font-size: 14px;

border-right: 2px solid #E6EDEB;

vertical-align: middle;

}



th:last-child {

width: 159px;

border-right: none;

}



td {



height: 40px;

line-height: 40px;

text-align: center;

color: #5C7191;

font-size: 12px;

border-right: 2px solid #E6EDEB;

padding: 0;

vertical-align: middle;

overflow: hidden;

text-overflow: ellipsis;

white-space: nowrap;

}



td:nth-child(1) {

width: 87px !important;

}



td:last-child {

width: 159px !important;

cursor: pointer;

border-right: none;

}



.tableDetail:hover {

text-decoration: none;

}



.tableDetail:before {

content: "";

display: inline-block;

vertical-align: middle;

width: 13px;

height: 15px;

margin-right: 4px;

background: url(../../img/customer/eye.png) no-repeat;

background-size: 100%;

}



.tableDetail {

display: block;

margin: auto;

width: 60px;

height: 26px;

background: rgba(72, 158, 255, 1);

border: 1px solid rgba(69, 151, 245, 1);

box-shadow: 0px 1px 2px 0px rgba(120, 135, 142, 0.2);

border-radius: 2px;

color: #fff !important;

font-size: 12px;

text-align: center;

line-height: 24px;

cursor: pointer;

}

</style>

</head>



<body>

<div class="box">

<table cellspacing="0" cellpadding="0" border="1">

<tr>

<th>語文</th>

<th>数学</th>

<th>英语</th>

<th>历史</th>

<th>地理</th>

<th>化学</th>

<th>生物</th>

<th>物理</th>

<th>政治</th>

<th>疯子</th>

</tr>

<tr>

<td title="111111111111111111111111111">1111111111111111111111111111111111111111</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

</tr>

<tr>

<td title="111111111111111111111111111">1111111111111111111111111111111111111111</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

</tr>

<tr>

<td title="111111111111111111111111111">1111111111111111111111111111111111111111</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

</tr>

<tr>

<td title="111111111111111111111111111">1111111111111111111111111111111111111111</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

</tr>

<tr>

<td title="111111111111111111111111111">1111111111111111111111111111111111111111</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

<td>1</td>

</tr>

</table>

</div>

<script src="assets/jquery-3.1.1.js"></script>

<script>

let table =document.getElementsByTagName("table")[0];

        let rows = table.rows[0];

        let flag = false;

        var that;

        // 遍历

        document.onselectstart = new Function("return false");

        for (j = 0; j < rows.cells.length; j++) {

            $(rows.cells[j]).unbind("mousemove");

            $(rows.cells[j]).mousemove(function (e) {

                if (this.offsetWidth - 20 <= e.offsetX && e.offsetX <= this.offsetWidth) {

                    this.style.cursor = "col-resize";

                    // $(this).bind("mousedown");

                } else {

                    // flag = false;

                    this.style.cursor = "default";

                }

                if (that == undefined) {

                    that = this;

                }

                if (flag) {

                    that.style.cursor = 'default';

                    if (that.oldWidth + (event.x - that.oldX) > 0) {

                        that.width = that.oldWidth + (event.x - that.oldX);

                    }

                    //调整列宽

                    that.style.width = that.width;

                    that.style.cursor = 'col-resize';

                    //调整该列中的每个Cell

                    table = that;

                    while (table.tagName != 'TABLE') table = table.parentElement;

                    for (j = 0; j < table.rows.length; j++) {

                        table.rows[j].cells[that.cellIndex].width = that.width;

                    }

                    // 表格中的文字随着表格的伸缩而显示隐藏

                    var tdWidth = that.width;

                    var tdIndex = that.cellIndex;

                    for (let v of table.rows) {

                        var tdTitle = v.cells[tdIndex].title;

                        if (tdTitle) {

                            $(v.cells[tdIndex]).html("<div style='width:" + tdWidth + "px;text-align:center;overflow:hidden;text-overflow:ellipsis;white-space: nowrap;'>" + tdTitle + "</div>");

                        }

                    }

                    //调整整个表

                    // table.width = that.tableWidth + (that.offsetWidth - that.oldWidth);

                    // table.style.width = table.width;

                }

            })

            $(rows.cells[j]).unbind("mousedown");

            $(rows.cells[j]).mousedown(function () {

                that = this;

                flag = true;

                // if (event.offsetX > that.offsetWidth - 10) {

                    that.oldX = event.x;

                    that.oldWidth = that.offsetWidth;

                    $(that).bind("mousemove");

                // }

                //记录Table宽度

                // table = that;

                // while (table.tagName != 'TABLE') {

                // table = table.parentElement

                // };

                // that.tableWidth = table.offsetWidth;

            })

            $(rows.cells[j]).unbind("mouseup");

            $(rows.cells[j]).mouseup(function () {

                if (that == undefined) {

                    that = this;

                }

                flag = false;

            })

            // 非表头的地方点击更改flag值

            $(rows).siblings().unbind("mouseup");

            $(rows).siblings().mouseup(function(){

                flag = false;

            })

        }

</script>

</body>

</html>

​

 

从你的代码片段来看,这是基于某个前端框架(例如 Ant Design 或类似库)构建的一个表格组件。如果你想让 `<Table>` 的列宽自动调整,并且内容能够自适应显示宽度,可以尝试以下几种方式: --- ### 解决方案:设置 `autoWidth` 和移除固定宽度 #### 修改思路: 1. **删除固定的宽度**:如果你在某些地方设置了特定的列宽(如通过 `columns` 配置),需要将其改为动态计算。 2. **启用弹性布局**:检查是否支持 CSS 弹性盒模型 (`flex`) 来实现列宽自适应。 以下是改进后的代码示例: ```jsx <Table<DataType> columns={columns.map(col => ({ ...col, width: undefined, // 删除固定宽度属性 ellipsis: true // 如果内容超出则省略号处理 }))} dataSource={dataSource} rowClassName={(record, index) => (index % 2 === 1 ? "even" : "odd")} scroll={{ x: 'fit-content', y: 'calc(100vh - 350px)' }} // 自动适配宽度 pagination={false} bordered summary={() => ( <Table.Summary fixed> <Table.Summary.Row> <Table.Summary.Cell index={0} colSpan={3}> 合计 </Table.Summary.Cell> {items.map((item, index) => ( <Table.Summary.Cell index={index + 3}>{item}</Table.Summary.Cell> ))} </Table.Summary.Row> </Table.Summary> )} /> ``` --- ### 关键点解析: 1. **移除固定宽度** 原始配置中可能存在对每一列的显式宽度设定(如 `width=200`)。你可以将它们设为 `undefined` 或者直接去掉该字段。 2. **使用 `scroll.x='fit-content'`** 将水平滚动模式改为 `'fit-content'`,这会让表格根据实际内容大小进行自适应伸缩。 3. **添加文本溢出样式** 使用 `ellipsis=true` 属性可以让过长的内容隐藏并显示省略号,避免因文字过多导致界面混乱。 4. **CSS 调整** 确保父容器的样式也允许子元素自由拉伸,例如: ```css .ant-table { table-layout: auto; /* 动态分配各列比例 */ } ``` --- ### 其他注意事项: 如果上述修改仍然无法满足需求,请进一步确认以下几个方面: - 表格所使用的 UI 框架版本是否存在限制; - 数据源结构是否有异常值影响渲染效果; - 是否存在外部全局样式的干扰。 希望以上建议对你有帮助! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值