前端表格列宽自适应

function getAdaptionWidth(prop, tableData) {

        //接收两个参数,prop为列的属性值,tableData为表格数据源

        prop = prop + ''

        if(!Array.isArray(tableData) || tableData.length === 0) {

            return

        }

        //判断传递的数据是否可用

        if(!prop || prop.length || prop.length === 0 || prop === undefined || prop === null) {

            return

        }

        //获取该列中最长的数据

        let index = 0

        let columnContent = ''

        for(let i = 0; i < tableData.length; i++) {

            if(tableData[i][prop] === null) {

                continue

            }

            const now_text = tableData[i][prop] + ''

            const max_text = tableData[index][prop] + ''

            if(now_text.length > max_text.length) {

                index = i

            }

        }

        columnContent = tableData[index[prop]]

        let coulmnWidth = 0

        if(columnContent === null || columnContent === undefined) {

            columnContent = ""

        }

        for(const char of columnContent) {

            if((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z')) {

                coulmnWidth += 10

            }else if(char >= '\u4e00' && char <= '\u9fa5') {

                coulmnWidth += 20

            }else {

                coulmnWidth += 10

            }

        }

        if(coulmnWidth < 85) {

            coulmnWidth = 86

        }

        if(coulmnWidth > 200) {

            coulmnWidth = 200

        }

        return coulmnWidth + 'px'

    }

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值