因为要做树形表格使用treeTable插件,但目前插件作者还没有更新表格固定列的功能,恰巧项目又需要实现表格固定列这一需求,所以自己写了能实现固定列的函数。
<script>
//固定列
function fixColumn() {
var tList = $("thead tr");
//thead
$.each(tList, function (index, item) {
$(item).find("th").eq(0).children().eq(0).css({ "border-right": "1px solid #e6e6e6","border-bottom": "1px solid #e6e6e6" });
$(item).find("th").eq(0).css({ position: "-webkit-sticky", position: "sticky", left: "0px", width: "202px", "background-color": " #f2f2f2", "z-index": "999" })
})
//tbody
var bodyList = $("tbody tr");
$.each(bodyList, function (index, item) {
$(item).find("td").eq(0).children().eq(0).css({ "border-right": "1px solid #e6e6e6","border-bottom": "1px solid #e6e6e6" });
$(item).find("td").eq(0).css({ position: "-webkit-sticky", position: "sticky", left: "0px", width: "202px", "background-color": " white", "z-index": "999" })
})
}
</script>
这个函数只需要放在表格数据渲染完成的done回调里面就可以实现,如下
实现前,无法固定第一列
实现后,可以固定第一列
提示:position 属性sticky存在浏览器兼容问题,部分浏览器不支持,目前我只测试了chrome、firefox、360浏览器,都可以实现该功能。