2022年4月自写json转table记录

news2024/10/7 20:28:34

表头属性

属性名描述类型默认值
key

设置字段名。通常是表格数据列的唯一标识

string-
label

设置列的标题。

string-
width

设置列宽。若不填写,则自动分配;若填写,则支持值为:数字、百分比。如: width: 200 / width: '30%'

number/string-
align

单元格排列方式。可选值有:left | center | right

string

left

templet{key: "name", label:"姓名", align: "left",  tempelt:function(d){
        return '<span style="color:orange"              οnclick=showName(\''+d.name+'\')>'+d.name+'</span>';
}},
function-
fixed

还没开发,预留字段

设置固定列,即不跟随 table 横向滚动条而滚动。可选值有:

  • left 固定在左
  • right 固定在右
string-

表格基础属性

属性名描述类型默认值
el绑定原始 table 元素,必填。string/DOM-
height

设置表格容器高度,默认自适应。其他可选值的规则如下:

  • height: 315 设置固定高度
number
string
-
resizable表格宽度随页面大小变化自适应boolean true
rows

表头属性集,通过数组定义表头,必填。 更多表头属性见 : #表头属性

array-
data直接赋值数据。既适用于只展示一页数据array-

shaoyu.js

(function (global) {
    "use strict";
    var Shaoyu = function () {
        this.options = {
            el: "",
            page: true,
            rows: [],
            height: "",
            data: [],
            keys: [],
            resizable: true
        };
        this.cellMinWidth = 80;
        this.isSzScroll = false;
        this.isHxScroll = false;
        this.kongyuWidth = 0;
    };

    Shaoyu.prototype = {
        init: function (config) {
            var _this = this;
            if (!config.el) {
                console.error("el is required!");
                return false;
            }
            config.el = _this.getDom(config.el);
            _this.options = Object.assign(this.options, config);
            var str = '<div class="shaoyu-table-content"><div class="shaoyu-table-header" style="overflow: hidden;"><table class="shaoyu-table" style="table-layout:fixed;"></table></div></div>';
            _this.append(_this.options.el, str, 1);
            _this.render();
            return _this;
        },
        render: function () {
            this.renderHeader();
            this.renderBody();
            this.renderCellWidth();
            this.bindEvent();
        },
        //渲染头部
        renderHeader: function () {
            var cellStyleArr = [];
            cellStyleArr.push(".shaoyu-table-content{border-width: 1px;border-style: solid;border-color: #eee;}");
            cellStyleArr.push(".shaoyu-table tbody tr:hover,.shaoyu-table thead tr{ background-color: #FAFAFA;}");
            // cellStyleArr.push("th:nth-last-child(1),td:nth-last-child(1){ border-right: none !important;}");
            cellStyleArr.push('.shaoyu-table-thead>tr>th{padding:5px 0;border-top:none;border-left:none;border-width:1px;border-style:solid;border-color:#eee;font-weight: 400;}');
            cellStyleArr.push(".shaoyu-table-cell{height:28px;line-height:28px;padding:0 15px;position:relative;box-sizing:border-box;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}");
            cellStyleArr.push(".shaoyu-table-tbody>tr>td{border-width:1px;border-style:solid;border-color:#eee;}");
            cellStyleArr.push(".shaoyu-table-thead>tr>th,.shaoyu-table-tbody>tr>td{position:relative;padding:0px 15px;min-height:20px;line-height:20px;font-size:14px;border-top: none;border-left: none;}");
            cellStyleArr.push("div.shaoyu-table-content .shaoyu-table{border-collapse:collapse;border-spacing:0;background-color:#fff;color:#666;}");
            var str = "";
            var _this = this;
            _this.options.rows.map((x, i) => {
                str = str + '<th class="shaoyu-table-cell" ><div class="shaoyu-table-cell-' + x.key + '">' + x.label + '</th>';
                _this.options.keys.push(x.key);
                cellStyleArr.push('.shaoyu-table-cell-' + x.key + '{text-align: ' + (x.align ? x.align : left) + ';}');
            })
            str = '<thead class="shaoyu-table-thead"><tr>' + str + '</tr></thead>';
            var el = document.querySelector("div.shaoyu-table-content div.shaoyu-table-header table");
            cellStyleArr.push("div.shaoyu-table-body{height:" + this.options.height + "px;overflow: auto;}");
            _this.append(el, str, 2);
            _this.renderStyle(cellStyleArr);
        },
        renderStyle: function (arr) {
            arr.unshift("<style>");
            arr.push("</style>");
            this.append(document.querySelector("html head"), arr.join(""), 1);
        },
        //计算每列列宽
        renderCellWidth: function () {
            var cellStyleArr = [];
            var _this = this;
            var allWidth = _this.options.el.offsetWidth;
            var hasWidthCellNum = 0;
            var overedWidth = 0;
            _this.options.rows.map(x => {
                if (x.hasOwnProperty("width")) {
                    hasWidthCellNum++;
                    overedWidth = overedWidth + x.width;
                    cellStyleArr.push(".shaoyu-table-cell-" + x.key + "{width: " + x.width + "px;}")
                }
            });
            console.log(_this.isSzScroll, _this.isHxScroll)
            if (((_this.options.rows.length - hasWidthCellNum) * _this.cellMinWidth + overedWidth) < allWidth) {//如果所有列宽度没有超过容器宽度
               
                if(_this.isSzScroll){
                    var residueWidth = allWidth - overedWidth - hasWidthCellNum * 32-15;
                    var cellWidth = Math.ceil(residueWidth / (_this.options.rows.length - hasWidthCellNum));
                    console.log(cellWidth);
                    cellWidth = (cellWidth - 32) > _this.cellMinWidth ? cellWidth - 32 : _this.cellMinWidth;
                }else{
                    var residueWidth = allWidth - overedWidth - hasWidthCellNum * 32;
                    var cellWidth = Math.ceil(residueWidth / (_this.options.rows.length - hasWidthCellNum));
                    cellWidth = (cellWidth - 32) > _this.cellMinWidth ? cellWidth - 32 : _this.cellMinWidth;
                }
                _this.kongyuWidth = allWidth-overedWidth-hasWidthCellNum*32-((_this.options.rows.length - hasWidthCellNum)*(cellWidth+31));
                _this.kongyuWidth = _this.kongyuWidth>0?_this.kongyuWidth:0;
                if(_this.isSzScroll){
                    _this.kongyuWidth = _this.kongyuWidth-16;
                }
                console.log(allWidth,overedWidth,hasWidthCellNum,residueWidth,cellWidth,_this.kongyuWidth);
                var firstNode = null;
                _this.options.rows.map((x,i) => {
                    if (!x.hasOwnProperty("width")) {
                        if(!firstNode){
                            firstNode = true
                            cellStyleArr.push(".shaoyu-table-cell-" + x.key + "{width: " + (cellWidth+_this.kongyuWidth) + "px;}")
                        }else{
                            cellStyleArr.push(".shaoyu-table-cell-" + x.key + "{width: " + cellWidth + "px;}")
                        }
                        
                    }
                });
            }else {//如果所有列宽度超过容器宽度
                _this.options.rows.map(x => {
                    if (!x.hasOwnProperty("width")) {
                        cellStyleArr.push(".shaoyu-table-cell-" + x.key + "{width: " + _this.cellMinWidth + "px;}")
                    }
                });
            }
            if(_this.isSzScroll){
                cellStyleArr.push("th:nth-last-child(2){ border-right: solid 1px #eee !important;}")
            }else{
                if(_this.isHxScroll){
                    cellStyleArr.push("th:nth-last-child(2){ border-right: none !important;}")
                }
            }
            _this.renderStyle(cellStyleArr);

        },
        //判断是否有滚动条:vertical纵轴,horizontal横轴
        hasScrolled: function (element, direction) {
            if (direction === 'vertical') {
                return element.scrollHeight > element.clientHeight;
            } else if (direction === 'horizontal') {
                if (this.isSzScroll) {
                    return element.scrollWidth > element.clientWidth + 17;
                }
                return element.scrollWidth > element.clientWidth;
            }
        },
        //渲染body
        renderBody: function () {
            var _this = this;
            var str = '<div class="shaoyu-table-body" ><table class="shaoyu-table" style="table-layout: fixed;"><tbody class="shaoyu-table-tbody"></tbody></table></div>';
            var el = document.querySelector("div.shaoyu-table-content");
            _this.append(el, str, 1);
            _this.options.data.map(x => {
                var str = "";
                _this.options.rows.map(y => {
                    if (y.hasOwnProperty("tempelt")) {
                        str = str + '<td class="shaoyu-table-cell" ><div class="shaoyu-table-cell-' + y.key + '">' + y.tempelt(x) + '</th>'
                    } else {
                        str = str + '<td class="shaoyu-table-cell" ><div class="shaoyu-table-cell-' + y.key + '">{{' + y.key + '}}</th>'
                    }
                })
                str = '<tr>' + str + '</tr>';
                var el = document.querySelector("div.shaoyu-table-content div.shaoyu-table-body table tbody");
                _this.append(el, str, 2);
                _this.parseData(el, x)
            })
            _this.onscroll();
            //如果有竖向滚动
            _this.isSzScroll = _this.hasScrolled(document.querySelector("#container div.shaoyu-table-content div.shaoyu-table-body"), "vertical");
            _this.isHxScroll = _this.hasScrolled(document.querySelector("#container div.shaoyu-table-content div.shaoyu-table-body"), "horizontal");
            if (_this.isSzScroll) {
                var str = '<th class="shaoyu-table-cell"><div class="" style="width: 17px;"></div></th>';
                var el = document.querySelector("div.shaoyu-table-content div.shaoyu-table-header table tr");
                _this.append(el, str, 3);
            }
            var cellStyleArr = [];
            if (_this.isSzScroll) {
                cellStyleArr.push(".shaoyu-table-tbody>tr:nth-last-child(1)>td{ border-bottom: none !important;}");
                
                _this.renderStyle(cellStyleArr);
            }
        },
        //获取元素
        getDom: function (el) {
            return typeof el === "string" ? document.querySelector(el) : el;
        },
        //插入元素
        append: function (dom, str, type) {
            var _this = this;
            [].forEach.call(_this.parseDom(str, type), function (el) {
                dom.append(el);
            });
        },
        //生成dom元素
        parseDom: function (arg, type) {
            var objE = document.createElement("div");
            if (type == 2) {
                objE = document.createElement("table");
            } else if (type == 3) {
                objE = document.createElement("tr");
            }
            objE.innerHTML = arg;
            return objE.childNodes;
        },
        //解析模板字符串
        parseData: function (el, data) {
            let html = template(el, data)
            if (typeof el === "string") {
                document.getElementById(id).innerHTML = html;
            } else {
                el.innerHTML = html;
            };

            function template(id, obj) {
                let tem = "";
                if (typeof el === "string") {
                    tem = document.getElementById(id).innerHTML;
                } else {
                    tem = el.innerHTML
                }
                let reg = /{{(\w*)}}/
                let arr = []
                while (arr = reg.exec(tem)) {
                    tem = tem.replace(arr[0], obj[arr[1]])
                }
                return tem
            }
        },
        onscroll: function () {
            var fixedDom = document.querySelector("#container div.shaoyu-table-content div.shaoyu-table-body");
            fixedDom.addEventListener('scroll', winScroll, false);

            function winScroll(e) {
                //横向滚动
                document.querySelector("#container div.shaoyu-table-content div.shaoyu-table-header").scrollLeft = fixedDom.scrollLeft;
                // if (fixedDom.offsetLeft < document.body.scrollLeft) {

                // };
                // if (fixedDom.offsetLeft > document.body.scrollLeft) {

                // };
            }
        },
        bindEvent: function () {
            var _this = this;
            var timer;
            window.onresize = function () {
                if (timer) {
                    window.clearTimeout(timer)
                };
                timer = setTimeout(() => {
                    if (_this.options.resizable) {
                        var width = _this.options.el.parentNode.clientWidth;
                        _this.options.el.style.width = width + "px";
                        _this.renderCellWidth();
                    }
                    // 清空计时器
                    timer = null
                }, 500)

                
            }
        }
    };
    //兼容CommonJs规范
    if (typeof module !== 'undefined' && module.exports) module.exports = Shaoyu;

    //兼容AMD/CMD规范
    if (typeof define === 'function') define(function () {
        return Shaoyu;
    });

    //注册全局变量,兼容直接使用script标签引入该插件
    global.Shaoyu = Shaoyu;
    //this,在浏览器环境指window,在nodejs环境指global
    //使用this而不直接用window/global是为了兼容浏览器端和服务端
    //将this传进函数体,使全局变量变为局部变量,可缩短函数访问全局变量的时间
})(this);

 shaoyu.css

