layui固定表格表头的方法:首先找到layui中的table.js文件;然后在变量table中加入fiexdrowheight属性;最后添加代码为“if(object.prototype.tostring.call(...))”即可。
推荐:《layui教程》
实现效果:表头和底部分页固定,鼠标滚动只会滚顶body中数据。效果如下:
1、找到layui中的table.js文件,在变量table中加入属性,如图红框中内容:
fiexdrowheight:是否开启固定行高,默认是false
fiexdrowheight_rows:表格中显示条数,默认是10条
以上两个参数都可以通过自己传入参数值
2、找到pulldata函数:
在此函数中ajax异步请求成功回调的success函数中最后添加如下代码:
//固定行高、表头处理if(object.prototype.tostring.call(options.fiexdrowheight).slice(8, -1) === 'boolean' && options.fiexdrowheight) { var p_ = $([lay-id=' + options.id + ']) var tr_len = p_.find(elem_main).find(tr).length if(tr_len > 10){ if(object.prototype.tostring.call(options.fiexdrowheight_rows).slice(8, -1) !== 'number') { options.fiexdrowheight_rows = 10 } var height_main = (options.fiexdrowheight_rows * 39) + 'px' var height_fixed = (options.fiexdrowheight_rows * 39) + 'px' //如果出现横向滚动条时 if(p_[0].parentnode.clientwidth < document.getelementsbyclassname('layui-table-main')[0].getelementsbyclassname('layui-table')[0].clientwidth) { height_main = ((options.fiexdrowheight_rows * 39) + 18) + 'px' } p_.find(elem_main).css(height, height_main); p_.find(elem_fixl).find(elem_body).css(height, height_fixed); p_.find(elem_fixr).find(elem_body).css(height, height_fixed); }else { p_.find(elem_main).css(height, auto); p_.find(elem_fixl).find(elem_body).css(height, auto); p_.find(elem_fixr).find(elem_body).css(height, auto); }}
3、应用:如图:
以上就是layui怎么固定表格的表头的详细内容。
