【用JS自制表格软件玩数据】7. 设计常用的样式功能与单元格合并

当写完本系列后,我会把源代码分享出来给大家。本课程也会持续更新与矫正。欢迎留言指正!

简介

目前,我们的表格软件的基础结构已经开发得差不多了。现在开始要设计一些字体修改,加粗,变斜,合并单元格等功能。具体功能就放在顶部的工具栏中。

在这里插入图片描述

存储对象列表

这其中,需要一个鼠标的储存对象,它会把选中的单元格列表,都存储在 cells 这个属性中。以数组的形式存放。

/**
 * @property {Object} focus 鼠标点击的焦点
 */
this.focus = {
	"x":-1,"y":-1, // 鼠标选中状态:{-1 未选中,0 选中全部, 其他选中某元素
	"cells":[], // 储存已经被选中的单元格列表
	"Rectangle":{
		"Rowlength":0,
		"Collength":0
	},
	"leftbuttonHold" : false, // 鼠标左键在单元格中按住
	"menufocus":0, // 当前选中哪个顶部菜单
	"hasPopwin":false, // 判断是否有弹窗
	"sheets_size":[]
}

字体修改

在这里插入图片描述

{
	"Tag" : "select", "ID" : this.creatGlobalId(), "ClassName" : "font-name",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		if(!that.focus.x && !that.focus.y){
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.fontFamily = target.value;
			}
		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				cell.style.fontFamily = target.value;
			}
		}
	}, "change":function(){ console.log("change"); }},
	"Child":[
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:隶书", "Text": "隶书" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:微软雅黑", "Text": "微软雅黑" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:宋体", "Text": "宋体" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Noto Sans", "selected": "selected", "Text": "Noto Sans" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Arial", "Text": "Arial" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Calibri", "Text": "Calibri" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Comic Sans MS", "Text": "Comic Sans MS" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Courier New", "Text": "Courier New" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Impact", "Text": "Impact" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Georgia", "Text": "Georgia" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Garamond", "Text": "Garamond" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Open Sans", "Text": "Open Sans" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Palatino", "Text": "Palatino" },
		{ "Tag": "option", "ID": this.creatGlobalId(), "Style": "--font:Verdana", "Text": "Verdana" }
	]
}

尺寸修改

在这里插入图片描述

{
	"Tag" : "select", "ID" : this.creatGlobalId(), "ClassName" : "font-size",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.fontSize = target.value+"px";
			}

		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				cell.style.fontSize = target.value+"px";
			}
		}
	}, "change":function(){}},
	"Child":[
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "selected" : "selected", "Text" : "14" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "16" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "18" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "20" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "22" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "24" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "26" },
		{ "Tag" : "option", "ID" : this.creatGlobalId(), "Text" : "28" }
	]
}

加黑

在这里插入图片描述

{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon icon-bold",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.fontWeight = "bold";
			}

		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				console.log(cell.style.fontWeight);
				if(cell.style.fontWeight == "bold"){
					cell.style.fontWeight = "normal";
				}else{
					cell.style.fontWeight = "bold";
				}
			}
			
			console.log(that.focus);
		}
	}, "change":function(){}}
}

斜体

在这里插入图片描述

{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon icon-italic",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.fontStyle = "italic";
			}

		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				console.log(cell.style.fontStyle);
				if(cell.style.fontStyle == "italic"){
					cell.style.fontStyle = "normal";
				}else{
					cell.style.fontStyle = "italic";
				}
			}
		}
	}, "change":function(){}}
}

下划线

在这里插入图片描述

{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon icon-underline",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.textDecoration = "underline";
			}

		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				console.log(cell.style.textDecoration);
				if(cell.style.textDecoration == "underline"){
					cell.style.textDecoration = "none";
				}else{
					cell.style.textDecoration = "underline";
				}
			}
		}
	}, "change":function(){}}
}

居中,居左,居右

在这里插入图片描述