/* .shaoyu-table-thead>tr>th {
    padding: 5px 0;
    border-top: none;
    border-left: none;
    border-width: 1px;
    border-style: solid;
    border-color: #eee;
}

.shaoyu-table-cell {
    height: 28px;
    line-height: 28px;
    padding: 0 15px;
    position: relative;
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.shaoyu-table-tbody>tr>td {
    border-width: 1px;
    border-style: solid;
    border-color: #eee;
}


.shaoyu-table-thead>tr>th,
.shaoyu-table-tbody>tr>td {
    position: relative;
    padding: 9px 15px;
    min-height: 20px;
    line-height: 20px;
    font-size: 14px;
}
.shaoyu-table{
    border-collapse: collapse;
    border-spacing: 0;
    width: 100%;
    background-color: #fff;
    color: #666;
} */

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/shaoyu.css" />
    <script src="./js/shaoyu.js"></script>
    <style>
        #container{
            width: 600px;
        }
    </style>
</head>
<body>
    <div id="container"></div>
</body>
<script>
    var aa = new Shaoyu().init({
        el: "#container",
        height: 300,
        resizable: true,
        rows:[
            {key: "name", label:"姓名", align: "left", fixed:"left",  tempelt:function(d){
                return '<span style="color:orange" onclick=showName(\''+d.name+'\')>'+d.name+'</span>';
            }},
            {key: "age", label: "年龄", align: "center" },
            {key: "age3", label: "年龄", align: "center" },
            {key: "age4", label: "年龄", align: "center" },
            {key: "age5", label: "年龄", align: "center" },
            {key: "age2", label: "年龄", align: "center", width: 100 },
            {key: "city", label: "城市", align: "center", width: 100},
            {key: "sex", label: "性别", align: "center", width: 50, tempelt: function(d){
                if(d.sex==0){
                    return "男";
                }
                return "女";
            }}
        ],
        data: [
            {name:"张三", age:"16",age2:"16", city: "西安", sex: "0"},
            {name:"李四", age:"17",age2:"16", city: "咸阳", sex: "0"},
            {name:"王五", age:"18",age2:"16", city: "宝鸡", sex: "1"},
            {name:"戴六", age:"19",age2:"16", city: "渭南", sex: "1"},
            {name:"古七", age:"20",age2:"16", city: "铜川", sex: "0"},
            {name:"羊羊", age:"21", city: "安康", sex: "1"},
            {name:"丽宝宝", age:"22", city: "延安", sex: "0"},
            {name:"曹操", age:"23", city: "榆林", sex: "1"},
            {name:"典韦", age:"24", city: "商洛", sex: "0"},
            {name:"典韦", age:"24", city: "商洛", sex: "0"},
            {name:"典韦", age:"24", city: "商洛", sex: "0"},
            {name:"典韦", age:"24", city: "商洛", sex: "0"},
            {name:"典韦", age:"24", city: "商洛", sex: "0"},
            {name:"三贤", age:"25", city: "汉中", sex: "1"}
        ]
    });
    function showName(name){
        console.log(name)
    }
