table表格的相关设置(边框合并、隔行变色、添加内边框、单独设置某一列)

本文介绍了如何使用CSS来美化HTML表格,包括如何实现边框合并、隔行变色以及为表格添加内边框。同时,通过JavaScript示例展示了如何单独设置每一列的样式,如宽度调整和特定值的文本加粗。

1、为table表格设置边框合并

table{
    border-collapse:collapse;
    border-spacing:0;
}

2、隔行变色和添加内边框
function addColor() {
    var tbodyTrList = $(".table tbody tr");
    var tdList = $(".table td");
    for (var i = 0; i < tbodyTrList.length; i++) {
        if (i % 2 == 0) {
            tbodyTrList[i].className = "color";//隔行变色
        }
    }
    for (var j = 0; j < tdList.length; j++) {
        if (j % 6 != 0) {
            $(tdList[j]).addClass("border-left");//除第一列以外添加左边框
        }
    }
}

addColor();

3、单独设置每一列的样式

<script>
        $(document).ready(function () {
            var obj;
            $("tbody tr").each(function () {               
                $(this).find("td:nth-child(2)").first().css({"width":"360px"});//设置第二列宽度
                obj = $(this).find("td:nth-child(3)");//设置第三列
                if($(obj).html()!="0"){//值不是0的加粗
                    $(obj).css({"font-weight":"bold"});
                }
            });
        });

</script>

const WIDTH = 750; const HEIGHT = 1334; const ctx = uni.createCanvasContext('myCanvas', this); const padding = 10; // 内边距 const colWidths = [100, 100, 100, 100, 100, 100, 100]; // 每列的宽度,根据数据动态调整 const colNames = ['序号', '商品名称', '报单数', '分拣数', '单位', '单价', '小计']; // 列名 const rowHeight = 30; // 行高 const textStyle = { fontSize: 14, fillStyle: '#000' }; // 文本样式 const lineStyle = { strokeStyle: '#ccc', lineWidth: 1 }; // 线样式 const bgColor = '#f9f9f9'; // 背景色 const textColor = '#333'; // 文本颜色 const headerBgColor = '#ddd'; // 表头背景色 const headerTextColor = '#000'; // 表头文本颜色 const headerHeight = 40; // 表头高度 let totalWidth = colWidths.reduce((acc, cur) => acc + cur, 0) + padding * 2; // 总宽度,包括内边距 let totalHeight = headerHeight + (this.goods_info.length + 1) * rowHeight; // 总高度,包括表头和行高 // 设置画布大小 ctx.setFillStyle(bgColor); ctx.fillRect(0, 0, totalWidth, totalHeight); // 绘制背景 ctx.setFillStyle(headerBgColor); // 设置表头背景色 ctx.fillRect(0, 0, totalWidth, headerHeight); // 绘制表头背景 ctx.setFontSize(textStyle.fontSize); // 设置字体大小 ctx.setFillStyle(headerTextColor); // 设置表头文本颜色 colNames.forEach((name, index) => { // 绘制表头文本 ctx.fillText(name, padding + colWidths.slice(0, index).reduce((acc, cur) => acc + cur, 0), headerHeight / 2 + padding); ctx.setStrokeStyle(lineStyle.strokeStyle); // 设置线条颜色和宽度,绘制列分隔线 ctx.moveTo(padding + colWidths.slice(0, index + 1).reduce((acc, cur) => acc + cur, 0), headerHeight); ctx.lineTo(padding + colWidths.slice(0, index + 1).reduce((acc, cur) => acc + cur, 0), totalHeight); ctx.stroke(); // 绘制线条 }); const startX = 20; const startY = 20; // 绘制表格 let tableRows = [] tableRows = this.goods_info.map(item => [0, item.goods_name, item.num,item.sortingstatu,item.unitname, item.price, item.totalprice ]); // 绘制表行 tableRows.forEach((row, rowIndex) => { row.forEach((cell, colIndex) => { ctx.fillText(cell, startX + colIndex * colWidths, startY + (rowIndex + 1) * rowHeight + rowHeight / 2); ctx.strokeRect(startX + colIndex * colWidths, startY + (rowIndex + 1) * rowHeight, colWidths, rowHeight); }); }); let tableFooter = ['总计', '', this.total_price] // 绘制表尾 const footerY = startY + (this.goods_info.length + 1) * rowHeight; tableFooter.forEach((footer, index) => { ctx.fillText(footer, startX + index * colWidths, footerY + rowHeight / 2); ctx.strokeRect(startX + index * colWidths, footerY, colWidths, rowHeight); }); console.log("canvas"); ctx.draw()
10-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值