{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon icon-alignl",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.justifyContent = "left";
			}
		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				cell.style.justifyContent = "left";
			}
		}
	}, "change":function(){}}
},
{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon icon-alignc",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.justifyContent = "center";
			}

		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				cell.style.justifyContent = "center";
			}
		}
	}, "change":function(){}}
},
{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon icon-alignr",
	"Action":{ "click":function(ev){
		var target = ev.target || ev.srcElement;
		if(!that.focus.x && !that.focus.y){
			
			var cells__inputs = document.getElementsByClassName("cells__input");
			var length = cells__inputs.length;
			for(var i = 0;i<length;i++){
				cells__inputs[i].style.textAlign = "right";
			}

		} else if(that.focus["cells"].length != 0){
			var length = that.focus["cells"].length;
			for(var i = 0;i<length;i++){
				var cell = that.getcell(that.focus["cells"][i]);
				cell.style.justifyContent = "right";
			}
		}
	}, "change":function(){}}
}

单元格的合并与居中

在这里插入图片描述

{
	"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "merge-center",
	"Action":{ "click":function(){ }, "change":function(){}},
	"Child":[
		{
			"Tag" : "div", "ID" : this.creatGlobalId(), "ClassName" : "icon",
			"Action":{ "click":function(){ }, "change":function(){}},
		},
		{
			"Tag" : "span", "ID" : this.creatGlobalId(), "Text" : "合并与居中",
			"Action":{
				"click":function(ev){
					var target = ev.target || ev.srcElement;
					
					if(that.focus["cells"].length == 0){
						console.log("未选中单元格");
						return;
					}

					var length = that.focus["cells"].length;
					if(length == 1){
						console.log("只选中一个单元格");
						
						var bigcell = that.getcell(that.focus["cells"][0]);
						
						// var gridColumn = bigcell.getAttribute("grid-column").split("/");
						// gridColumn[1] = "span 1";
						// var gridRow = bigcell.getAttribute("grid-row").split("/");
						// gridRow[1] = "span 1";
						bigcell.style.gridColumnEnd = "span 1";
						bigcell.style.gridRowEnd = "span 1";
						var ttt = bigcell.getAttribute("data-merge").split(":");
						if( ttt[0] != ttt[1] ){
							var displaylist = that.charcode.GetRectangle(ttt[0],ttt[1])["Rectangle"];
							for(var i in displaylist){
								that.getcell(displaylist[i]).style.display = "flex";
							}
							ttt[1] = ttt[0];
							bigcell.setAttribute("data-merge",ttt.join(":"));
						}
					}
					else{
						for(var i = 0; i< length; i++){
							var ttt = that.getcell(that.focus["cells"][i]).getAttribute("data-merge").split(":");
							if( ttt[0] != ttt[1] ){
								console.log("无法合并");
								return;
							}
						}
						var i=0;
						var firstRowCol = that.charcode.getRowCol(that.focus["cells"][i]);
						var ColStart = that.charcode.CharToDecimal(firstRowCol[0])+1;
						var RowStart = parseInt(firstRowCol[1])+1;
						
						var firstElement = that.getcell(that.focus["cells"][i]); // 获取第一个元素,合并第一个元素
						var ttt = firstElement.getAttribute("data-merge").split(":");
						ttt[1] = that.focus["cells"][that.focus["cells"].length-1];
						firstElement.setAttribute("data-merge",ttt.join(":"));
						
						firstElement.style.gridColumn = ColStart+"/span "+that.focus["Rectangle"]["Collength"];
						firstElement.style.gridRow = RowStart+"/span "+that.focus["Rectangle"]["Rowlength"];
						firstElement.style.justifyContent = "center";
						
					
						for(var i = 1; i< length; i++){
							that.getcell(that.focus["cells"][i]).style.display = "none";
						}
						that.emptyselectcells();
					}
					// var currentID = target.getAttribute("id");

				}, "change":function(){}
			},
		},
	]
}

最终效果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妇男主任

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值