</script>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/723018.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

mysql的两种安装方式(yum在线安装和通用二进制)

文章目录 msqly的安装一、yum在线安装二、通用二进制安装mysql msqly的安装 一、yum在线安装 yum是一种在线安装方式&#xff0c;通过官网网址在linux下载安装 首先是配置一个yum安装源 yum install http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm也…

基于STM32单片机的智能家居烟雾温度火灾防盗报警的设计与实现

功能介绍 以STM32单片机作为主控系统&#xff1b;LCD1602液晶显示屏来显示显示测得的值&#xff1b;SR501人体红外感应是否有人进行防盗&#xff1b;通过烟雾传感器MQ-2获取前的烟雾值&#xff1b;通过DHT11温湿度传感器来获取当前的温湿度&#xff1b;所有的信息通过通过esp82…

几行代码教你轻松完成超大模型推理:LLaMA-30B+TITAN RTX*4+accelerate

是不是苦于没有ChatGPT的API key或者免费的token而无法愉快地和它玩耍&#xff1f;想不想在有限的计算资源上部署大模型并调戏大模型&#xff1f;&#xff1f;想不想解锁大模型的除了对话之外的其它功能&#xff1f;&#xff1f;&#xff1f;几行代码教你搞定如何在有限的计算资…

Devops系列三(拉取私库的helm chart,将java应用发布部署至k8s的示例)

