EffectsDocumentationfirst stepGet the icon Form object from tableFactory. or: Step 2Load parameters and pass in json object Table effect: json parameter details: Examples: corresponds to: Step 3Load the table into the div with the corresponding ID in the page. like: The width of the TD can be dynamically adjusted according to the number of columns Put it into a separate file and call it directly. Source code//Table factory var tableFactory = function(type){ if (this instanceof tableFactory) { return new this[type](); }else{ return new tableFactory(type); //Prevent the situation where new is not written } } tableFactory.prototype = { chartForm : function(){ var html = ''; //Private properties this.loadParams = function(opts){ var jsonArr = []; //JSON array var icount = 0; //Used to control row color var rowHeaderArr = []; //Format array of the first column of each row if (opts.jsonArr) { jsonArr = opts.jsonArr; } if (opts.rowHeaderArr) { rowHeaderArr = opts.rowHeaderArr; } var colNum = jsonArr.length; //Record total number of columns var rowNum = rowHeaderArr.length - 1; //Total number of rows (excluding the first row) html = "<TABLE id='table' style=\"border-collapse:collapse;border-spacing:0;border:1px solid #ccc;font-size:12px;text-align:center;\" >"+ "<TBODY><TR> "+ "<TD > </TD>"; //The empty TD in the upper left corner //Spell the first line for(var i = 0;i < colNum;i++){ var c1 = jsonArr[i].c1; html += '<TD style="border:1px solid #ccc;height:14px;background-color:#e2fdfe;font-size:14px;font-weight:bold;padding:3px;" >' + c1 + '</TD>'; } html += '</TR><TR>'; for(var i = 0;i < rowNum + 1;i++){ //Spelling row header var colorBox = rowHeaderArr[i].split(',')[0]; var hearderText = rowHeaderArr[i].split(',')[1]; html += "<TD class='colorBox' style='padding:3px;height:14px;border:1px solid #ccc;width:66px;text-align:center;'><div style='border-radius:2px 2px 2px 2px;display:inline-block;width:12px;height:12px;background-color:"+colorBox+";float:left;'></div>"+hearderText+"</TD>"; //Spell all columns to the right of this row for(var j = 0;j < colNum;j++){ //alert(i); var colValue = jsonArr[j]['c'+(i+2)]; var tdWidth; if(colNum <= tableFactory.TD_WIDTHS.length) tdWidth = tableFactory.TD_WIDTHS[colNum-1]; else tdWidth = tableFactory.TD_WIDTHS[tableFactory.TD_WIDTHS.length - 1]; console.info(tdWidth); if(i%2 == 0){ html += '<TD style = "height:14px;border:1px solid #ccc;width:'+ tdWidth +'px;text-align:center;background-color:#e2fdfe;">'+colValue+'</TD>'; }else{ html += '<TD style = "height:14px;border:1px solid #ccc;width:'+ tdWidth +'px;text-align:center;background-color:#fff;">'+colValue+'</TD>'; } } //line break html += '</tr><tr>'; } html += '</TR><TR>'; html += '</TR>'; html += '</TBODY></TABLE>'; } ; this.loadData = function(houseId,callback){ document.getElementById(houseId).innerHTML = html; //Display table if(callback) callback(); } } } tableFactory.TD_WIDTHS = [220,190,150,120,90,60,10]; Demo: <script src='common.js'></script> <div id='TB' class='TB'></div> <div id='TB1' class='TB'></div> <div id='TB2' class='TB'></div> <div id='TB3' class='TB'></div> <div id='TB4' class='TB'></div> <style> .TB { padding:6px; } </style> <script> var chartForm = tableFactory('chartForm'); chartForm.loadParams({ jsonArr: [ {c1:'Beijing',c2:2,c3:3,c4:3,c5:1,c6:6}, {c1:'Shanghai',c2:2,c3:2,c4:3,c5:1,c6:6}, {c1:'Henan',c2:2,c3:4,c4:3,c5:1,c6:6}, {c1:'Hebei',c2:2,c3:6,c4:3,c5:1,c6:6}, {c1:'Northeast',c2:2,c3:6,c4:3,c5:1,c6:6}, {c1:'Sichuan',c2:2,c3:6,c4:3,c5:1,c6:6}, {c1:'重庆',c2:2,c3:6,c4:3,c5:1,c6:6} ], rowHeaderArr: [ '#000CCC,Total number of tasks', //row name '#990033,Project establishment stage', '#66FF00, Inspection phase', '#663399, trial stage', '#33CCFF, reporting stage' ] }); chartForm.loadData('TB'); chartForm.loadParams({ jsonArr : [ {c1:'Beijing',c2:2,c3:3,c4:3,c5:1,c6:6}, {c1:'Shanghai',c2:2,c3:2,c4:3,c5:1,c6:6}, {c1:'Henan',c2:2,c3:4,c4:3,c5:1,c6:6}, {c1:'Hebei',c2:2,c3:6,c4:3,c5:1,c6:6}, {c1:'Northeast',c2:2,c3:6,c4:3,c5:1,c6:6}, {c1:'Sichuan',c2:2,c3:6,c4:3,c5:1,c6:6}, ], rowHeaderArr : [ '#000CCC,Total number of tasks', //row name '#990033,Project establishment stage', '#66FF00, Inspection phase', '#663399, trial stage', '#33CCFF, reporting stage' ] }); chartForm.loadData('TB1'); chartForm.loadParams({ jsonArr: [ {c1:'Beijing',c2:2,c3:3,c4:3,c5:1,c6:6}, {c1:'Shanghai',c2:2,c3:2,c4:3,c5:1,c6:6}, {c1:'Henan',c2:2,c3:4,c4:3,c5:1,c6:6}, {c1:'Hebei',c2:2,c3:6,c4:3,c5:1,c6:6}, {c1:'Northeast',c2:2,c3:6,c4:3,c5:1,c6:6}, ], rowHeaderArr: [ '#000CCC,Total number of tasks', //row name '#990033,Project establishment stage', '#66FF00, Inspection phase', '#663399, trial stage', '#33CCFF, reporting stage' ] }); chartForm.loadData('TB2'); </script> The above is the details of writing a super practical JavaScript table plug-in in Java programming. For more information about Java programming JavaScript table plug-in, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Distributed monitoring system Zabbix uses SNMP and JMX channels to collect data
>>: HTML Tutorial: Collection of commonly used HTML tags (4)
Abstract: HBase comes with many operation and mai...
Table of contents Overview What are Generics Buil...
Elasticsearch is very popular now, and many compa...
Explain the whole process of CLion from scratch. ...
Table of contents 1. Optional chaining operator [...
Sometimes, we need to use the hyperlink <a> ...
Since I need to learn how to build servers and da...
Table of contents 1. Vue3 component communication...
This article example shares the specific code of ...
The installation and configuration method of MySQ...
Table of contents 1. Introduction to SELinux 2. B...
Table of contents JavaScript prototype chain Obje...
This article describes how to install mysql5.7.16...
Docker is an open source project that provides an...
The recommended code for playing background music ...