关于LayUI 表格高度解决方案

需求是这样式的:我有一个产品列表,但是我想在产品列表中显示产品主图信息。(本文只涉及LayUI技巧,不涉及JAVA)
JS 渲染部分

table.render({
            cellHeight: 300,
            elem: '#currentTableId',
            url: 'Product/ProductAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'id', width: 20, title: 'ID', sort: true},
                {field: 'productName', width: 150, title: '产品名称'},
                {field: 'productImage', width:100, title: '图片', minWidth: 50,
                    templet:function(res){
                        return '<img src="'+res.productImage+'"> </img>'
                    }
                },
                {field: 'typeName', width: 100, title: '类别'},
                {field: 'spc', width: 150, title: '规格'},
                {field: 'productUnit', width: 80, title: '单位'},
                {field: 'parameters', width: 100, title: '技术参数'},
                {field: 'agencyPrice', width: 100, title: '代理价'},
                {field: 'customPrice', width: 100, title: '经销价'},
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            id:'testReload'
        });

但是会出现一个问题,图片显示不全。
解决方案就是修改Table的样式

<style>
    .layui-table-cell{
        height:auto;
        line-height: 30px;
    }
</style>

完整页面如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet"   th:href="@{lib/layui-v2.5.5/css/layui.css}"   media="all">
    <link rel="stylesheet"  th:href="@{css/public.css}"   media="all">
    <script th:src="@{lib/layui-v2.5.5/layui.js}"  charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">
			<!-- 数据渲染区  -->
		</table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="chose">选择</a>
        </script>

    </div>
</div>
<script th:src="@{lib/layui-v2.5.5/layui.js}"  charset="utf-8"></script>
<script>

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            cellHeight: 300,
            elem: '#currentTableId',
            url: 'Product/ProductAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'id', width: 20, title: 'ID', sort: true},
                {field: 'productName', width: 150, title: '产品名称'},
                {field: 'productImage', width:100, title: '图片', minWidth: 50,
                    templet:function(res){
                        return '<img src="'+res.productImage+'"> </img>'
                    }
                },
                {field: 'typeName', width: 100, title: '类别'},
                {field: 'spc', width: 150, title: '规格'},
                {field: 'productUnit', width: 80, title: '单位'},
                {field: 'parameters', width: 100, title: '技术参数'},
                {field: 'agencyPrice', width: 100, title: '代理价'},
                {field: 'customPrice', width: 100, title: '经销价'},
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            id:'testReload'
        });

        var $ = layui.$, active = {
            reload: function(){
                //用户名
                var usernameId=$('#username');
                var telId=$('#tel');//电话
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                       username:usernameId.val().trim(),
                       tel:telId.val().trimEnd()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });



        /**
         * 获取选中id的集合
         */
        function getCheckId(data){
            var arr=new Array();
            for(var i=0;i<data.length;i++){
                arr.push(data[i].id);
            }
           //拼接id记录
            return arr.join(",");
        }


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'chose') {

                let a=parent.ChooseValues(data);
                console.log(JSON.stringify(a));
                var index= parent.layer.getFrameIndex(window.name);

                parent.layer.close(index);
                return false;
            };
        })
    })
</script>

</body>
<style>
    .layui-table-cell{
        height:auto;
        line-height: 30px;
    }
</style>
</html>

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

### 调整 Layui 表格组件中行的高度Layui表格组件中,可以通过自定义 CSS 来调整表格的行高。具体来说,可以针对 `td` 和 `th` 元素设置高度以及内边距(padding),从而实现对行高的控制。 以下是具体的解决方案: #### 自定义 CSS 设置行高 通过覆盖默认样式,可以直接修改表格单元格的高度和内边距。例如,可以在项目的全局样式文件或者局部样式中加入以下代码: ```css /* 针对表格头部 */ table.layui-table thead th { height: 40px; /* 设置表头高度 */ line-height: 40px; /* 设置文字垂直居中 */ padding: 5px; /* 可选:调整上下间距 */ } /* 针对表格主体部分 */ table.layui-table tbody td { height: 40px; /* 设置每一行的高度 */ line-height: 40px; /* 设置文字垂直居中 */ padding: 5px; /* 可选:调整上下间距 */ } ``` 上述代码中的 `.layui-table` 是 Layui 默认为表格添加的类名[^2]。如果需要进一步优化显示效果,还可以调整字体大小、颜色以及其他相关属性。 #### 动态加载样式 如果希望在运行时动态应用这些样式,也可以借助 JavaScript 或者 jQuery 实现。例如,在初始化表格之后执行以下脚本: ```javascript document.querySelectorAll('table.layui-table tbody td').forEach(function(cell) { cell.style.height = '40px'; // 动态设置高度 cell.style.lineHeight = '40px'; // 动态设置行高等效于垂直居中 }); ``` 此方法适用于需要根据不同条件动态调整样式的场景。 --- #### 注意事项 1. **优先级问题** 如果发现自定义样式未生效,可能是因为原有样式的权重较高。此时可以根据重要性规则增加 `!important` 关键字来提升优先级[^3]。例如: ```css table.layui-table tbody td { height: 40px !important; line-height: 40px !important; } ``` 2. **响应式设计** 在移动端或其他分辨率下,建议测试不同屏幕尺寸下的表现,并考虑使用媒体查询适配不同的设备。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值