一、说在前面的话 本文是紧接上文&#xff0c;上面已准备好了helm chart&#xff0c;接下来就是在K8S的kubectl执行部署。 二、使用helm部署 1、在kubectl添加helm 私有repo [adminjenkins]$ helm repo list Error: no repositories to show[adminjenkins]$ helm repo add …

github Copilot使用及代理设置

使用前先保证自己能正常访问github.com&#xff0c;找个科学上网工具 找到系统的hosts文件做个配置&#xff0c;在https://www.ipaddress.com/ 中搜索github.com和api.github.com 140.82.112.4 github.com 140.82.113.6 api.github.com 我的搜索完是上面两个ip&#xff0c;加入…

港联证券|新动向!A股“定点”公告越来越多,大订单透露高景气

近年来&#xff0c;A股商场发布“定点”公告的上市公司越来越多&#xff0c;近三年尤为显着。 需求阐明的是&#xff0c;本文中此类“定点”公告包括定点&#xff08;意向&#xff09;书、定点通知&#xff08;书&#xff09;、定点信&#xff08;函&#xff09;等&#xff0c;…

Devops系列四(使用argocd部署java应用到k8s容器)

一、说在前面的话 上文已为我们准备好了以下内容&#xff1a; 制作java应用的docker镜像&#xff0c;并推送至镜像仓库上传helm yaml代码至gitlab仓库&#xff08;此gitlab和java应用所在的gitlab可以独立&#xff0c;也可以在一起&#xff0c;但是不宜在同一个工程&#xff…

C#:AES的加密解密,用于明文加密

大白话理解&#xff1a;将明眼能看到的字符给用另一种读不懂的语言给翻译&#xff0c;就像是摩斯密码……就像base64加密&#xff0c;都有异曲同工之妙。 建一个新的类&#xff08;这里放了aes加密解密的方法&#xff09; public static class AesPassword{/// <summary&g…

解锁Nginx的奇幻世界:入门

文章目录 一 Nginx简介1.1 C10k问题1.2 Nginx 二 Nginx的作用2.1 反向代理2.2 方向代理演示2.3 负载均衡2.4 动静分离 三 Nginx的安装3.1 windows环境 一 Nginx简介 1.1 C10k问题 C10k问题指的是在一个服务器端同时处理成千上万&#xff08;10,000&#xff09;个并发连接的能力…

【论文精读】RA-MVSNet:Multi-View Stereo Representation Revisit: Region-Aware MVSNet

今天读的是一篇发表在CVPR2023上的文章&#xff0c;作者来自浙大与阿里巴巴。 文章链接&#xff1a;Multi-View Stereo Representation Revisit: Region-Aware MVSNet 目录 Abstract1 Introduction2 Related Work3 Method3.1 Cost Volume Construction3.2 Signed Distance Supe…

十一、弹性盒flex - 介绍

目录 1.flex介绍 2.详解 一、flex介绍 flex&#xff08;弹性盒&#xff0c;伸缩盒&#xff09; css中的又一种布局手段&#xff0c;它主要用来代替浮动来完成页面的布局。flex可以使元素具有弹性&#xff0c;让元素可以跟随页面的大小的改变而改变。 我们知道float&#xff1…

【linux kernel】一文总结linux内核通知链

文章目录 1、通知链简介2、通知链的类型3、原理分析和API&#xff08;1&#xff09;注销通知器&#xff08;2&#xff09;注销通知器&#xff08;3&#xff09;通知链的通知 4、实例代码&#xff08;1&#xff09;定义一个通知链&#xff08;2&#xff09;实现观察者模块&#…

从源码全面解析 Java SPI 的来龙去脉

一、引言 对于 Java 开发者而言&#xff0c;关于 dubbo &#xff0c;我们一般当做黑盒来进行使用&#xff0c;不需要去打开这个黑盒。 但随着目前程序员行业的发展&#xff0c;我们有必要打开这个黑盒&#xff0c;去探索其中的奥妙。 虽然现在是互联网寒冬&#xff0c;但乾坤…

数字化转型应该从哪里开始?

数字化转型可能是一个复杂的过程&#xff0c;涉及将数字技术和战略集成到组织的各个方面。虽然具体的起点可能会根据组织的规模、行业和目标而有所不同&#xff0c;但数字化转型计划通常从以下几个共同领域开始&#xff1a; 愿景和战略&#xff1a;转型之旅应从与组织目标一致的…

百万数据SQL优化技巧,看这一篇就够了(实操+详细总结)

前言&#xff1a;这次准备了100W的数据进行SQL性能测试&#xff0c;数据库采用的是MySQL&#xff0c;总共介绍了常见的15种SQL优化方式&#xff0c;每一种优化方式都进行了实打实的测试&#xff0c;逐行讲解&#xff0c;通俗易懂&#xff01; 目录 一、准备数据 1、创建表结构…

Windows环境下安装和配置python环境

Windows环境下安装和配置python环境 1.官网下载&#xff1a;https://www.python.org/downloads/release 2.安装&#xff1a;自定义路径即可无脑下一步 3.cmd打开控制台&#xff0c;输入python&#xff0c;如果页面切换成以下样子就说明安装成功了 4.运行python脚本【步骤3切入…

555、Vue 3 学习笔记 -【常用Composition API(四)】 2023.07.06

目录 一、setup的两个注意点1. setup执行的时机2. setup的参数 二、 计算属性与监视1. computed函数2. watch函数3. watchEffect函数 三、参考链接 一、setup的两个注意点 1. setup执行的时机 在beforeCreate之前执行一次&#xff0c;this是undefined 2. setup的参数 props…

GitLab名词介绍

GitLab名词介绍 分支&#xff1a;active、stale、default、protected IDEA中git面板&#xff1a;本地、远程、HEAD 合并时的选项&#xff1a;Delete、squash 查看Git常用操作 分支&#xff1a;active、stale、default、protected 在分支页面下&#xff0c;有active、stale…

k8s中kubectl陈述式/声明式资源管理

k8s陈述资源管理方法的说明 1.kubernetes 集群管理集群资源的唯一入口是通过相应的方法调用 apiserver 的接口 2.kubectl 是官方的CLI命令行工具&#xff0c;用于与 apiserver 进行通信&#xff0c;将用户在命令行输入的命令&#xff0c;组织并转化为 apiserver 能识别的信息&…

TongWeb8关于内存泄露提示: To prevent a memory leak

原因&#xff1a; 该问题与 https://blog.csdn.net/realwangpu/article/details/109510297 相同&#xff0c;TongWeb7, TongWeb8在卸载应用时&#xff0c;会尝试回收可能存在的内存泄露&#xff0c; 本质应该从应用方面解决。 解决办法&#xff1a; 若无法修改应用&#xff0c…